<p>This below should work. It returns a list,</p>

<pre><code>## example data
j = 5 
M &lt;- matrix(NA,j,j)
M &lt;- sapply(1:j, function(i) `length&lt;-`(1:(j-i+1), j))
rownames(M) &lt;- c("gene1", "gene2", "gene3", "gene4", "gene5")
colnames(M) &lt;- c("cellLine1", "cellLine2", "cellLine3", "cellLine4", "cellLine5")
#####
genelist = apply(M,2,function(i)rownames(M)[which(i&gt;=median(i,na.rm=T))])

####
#you can call the genes for cellline 1 using:
genelist$cellLine1
</code></pre>

<p>Hope I got it correct!</p>

CC BY-SA 4.0
2019-08-24T07:02:03.533Z
2019-08-26T14:48:14.043Z
2019-08-26T14:48:14.043Z
StupidWolf
<p>Forgive the simple noob question</p>

<p>I have TPM data of ~50k genes (rows) across ~1k cell lines (columns). In R, I would like to output an "intermediate expression" gene list for each cell line, like:</p>

<pre class="lang-r prettyprint-override"><code>&gt;head(finalExpressedGeneList, n=3)

Celltype1    Celltype2    CelltypeN
gene1        gene5        gene3
gene3        gene6        gene6
etc          etc          etc
</code></pre>

<p>I am defining intermediate expression as "gene expression >= median value within each cell line." I have already replaced all 0 values with NA using:</p>

<pre class="lang-r prettyprint-override"><code>&gt;data[data == 0] &lt;- NA
</code></pre>

<p>I then got medians for each column using:</p>

<pre class="lang-r prettyprint-override"><code>&gt;colMed &lt;- apply(data,2, FUN = median, na.rm = TRUE)
</code></pre>

<p>Now I need to somehow filter the rows from each column and copy the associated gene into a new matrix. I started building the code below:</p>

<pre class="lang-r prettyprint-override"><code>&gt;if (data[,1]&gt;=colMed [1]) 
   {
   finalExpressedGeneList &lt;- row.names(data)
   }
</code></pre>

<p>But I realized that even if I got it working, I would still have to iterate over all of my columns. I'm sure that there is a cleaner way to do this. </p>

<p>EDIT (adding example data)</p>

<pre class="lang-r prettyprint-override"><code>#create example data
&gt;j = 5 
&gt;M &lt;- matrix(NA,j,j)
&gt;M &lt;- sapply(1:j, function(i) `length&lt;-`(1:(j-i+1), j))
&gt;rownames(M) &lt;- c("gene1", "gene2", "gene3", "gene4", "gene5")
&gt;colnames(M) &lt;- c("cellLine1", "cellLine2", "cellLine3", "cellLine4", "cellLine5")
&gt;view(M)

       cellLine1   cellLine2   cellLine3   cellLine4   cellLine5
gene1          1           1           1           1           1
gene2          2           2           2           2          NA 
gene3          3           3           3          NA          NA
gene4          4           4          NA          NA          NA
gene5          5          NA          NA          NA          NA

</code></pre>

<p>I would like to get an output of:</p>

<pre class="lang-r prettyprint-override"><code>
cellLine1   cellLine2   cellLine3   cellLine4   cellLine5
    gene3       gene3       gene2       gene2       gene1
    gene4       gene4       gene3
    gene5

</code></pre>

<p>because the median of cellLine1 has three genes greater than or equal to the median of that column (3), cellLine2 has two genes that are greater than or equal to the median of that column (2.5), etc...</p>

<r><rna-seq><filtering>
How do I create a filtered gene list using expression medians
# How do I create a filtered gene list using expression medians

(Asked by: username_0 on 2019-08-23)

Forgive the simple noob question
I have TPM data of ~50k genes (rows) across ~1k cell lines (columns). In R, I would like to output an "intermediate expression" gene list for each cell line, like:
>head(finalExpressedGeneList, n=3)

Celltype1    Celltype2    CelltypeN
gene1        gene5        gene3
gene3        gene6        gene6
etc          etc          etc

I am defining intermediate expression as "gene expression >= median value within each cell line." I have already replaced all 0 values with NA using:
>data[data == 0] <- NA

I then got medians for each column using:
>colMed <- apply(data,2, FUN = median, na.rm = TRUE)

Now I need to somehow filter the rows from each column and copy the associated gene into a new matrix. I started building the code below:
>if (data[,1]>=colMed [1]) 
   {
   finalExpressedGeneList <- row.names(data)
   }

But I realized that even if I got it working, I would still have to iterate over all of my columns. I'm sure that there is a cleaner way to do this. 
EDIT (adding example data)
#create example data
>j = 5 
>M <- matrix(NA,j,j)
>M <- sapply(1:j, function(i) `length<-`(1:(j-i+1), j))
>rownames(M) <- c("gene1", "gene2", "gene3", "gene4", "gene5")
>colnames(M) <- c("cellLine1", "cellLine2", "cellLine3", "cellLine4", "cellLine5")
>view(M)

       cellLine1   cellLine2   cellLine3   cellLine4   cellLine5
gene1          1           1           1           1           1
gene2          2           2           2           2          NA 
gene3          3           3           3          NA          NA
gene4          4           4          NA          NA          NA
gene5          5          NA          NA          NA          NA


I would like to get an output of:

cellLine1   cellLine2   cellLine3   cellLine4   cellLine5
    gene3       gene3       gene2       gene2       gene1
    gene4       gene4       gene3
    gene5


because the median of cellLine1 has three genes greater than or equal to the median of that column (3), cellLine2 has two genes that are greater than or equal to the median of that column (2.5), etc...

**Tags:** r, rna-seq, filtering

---

## 1 answers

### Answer by username_1 on 2019-08-24. Score: 0 | Accepted answer

This below should work. It returns a list,
## example data
j = 5 
M <- matrix(NA,j,j)
M <- sapply(1:j, function(i) `length<-`(1:(j-i+1), j))
rownames(M) <- c("gene1", "gene2", "gene3", "gene4", "gene5")
colnames(M) <- c("cellLine1", "cellLine2", "cellLine3", "cellLine4", "cellLine5")
#####
genelist = apply(M,2,function(i)rownames(M)[which(i>=median(i,na.rm=T))])

####
#you can call the genes for cellline 1 using:
genelist$cellLine1

Hope I got it correct!


---

