Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 206278

How to reshape and then convert a data frame into dgCMatrix?

$
0
0

I have a data frame like following (the rownames are "1", "2", "3"...). Since there are non unique entries in each column, I cannot assign any of them as row names.

gene cell count
a    c1    1
a    c2    1
a    c3    4
b    c1    3
b    c2    1
b    c3    1
f    c1    3
d    c8    9
e    c11   1

Each gene is measured in each cell (means they have a value in count column) but zero counts are not shown (for example gene "a" has zero counts in cells c8 and c11, hence do not appear).

Now I want to reshape/convert the data frame into dgCMatrix with following arrangement

(genes as row names, cells as column names and count values as data points)

   c1  c2  c3  c8  c11 
a  1   1   4   .    .
c  3   1   1   .    . 

where "." corresponds to a zero count.

I tried reshape, reshape2, as.matrix as mentioned in many posts here, but no success. Kindly help me with this particular example.

Best wishes, Tushar


Viewing all articles
Browse latest Browse all 206278

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>