I would like to use purr::pmap()
to hand over column names as parameters to a function using those names in dplyr
functions. Example:
library(purrr)
library(dplyr)
tib <- tibble(A1 = 0:9, A2 = 10:19, A3 = 20:29,
B1 = 30:39, B2 = 40:49, B3 = 50:59,
C1 = 60:69, C2 = 70:79, C3 = 80:89)
select_three <- function(data, first, second, third) {
result = data %>%
select({{first}}, {{second}}, {{third}})
result
}
This works:
single_test <- select_three(tib, A1, A2, A3)
print(single_test)
# A tibble: 10 x 3
A1 A2 A3
<int> <int> <int>
1 0 10 20
2 1 11 21
3 2 12 22
4 3 13 23
5 4 14 24
6 5 15 25
7 6 16 26
8 7 17 27
9 8 18 28
10 9 19 29
This doesn't:
params <- list(tib,
c(A1, B1, C1),
c(A2, B2, C2),
c(A3, B3, C3))
pmap_test <- pmap_dfr(params, select_three)
Error: Element 1 of `.l` must have length 1 or 30, not 9
I would suggest to pass character vectors instead.
Note that
select
works with character arguments so usingdata %>% select(first, second, third)
would work here but if you are going to use this for something else other thanselect
general way would be to use.data
pronoun.