Home > Software design >  Using mean in dplyr chain with curly braces always returns NA
Using mean in dplyr chain with curly braces always returns NA

Time:12-13

Trying to create a simple function that summarizes a variable of choice via a dplyr chain. Here's my attempt:

get_mutated_df <- function(data, outcome){
  {{data}} %>% group_by(speed) %>% 
  summarize(dist_mean = mean({{outcome}}, na.rm = T)) %>%
  print()
}

data(cars)
get_mutated_df(cars, "dist")

However, this returns a tibble of NAs:

# A tibble: 19 × 2
   speed dist_mean
   <dbl>     <dbl>
 1     4        NA
 2     7        NA
 3     8        NA
 4     9        NA

What's the appropriate way of doing this?

CodePudding user response:

No {} at data, and remove "" to dist will works.

get_mutated_df <- function(data, outcome){
  data %>% group_by(speed) %>% 
    summarize(dist_mean = mean({{outcome}}, na.rm = T)) %>%
    print()
}

get_mutated_df(cars, dist)

   speed dist_mean
   <dbl>     <dbl>
 1     4       6  
 2     7      13  
 3     8      16  
 4     9      10  
 5    10      26  
 6    11      22.5
 7    12      21.5
 8    13      35  
 9    14      50.5
10    15      33.3
11    16      36  
12    17      40.7
13    18      64.5
14    19      50  
15    20      50.4
16    22      66  
17    23      54  
18    24      93.8
19    25      85 

Code for pre_ thing

carss <- cars
carss$pre_dist <- cars$dist

get_mutated_df_2 <- function(data, outcome){
  outcome <- deparse(substitute(outcome))
  outcome <- paste0("pre_", outcome)
  outcome <- as.symbol(outcome)
  data %>% group_by(speed) %>% 
    summarize(dist_mean := mean({{outcome}}, na.rm = T)) %>%
    print()
}
get_mutated_df_2(carss, dist)

   speed dist_mean
   <dbl>     <dbl>
 1     4       6  
 2     7      13  
 3     8      16  
 4     9      10  
 5    10      26  
 6    11      22.5
 7    12      21.5
 8    13      35  
 9    14      50.5
10    15      33.3
11    16      36  
12    17      40.7
13    18      64.5
14    19      50  
15    20      50.4
16    22      66  
17    23      54  
18    24      93.8
19    25      85  
  • Related