I am trying to pass a set of variables/values in a data.frame to a map
function, but am not sure how to deal with the fact that .x
refers to a quosure that needs to be evaluated: mutate(df2 = map2(variable, value, ~filter(df1, .x==.y)))
A naive !!.x
will not work.
Here my data.frame has one column for variable, one for value, that will be mapped in a filter call:
tibble(variable=c("wool", "tension"),
value= c("A", "L"))
#> # A tibble: 2 x 2
#> variable value
#> <chr> <chr>
#> 1 wool A
#> 2 tension L
How can I pass these to filter? Should I declare instead variable as quosure? I tried a few approaches:
library(tidyverse)
data(warpbreaks)
tibble(variable=c("wool", "tension"),
value= c("A", "L")) %>%
mutate(data_filtered=map2(variable, value, ~filter(warpbreaks, .x==.y)))
#> # A tibble: 2 x 3
#> variable value data_filtered
#> <chr> <chr> <list>
#> 1 wool A <data.frame [0 × 3]>
#> 2 tension L <data.frame [0 × 3]>
tibble(variable=c(quo(wool), quo(tension)),
value= c("A", "L")) %>%
mutate(data_filtered=map2(variable, value, ~filter(warpbreaks, eval_tidy(.x)==.y)))
#> Error in eval_tidy(.x): object 'wool' not found