R: how to remove specific lines in data.frame
> data = data.frame(a = c(100, -99, 322, 155, 256), b = c(23, 11, 25, 25, -999))
> data
a b
1 100 23
2 -99 11
3 322 25
4 155 25
5 256 -999
For a data.frame like this, I would like to remove any line that contains -99 or -999. Therefore my resulting datafile should only consist of lines 1, 3 and 4.
I was thinking about writing a loop for this, but I hope there will be an easier way. (If my data.frame had az columns then the loop method would be very clunky). My loop would probably look something like 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
}
+3
source to share