How do I create a filtered gene list using expression medians 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:

&gt;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:

&gt;data[data == 0] &lt;- NA


I then got medians for each column using:

&gt;colMed &lt;- 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:

&gt;if (data[,1]&gt;=colMed [1]) 
   {
   finalExpressedGeneList &lt;- 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
&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



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...
This below should work. It returns a list,

## 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


Hope I got it correct!