AuriDUL
2010-Mar-08 15:26 UTC
[R] [help] deleting rows which contain more than 2 NAs or zeros
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 all the basics yet as well. Thanks in advance. -- View this message in context: http://n4.nabble.com/help-deleting-rows-which-contain-more-than-2-NAs-or-zeros-tp1584613p1584613.html Sent from the R help mailing list archive at Nabble.com.
sjaffe
2010-Mar-08 15:46 UTC
[R] [help] deleting rows which contain more than 2 NAs or zeros
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, na.rm=T ) <= 2 ##true for rows with 2 or fewer 0's i1 & i2 ##logical vector, true for rows satisfying both conditions d[ i1 & i2, ] ##only those rows satisfying the condition, and all columns -- View this message in context: http://n4.nabble.com/help-deleting-rows-which-contain-more-than-2-NAs-or-zeros-tp1584613p1584641.html Sent from the R help mailing list archive at Nabble.com.