subset - R: how to remove certain rows in data.frame -


> data = data.frame(a = c(100, -99, 322, 155, 256), b = c(23, 11, 25, 25, -999)) > data        b 1 100   23 2 -99   11 3 322   25 4 155   25 5 256 -999 

for such data.frame remove row contains -99 or -999. resulting data.frame should consist of rows 1, 3, , 4.

i thinking of writing loop this, hoping there's easier way. (if data.frame have columns a-z, loop method clunky). loop this

i = 1 for(i in 1:nrow(data)){   if(data$a[i] < 0){     data = data[-i,]   }else if(data$b[i] < 0){     data = data[-i,]   }else data = data } 

maybe this:

ind <- reduce(`|`,lapply(data,function(x) x %in% c(-99,-999))) > data[!ind,]      b 1 100 23 3 322 25 4 155 25 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -