[R] [help] deleting rows which contain more than 2 NAs or zeros

2010-03-08 Thread AuriDUL
Hello. I have just started learning how to work with R program but I have encountered a problem. I can't think up how to remove the rows which contain two (2) or more NA or Zero (0). I would be glad if you could help me because I just have some basic knowledge so far and I even haven't mastered

Re: [R] [help] deleting rows which contain more than 2 NAs or zeros

2010-03-08 Thread sjaffe
If the data is a dataframe or matrix 'd': d <- d[apply(d, 1, function(v) sum( is.na(v) ) <= 2 & sum(v==0, na.rm=T) <= 2 ), ] which can be deconstructed as follows: i1 <- apply(d, 1, function(v) sum(is.na(v)) <= 2 ) ## true for rows with 2 or fewer na's i2 <- apply(d, 1, function(v) sum( v == 0,