Answers.Body: <p>I assume by "reverse sort of variance" you mean "highest variance". Assuming you made a matrix out of that (set the row names to the first column and then remove it) and called it <code>m</code>:</p>
  
  <pre><code>sel = order(apply(m, 1, var), decreasing=TRUE)[1:100]
  </code></pre>
  
  <p><code>sel</code> then contains the indices into your matrix (or the original dataframe).</p>
  
  <p>BTW, I hope you're not filtering by variance before performing differential expression. That would be incredibly incorrect. Doing this sort of filtering for performing PCA or something like that can be useful, though you should really be using normalized counts (or a transformation of them).</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-24T08:44:18.647Z
Answers.Text: Thanks. yes, I'm doing this filtering for PCA not for differential expression.
Answers.UserDisplayName: beginner
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-24T08:47:29.240Z
Answers.Text: BTW, before doing this I converted Ensembl_ID to gene names and I have multiple genes now so I did like this U2 <- aggregate(. ~ gene_name, data = U, max) and on that I will be calculating variance. Do you think this is right?
Answers.UserDisplayName: beginner
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-24T08:53:51.153Z
Answers.Text: Usually what I do is using the mean of the different transcripts for each gene, but it is right (if you can justify it).
Answers.UserDisplayName: llrs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-24T08:56:58.063Z
Answers.Text: @raju Do not convert to gene names until you are completely finished with the analysis.
Answers.UserDisplayName: Devon Ryan
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-24T09:03:02.130Z
Answers.Text: Ok. But if there are duplicate Ensembl_ids can I do this U2 <- aggregate(. ~ Ensembl_ID, data = U, max)
Answers.UserDisplayName: beginner
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-24T09:15:03.520Z
Answers.Text: And when I did this sel = order(apply(m, 1, var), decreasing=TRUE)[1:100] I see int[1:100] with numbers.
Answers.UserDisplayName: beginner
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-24T09:22:18.027Z
Answers.Text: If there are duplicate ensembl IDs then you did something wrong. You should see a vector with 100 numbers, as I said their the indices into `m`.
Answers.UserDisplayName: Devon Ryan
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-24T08:39:45.477Z
Answers.LastActivityDate: 2018-04-24T08:39:45.477Z
Answers.UserDisplayName: Devon Ryan
Body: <p>I have a gene expression data (read counts) table with many samples. Showing some of them below:</p>
  
  <pre><code>Ensembl_ID          A   B   C   D   E   F   G
  ENSG00000236601.2   0   0   0   1   0   0   1
  ENSG00000237094.12  2   187 442 109 88  144 486
  ENSG00000269732.1   0   0   0   0   0   0   0
  ENSG00000284733.1   0   0   0   0   0   0   0
  ENSG00000233653.3   0   0   0   0   0   0   0
  ENSG00000250575.1   1   11  17  5   5   8   17
  ENSG00000278757.1   0   0   2   0   1   0   7
  ENSG00000230021.9   0   25  30  27  9   8   11
  ENSG00000235146.2   0   0   0   0   0   0   0
  ENSG00000225972.1   0   7   3016    11  5   14  5
  ENSG00000225630.1   1   113 194 76  91  47  94
  ENSG00000237973.1   25  1037    9767    1160    886 1321    5220
  </code></pre>
  
  <p>I want to select top 100 genes with reverse sort of variance. How to do that?</p>
  
Tags: <rna-seq><r><bioconductor><reads><filtering>
Title: Select top 100 genes ranked by variance in read counts
ThreadText: # Select top 100 genes ranked by variance in read counts
  
  (Asked by: username_0 on 2018-04-24)
  
  I have a gene expression data (read counts) table with many samples. Showing some of them below:
  
  ```
  Ensembl_ID          A   B   C   D   E   F   G
  ENSG00000236601.2   0   0   0   1   0   0   1
  ENSG00000237094.12  2   187 442 109 88  144 486
  ENSG00000269732.1   0   0   0   0   0   0   0
  ENSG00000284733.1   0   0   0   0   0   0   0
  ENSG00000233653.3   0   0   0   0   0   0   0
  ENSG00000250575.1   1   11  17  5   5   8   17
  ENSG00000278757.1   0   0   2   0   1   0   7
  ENSG00000230021.9   0   25  30  27  9   8   11
  ENSG00000235146.2   0   0   0   0   0   0   0
  ENSG00000225972.1   0   7   3016    11  5   14  5
  ENSG00000225630.1   1   113 194 76  91  47  94
  ENSG00000237973.1   25  1037    9767    1160    886 1321    5220
  
  ```
  
  I want to select top 100 genes with reverse sort of variance. How to do that?
  
  **Tags:** rna-seq, r, bioconductor, reads, filtering
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-04-24. Score: 4 | Accepted answer
  
  I assume by "reverse sort of variance" you mean "highest variance". Assuming you made a matrix out of that (set the row names to the first column and then remove it) and called it `m`:
  
  ```
  sel = order(apply(m, 1, var), decreasing=TRUE)[1:100]
  
  ```
  
  `sel` then contains the indices into your matrix (or the original dataframe).
  
  BTW, I hope you're not filtering by variance before performing differential expression. That would be incredibly incorrect. Doing this sort of filtering for performing PCA or something like that can be useful, though you should really be using normalized counts (or a transformation of them).
  
  > **Comments:**
  > **username_0**: Thanks. yes, I'm doing this filtering for PCA not for differential expression.  
  > **username_0**: BTW, before doing this I converted Ensembl_ID to gene names and I have multiple genes now so I did like this U2 <- aggregate(. ~ gene_name, data = U, max) and on that I will be calculating variance. Do you think this is right?  
  > **username_2**: Usually what I do is using the mean of the different transcripts for each gene, but it is right (if you can justify it).  
  > **username_1**: @raju Do not convert to gene names until you are completely finished with the analysis.  
  > **username_0**: Ok. But if there are duplicate Ensembl_ids can I do this U2 <- aggregate(. ~ Ensembl_ID, data = U, max)  
  > **username_0**: And when I did this sel = order(apply(m, 1, var), decreasing=TRUE)[1:100] I see int[1:100] with numbers.  
  > **username_1**: If there are duplicate ensembl IDs then you did something wrong. You should see a vector with 100 numbers, as I said their the indices into `m`.  
  
  ---
  
  