OR operator in filter()?

43,452

Try

df %>%
    filter(x <=4| y>=5)
Share:
43,452
userfriendly
Author by

userfriendly

Updated on March 22, 2020

Comments

  • userfriendly
    userfriendly about 4 years

    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)
    
    • MichaelChirico
      MichaelChirico about 8 years
      filter(x <=4 | y > 5) doesn't work?
    • MichaelChirico
      MichaelChirico about 8 years
      also, your example data is pretty crappy, since y > 5 is a subset of x <= 4
    • userfriendly
      userfriendly about 8 years
      @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.
    • MichaelChirico
      MichaelChirico about 8 years
      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
    • Frank
      Frank about 8 years
      Also see stackoverflow.com/q/4935479/1191259 and type ?Syntax in the R console for a full list of basic operators.
    • userfriendly
      userfriendly about 8 years
      @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.