Thanks to @Ihora for the help. The names_glue
argument seems a bit more appropriate to me (instead of names_prefix
) in this situation. It no longer becomes necessary to use englue()
or ensym()
, since .value
is use: names_glue = "{.value}_{id}"
.
library(tidyverse)
data <- data.frame(
id = c(1, 2, 1, 2, 1, 2),
name = c("jane", "jane", "lauran", "lauran", "james", "james"),
month = c("april", "april", "may", "june", "june","june"))
pivot_data <- function (input_data, measure_1) {
input_data %>%
arrange(name, id) %>%
pivot_wider(
names_from = id,
names_glue = "{.value}_{id}",
values_from = {{measure_1}}
)
}
pivot_data(input_data = data,
measure_1 = month)
#> # A tibble: 3 × 3
#> name month_1 month_2
#> <chr> <chr> <chr>
#> 1 james june june
#> 2 jane april april
#> 3 lauran may june
Created on 2025-01-07 with reprex v2.1.0