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

sjaffe sjaffe at riskspan.com
Mon Mar 8 16:46:02 CET 2010


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.



More information about the R-help mailing list