0
votes

I have a dataset where I want to simply use an alias name for a column name and apply that to a dplyr group_by/summarise function.

Here is an example.

alias = 'cust'
alias2 = 'class' 

df <- data_frame(cust = c("A", "B", "A", "C", "B"), class = c(1, 2, 3, 4, 7))
df
       cust     class
      <chr>     <dbl>
    1 A         1
    2 B         2
    3 A         3
    4 C         4
    5 B         7

Here is the original dplyr function:

df %>%
dplyr::group_by(cust) %>%
dplyr::summarise(test = max(class) )

Result:

  cust   test
  <chr> <dbl>
1 A         3
2 B         7
3 C         4

Here is my attempt with the alias name:

df %>%
dplyr::group_by(!!alias) %>%
dplyr::summarise(test = max(!!alias2) )

  `"cust"` test 
  <chr>    <chr>
1 cust     class

How do I run this code with an alias? Any help would be great thanks!

2

2 Answers

1
votes

You need to convert alias and alias2 to column name symbols first:

library(dplyr); library(rlang);

df %>%
    group_by(!!sym(alias)) %>%
    summarise(test = max(!!sym(alias2)))

# A tibble: 3 x 2
#  cust   test
#  <chr> <dbl>
#1 A         3
#2 B         7
#3 C         4

group_by also has a group_by_at variant you can use directly with character vectors:

df %>%
    group_by_at(alias) %>%
    summarise(test = max(!!sym(alias2)))
0
votes

You can also put the aliases inside quosures earlier up. use quo for the script version and enquo if you put it inside a function:

library(tidyverse)
alias = quo(cust)
alias2 = quo(class)

df <- data_frame(cust = c("A", "B", "A", "C", "B"), class = c(1, 2, 3, 4, 7))
df %>%
  group_by(!!alias) %>%
  summarise(test = max(!!alias2))
#> # A tibble: 3 x 2
#>   cust   test
#>   <chr> <dbl>
#> 1 A         3
#> 2 B         7
#> 3 C         4

max_summary <- function(df, group_col, max_col) {
  alias <- enquo(group_col)
  alias2 <- enquo(max_col)
  df %>%
    group_by(!!alias) %>%
    summarise(test = max(!!alias2))
}
max_summary(df, cust, class)
#> # A tibble: 3 x 2
#>   cust   test
#>   <chr> <dbl>
#> 1 A         3
#> 2 B         7
#> 3 C         4

Created on 2018-07-31 by the reprex package (v0.2.0).