79336747

Date: 2025-01-07 16:56:40
Score: 1
Natty:
Report link

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

Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @Ihora
  • Low reputation (0.5):
Posted by: VinceGreg