Question: Count non-zero elements along the column and append to table in R
0
gravatar for User000
4.0 years ago by
User000420
User000420 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 • 4.9k views
ADD COMMENTlink modified 4.0 years ago by avaneesh.t20 • written 4.0 years ago by User000420
3
gravatar for Carlo Yague
4.0 years ago by
Carlo Yague5.7k
Canada
Carlo Yague5.7k 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 4.0 years ago • written 4.0 years ago by Carlo Yague5.7k

This is working, thanks!

ADD REPLYlink written 4.0 years ago by User000420
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: 1628 users visited in the last hour
_