Search code examples
rfunctiondplyr

pivot_wider within a function R


I am working in R and I have the following data.

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"))
)

data:

id name month
1 jane april
2 jane april
1 lauran may
2 lauran june
1 james june
2 james june

I am using a function to pivot it. This is because I have many different input tables and the value I want to be pivoted are called something different in each of my tables. (in this example, the measure is called "month")

pivot_data <- function(input_data,
                       measure_1){

output <- input_data %>%
          arrange(name, id) %>%
          pivot_wider(names_from = id,
                      names_prefix =  paste0({{measure_1}}, "_"),  # not correct
                      values_from = {{measure_1}})   # not correct
}

Now I run the function:

Not working: 

required_output <- pivot_data(input_data = data,
                              measure_1 = month)

Error in paste0({ : 
  cannot coerce type 'closure' to vector of type 'character'

I think it is something to do with {{}} or !! or sym? in the names_prefix and values_from

Required output:

name month_1 month_2
james june june
jane april april
lauran may june

Solution

  • 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