I want to delete datapoints if their value are above a certain value but below another, but I simply cannot figure out how to do it in R.
I want to remove data points if x<0.5 and y>2, but both criteria needs to be met.
Thanks in advance!
dat <- data.frame(x=runif(100,0,2), y=runif(100,1,4))
todrop <- which(dat$x <0.5 & dat$y>2)
dat <- dat[-todrop,]
If you want to use this programmatically, the use of subset
should be avoided (see Why is `[` better than `subset`? for details).
Instead, you could go with the data.frame
syntax:
dat[ dat$x >= 0.5 & dat$y <= 2, ]
Reading of ? "[.data.frame"
is an absolute must to any R beginner.