Question: Count non-zero elements along the column and append to table in R
0
gravatar for User000
6 months ago by
User000240
User000240 wrote:

I have a table like this,

gene1 a 2 0 1 0
gene2 b 5 0 2 2
gene3 a 7 4 0 0

I want to count the number of non-zero elements in rows and columns and append it to the same table. This is how I did it for rows: myfile$rowsum <- rowSums(myfile[4:6] != 0). For columns I am trying to make this one work, but it starts from the 1st column and not the 4th: rbind(myfile, colSums(myfile != 0)) Not sure how to append it and how to move starting from column 4. So i want something like this

gene1 a 2 0 1 0 1
gene2 b 5 0 2 2 2
gene3 a 7 4 0 0 1
          1 2 1
R • 259 views
ADD COMMENTlink modified 6 months ago by avaneesh.t20 • written 6 months ago by User000240
3
gravatar for Carlo Yague
6 months ago by
Carlo Yague3.2k
Belgium
Carlo Yague3.2k wrote:

This is how I did it for rows: myfile$rowsum <- rowSums(myfile[4:6] !=0)

Shouldn't it be rowSums(myfile[,4:6]) ?

For columns I am trying to make this one work, but it starts from the 1st column and not the 4th: rbind(myfile, colSums(myfile != 0))

Try rbind(myfile, c("colsum", "NA", "NA", colSums(myfile[,4:6] != 0)))

ADD COMMENTlink modified 6 months ago • written 6 months ago by Carlo Yague3.2k

This is working, thanks!

ADD REPLYlink written 6 months ago by User000240
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1396 users visited in the last hour