use column from function input for group_by variable when using dtplyr

Viewed 108

When trying to summarise columns by group using dtplyr, grouping seems to not be working. Since the group variable is an input of my function, I tried using group_by_ only to receive an error message.

Data:

df <- data.frame(
  id=c(1,1,1,2,2,2,3,3,3),
  year=c(2014, 2015, 2016, 2015, 2015, 2016, NA, NA, 2016),
  code=c(1,2,2, 1,2,3, 3,4,5),
  dv1=1:9,
  dv2=2:10
) %>% as.data.table()

cols <- c("dv1", "dv2")

> df
   id year code dv1 d2
1:  1 2014    1   1  2
2:  1 2015    2   2  3
3:  1 2016    2   3  4
4:  2 2015    1   4  5
5:  2 2015    2   5  6
6:  2 2016    3   6  7
7:  3   NA    3   7  8
8:  3   NA    4   8  9
9:  3 2016    5   9 10

Function:

for(i in seq_along(columns)) {
 sub1 <- df %>% 
   select("id", columns[i], group) %>%
   group_by(group) %>%
   summarise(mean=mean(.data[[columns[i]]], na.rm=T), sd=sd(.data[[columns[i]]], na.rm=T)) %>%
   ungroup() %>%
   as_tibble() 
 print(sub1)
}
}

test(data=df, columns=cols, group="year")

# A tibble: 1 x 3
  group  mean    sd
  <chr> <dbl> <dbl>
1 year      5  2.74
# A tibble: 1 x 3
  group  mean    sd
  <chr> <dbl> <dbl>
1 year      6  2.74
2 Answers

Here's a reprex that's similar to what you're looking for I think. It's not the all time sexiest solution, but it'll work:

library(tidyverse)
f <- function(grouping_var) {
    iris %>%
        group_by(!!sym(grouping_var)) %>%
        summarize(N = n())
}

f('Species')
#> # A tibble: 3 x 2
#>   Species        N
#> * <fct>      <int>
#> 1 setosa        50
#> 2 versicolor    50
#> 3 virginica     50

Created on 2021-03-17 by the reprex package (v1.0.0)

We can use .data

f<- function(dat, grouping_var) {
     dat %>%
         group_by(.data[[grouping_var]]) %>%
         summarise(N = n())
  }

-testing

f(iris, "Species")
# A tibble: 3 x 2
#  Species        N
#* <fct>      <int>
#1 setosa        50
#2 versicolor    50
#3 virginica     50

using the OP's example data

library(purrr)
map(cols, dat = df, .f = f)
Related