OR operator in filter()?
43,452
Try
df %>%
filter(x <=4| y>=5)
Author by
userfriendly
Updated on March 22, 2020Comments
-
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 about 8 years
filter(x <=4 | y > 5)
doesn't work? -
MichaelChirico about 8 yearsalso, your example data is pretty crappy, since
y > 5
is a subset ofx <= 4
-
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 about 8 yearsI mean, for your data,
filter(x<=4 | y > 5)
produces the same result asfilter(x<=4)
. For an instructive working example, there should be a difference -
Frank about 8 yearsAlso see stackoverflow.com/q/4935479/1191259 and type
?Syntax
in the R console for a full list of basic operators. -
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.
-