0

I have a dataframe with columns: coast, weight gain, length gain, release weight, release length. Coast is categorical ("E" or "W") and the other variables are numeric. I would like to create a dataframe where I filter by specific ranges for each coast, but I'm having a hard time figuring out the correct syntax.

For instance, I have this:

newdata<-filter(mydata, coast=="E", 
                between(weight_gain, number, number) &
                between(length_gain, number, number) & 
                between(weight_release, number, number) & 
                between(length_release, number, number))

That works fine, but it only considers coast "E" and I want to have another set of conditions for coast "W", which has slightly different numerical ranges. How do I combine the two conditions for "E" and "W" into one argument?

Thank you so much!

burphound
  • 161
  • 7
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jul 29 '21 at 16:09

1 Answers1

0

filter in dplyr can use & for AND and | for OR, so one approach if the numerical constraints vary by coast might be

newdata <- filter(mydata, 
                  (coast=="E" & 
                   between(weight_gain, number, number) &
                   between(length_gain, number, number) & 
                   between(weight_release, number, number) & 
                   between(length_release, number, number)) |
                  (coast=="W" & 
                   between(weight_gain, number, number) &
                   between(length_gain, number, number) & 
                   between(weight_release, number, number) & 
                   between(length_release, number, number)) )
Henry
  • 6,704
  • 2
  • 23
  • 39