Count non-zero elements along the column and append to table in R
2
0
Entering edit mode
7.7 years ago
User000 ▴ 710

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 • 6.4k views
ADD COMMENT
3
Entering edit mode
7.7 years ago

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 COMMENT
0
Entering edit mode

This is working, thanks!

ADD REPLY

Login before adding your answer.

Traffic: 2728 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6