0

I'm curious about what would be a more succinct way to achieve this:

nyc_crashes %>% filter(
  `NUMBER OF PERSONS INJURED`     >= 1 |
  `NUMBER OF PERSONS KILLED`      >= 1 |
  `NUMBER OF PEDESTRIANS INJURED` >= 1 |
  `NUMBER OF PEDESTRIANS KILLED`  >= 1 |
  `NUMBER OF CYCLIST INJURED`     >= 1 |
  `NUMBER OF CYCLIST KILLED`      >= 1 |
  `NUMBER OF MOTORIST INJURED`    >= 1 |
  `NUMBER OF MOTORIST KILLED`     >= 1
)

Could I use string matching for "INJURED | KILLED" and not have to write a different condition for each column name?

1

1 Answer 1

2

You could do that using filter_at with ends_with.

library(dplyr)
nyc_crashes %>%
  # Select columns that end with KILLED or INJURED
  filter_at(vars(c(ends_with("KILLED"),ends_with("INJURED"))), 
            # Keep rows where any of these variables is >= 1 
                          any_vars(. >= 1))
Sign up to request clarification or add additional context in comments.

2 Comments

is there a way to do this same thing but to keep rows where variable takes on specific values? For instance, instead of just >=1, where they equal 1, 3, or 99?
You could use any_vars(. %in% c(1, 3, 99)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.