10

I want to use the filter() function to find the types that have an x value less than or equal to 4, OR a y value greater than 5. I think this might be a simple fix I just can't find much info on ?filter(). I almost have it I think:

x = c(1, 2, 3, 4, 5, 6)
y = c(3, 6, 1, 9, 1, 1)
type = c("cars", "bikes", "trains")

df = data.frame(x, y, type)

df2 = df %>% 
      filter(x<=4)
Frank
  • 63,401
  • 8
  • 85
  • 161
userfriendly
  • 173
  • 1
  • 2
  • 12
  • 2
    `filter(x <=4 | y > 5)` doesn't work? – MichaelChirico Mar 21 '16 at 15:08
  • 3
    also, your example data is pretty crappy, since `y > 5` is a subset of `x <= 4` – MichaelChirico Mar 21 '16 at 15:09
  • @MichaelChirico Not sure what you mean because I just started programming. I just realized I screwed up the "type" part. I didn't know what `| ` did so thank you for that insight. – userfriendly Mar 21 '16 at 15:19
  • I mean, for your data, `filter(x<=4 | y > 5)` produces the same result as `filter(x<=4)`. For an instructive working example, there should be a difference – MichaelChirico Mar 21 '16 at 15:21
  • 2
    Also see http://stackoverflow.com/q/4935479/1191259 and type `?Syntax` in the R console for a full list of basic operators. – Frank Mar 21 '16 at 15:24
  • @MichaelChirico Oh, I see what you mean now. Yeah I should've looked out for that. I'll keep that in mind next time. Thanks. – userfriendly Mar 21 '16 at 15:25

1 Answers1

12

Try

df %>%
    filter(x <=4| y>=5)
akrun
  • 674,427
  • 24
  • 381
  • 486