[R] How to remove rows in a matrix having 0 as value in columns per condition

arun smartpink111 at yahoo.com
Thu Jan 2 15:58:21 CET 2014

Hi,It is not very clear.

data1 <- read.table("PGRTvsPDGRT_frags.txt",header=TRUE,stringsAsFactors=FALSE)
mat1<- as.matrix(data1[,-1])
row.names(mat1)<- data1[,1]
res <- mat1[apply(mat1,1,function(x) all(x[1:6]!=0) & all(x[7:12]!=0)),]
#[1] 0

#depending upon what you want
res1 <- mat1[apply(mat1,1,function(x) all(x[1:6]!=0) | all(x[7:12]!=0)),]
#[1] 12750    12


On Thursday, January 2, 2014 7:47 AM, Vivek Das <vd4mmind at gmail.com> wrote:

Dear Users,

I have few queries. I am having a matrix with some counts. there are 2 conditions for each. So I have a matrix with 12 columns with counts. First column is the id and next 6 columns are one condition and the second 6 columns are another condition. I want to remove the rows where each condition is having one 0 column. So for a rows if in both the conditions have one single 0 (which means each condition is having a 0 value) then that row is removed. How can we do that. I was doing like this earlier but we are left with some rows.
data1<- read.table("~/Desktop/Bonn_New_Pas_algo_data/results_30092013/edgeR_24122013/PGRTvsPDGRT/PGRTvsPDGRT_frags.txt",header=TRUE,stringsAsFactors=FALSE)
mat1<- as.matrix(data1[,-1])
row.names(mat1)<- data1[,1]
test <- apply(mat1, 1,function(x) all(x[1:6]==0) | all(x[7:12]==0) )
test1 <- mat1[!test,]

But this does not seem correct as I am having some rows like 

XLOC_004048 26.0133 0 0 0 0 232.296 2.676 8.21482 2.61507 0 0 0 
XLOC_004050 0 1 4 36 20 0 0 2 1 0 1 9  

This should not be there. Even if both condition together is having one column as zero in each condition in a row then it should be removed. but here we can see more than one column is having 0 values in both condition and still it is there.

The the matrix I am working upon is in attachment.
Can anyone give any suggestions


Vivek Das

More information about the R-help mailing list