Home > Software design >  How to use a placeholder in dplyr?
How to use a placeholder in dplyr?

Time:06-21

How can I tell dplyr that I want to use a placeholder, e.g. instead of a variable name contained in a dataset?

For example, if I want to filter in mtcars only those with 4 cylinders:

mtcars %>% filter(cyl == 4)

Now with a placeholder:

place_holder_variable <- "cyl"
mtcars %>% filter(place_holder_variable == 4)

However, now I get "<0 rows> (or 0-length row.names)" instead of the filtered list.

How do I have to proceed instead?

CodePudding user response:

Eval-parse works for this case:

place_holder <- c("cyl")
mtcars %>% filter(eval(parse(text = place_holder)) == 4)

Sorry, I'm quite new here and do not have the rep to add this as a comment.

  • Related