Question: Count non-zero elements along the column and append to table in R
0
gravatar for User000
15 months ago by
User000250
User000250 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 • 921 views
ADD COMMENTlink modified 15 months ago by avaneesh.t20 • written 15 months ago by User000250
3
gravatar for Carlo Yague
15 months ago by
Carlo Yague4.0k
Belgium
Carlo Yague4.0k 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 15 months ago • written 15 months ago by Carlo Yague4.0k

This is working, thanks!

ADD REPLYlink written 15 months ago by User000250
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: 1984 users visited in the last hour