1

As a part of a function I'm trying to create a dataframe and I want to name one of the columns after a variable. The below is some dummy data and is the part that I'm stuck on.

library(tidyverse)
graph <-  data.frame(brand = rep(c("A","B","C"), each = 3),
                    week_commencing = rep(as.Date(c('2020-03-01', '2020-04-01', '2020-05-01')), times = 3),
            sessions = sample(1000:2000,9),
            conv_rate = runif(9,0,1))

website = "A"
metric = "sessions"

graph %>% 
  filter(brand == (!!website)) %>%
  group_by(brand) %>% 
  summarise(metric = max(get(metric)),
            week_commencing = min(week_commencing),
            lab = "This Year") 

In the summarise function call I want the column name metric to be called sessions, I've tried using get(metric) and (!!metric) as part of the naming of the column but it doesn't work.

Is this even possible to do in R? Any help would be appreciated.

FluffySheep1990
  • 381
  • 3
  • 16
  • Even inside a function, I don't think you need tidyeval for the filter. You can just do: `filter(brand == website)` – eipi10 Jun 28 '20 at 18:12
  • this question has answers that show how to change column name(s) in dataframe in base R that can use variables https://stackoverflow.com/q/6081439/59470 – topchef Jun 28 '20 at 18:14

3 Answers3

0

Do you mean something like this?

library(tidyverse)
graph <-  data.frame(brand = rep(c("A","B","C"), each = 3),
                     week_commencing = rep(as.Date(c('2020-03-01', '2020-04-01', '2020-05-01')), times = 3),
                     sessions = sample(1000:2000,9),
                     conv_rate = runif(9,0,1))

website = "A"

metric = "sessions"

graph %>% 
  filter(brand == (!!website)) %>%
  group_by(brand) %>% 
  summarise(metric = max(get(metric)),
            week_commencing = min(week_commencing),
            lab = "This Year") %>% rename(sessions=metric)

# A tibble: 1 x 4
  brand sessions week_commencing lab      
  <fct>    <int> <date>          <chr>    
1 A         1819 2020-03-01      This Year
Duck
  • 39,058
  • 13
  • 42
  • 84
0

If you wish to use a variable as a name on the left hand side of a dplyr function, (without having to separately rename the column), you can use !!variable := instead of variable =, so in your case it would be:

graph %>% 
  filter(brand == (!!website)) %>%
  group_by(brand) %>% 
  summarise(!!metric := max(get(metric)),
            week_commencing = min(week_commencing),
            lab = "This Year") 
#> # A tibble: 1 x 4
#>   brand sessions week_commencing lab      
#>  <chr>    <int> <date>          <chr>    
#> 1 A         1901 2020-03-01      This Year
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
0

We can also do this with

library(dplyr)   
graph %>%
   filter(brand == website) %>%
   group_by(brand) %>% 
   summarise(metric = max(!! rlang::sym(metric)),
             week_commencing = min(week_commencing),
             lab = "This Year") %>% rename_at(vars(metric), ~ 'sessions')
# A tibble: 1 x 4
  brand sessions week_commencing lab      
  <chr>    <int> <date>          <chr>    
1 A         1555 2020-03-01      This Year
akrun
  • 874,273
  • 37
  • 540
  • 662