library(tidyverse)
(df <- tibble(
id = 1:3,
wut = letters[3:1],
x1 = 1:3,
x2 = 4:6,
x3 = 7:9
))
#> # A tibble: 3 x 5
#> id wut x1 x2 x3
#> <int> <chr> <int> <int> <int>
#> 1 1 c 1 4 7
#> 2 2 b 2 5 8
#> 3 3 a 3 6 9
col_prod <- function(...) {
args <- list(...)
Reduce(`*`, args)
}
df %>%
mutate(y = pmap_dbl(select(., starts_with("x")), col_prod))
#> # A tibble: 3 x 6
#> id wut x1 x2 x3 y
#> <int> <chr> <int> <int> <int> <dbl>
#> 1 1 c 1 4 7 28
#> 2 2 b 2 5 8 80
#> 3 3 a 3 6 9 162
df %>%
mutate(y = pmap_dbl(across(starts_with("x")), col_prod))
#> # A tibble: 3 x 6
#> id wut x1 x2 x3 y
#> <int> <chr> <int> <int> <int> <dbl>
#> 1 1 c 1 4 7 28
#> 2 2 b 2 5 8 80
#> 3 3 a 3 6 9 162
Created on 2021-02-24 by the reprex package (v1.0.0.9002)
Wouldn't it be safer to use across(...) instead if select (.,...)?