0

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!

R-gal
  • 11
  • 3

2 Answers2

2
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,]
Rorschach
  • 31,301
  • 5
  • 78
  • 129
  • Thank you, but it is not working. Only data for which both criteria (i.e. x<0.5 and y>2) are met should be removed - any idea about how to do that? thanks!! – R-gal May 30 '13 at 12:15
  • @R-gal please read http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example Show some code and tell us "what actually is not working" – Beasterfield May 30 '13 at 12:35
  • Thanks Beasterfield, I'm new in this, sorry. I have a dataset with 500 datapoints, where I want to remove some of them because they are obviously wrong. I have tried both suggestions, but they remove too many points, i.e. they remove ALL points where x<0.5 even though the criteria for y isn't met. Does it make sense? Will try to post the codes I used as well.. – R-gal May 30 '13 at 12:44
2

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.

Community
  • 1
  • 1
Beasterfield
  • 7,023
  • 2
  • 38
  • 47