Home > Enterprise >  How to access a column name in a user defined function with dplyr?
How to access a column name in a user defined function with dplyr?

Time:10-12

Here is my example df:

samp_df <- tibble(var1 = c('a', 'b', 'c'),
              var_in_df = c(3,7,9))

And here is an example of my function:

calculateMean <- function(df, variable){

     df <- df %>% 
     mutate(mean_of_var = mean({{variable}}))

}

df_result <- calculateMean(samp_df, var_in_df)

I want to be able to dynamically use the name of the column in the data frame when naming the new variable that is being created in the function and added to the data frame. For example, in the dataframe, the variable is titled var_in_df, and so in this example I would like my function to create a variable called "mean_of_var_in_df", I thought I could use a paste function, like this:

calculateMean <- function(df, variable){

  df <- df %>% 
    mutate(paste('mean_of', {{variable}}, sep = '-') = mean({{variable}}))

}

But that doesn't work. Does anyone know how to do this? I also need to stick to the dplyr syntax, even though it might be easier with base R.

Thanks!

CodePudding user response:

Using the special assignment operator := and making use of glue syntax you could do:

samp_df <- tibble::tibble(var1 = c('a', 'b', 'c'),
                  var_in_df = c(3,7,9))

library(dplyr)

calculateMean <- function(df, variable) {
  df %>%
    mutate("mean_of-{{variable}}" := mean({{ variable }}))
}

calculateMean(samp_df, var_in_df)
#> # A tibble: 3 × 3
#>   var1  var_in_df `mean_of-var_in_df`
#>   <chr>     <dbl>               <dbl>
#> 1 a             3                6.33
#> 2 b             7                6.33
#> 3 c             9                6.33
  • Related