r - dbplyr 按动态变量名称分组

标签 r dplyr dbplyr

如何使用动态变量名称进行分组?

我的示例:尝试对 Species 列进行分组,知道它位于 grouping_variable var 中?

library(dplyr)
library(dbplyr)
library(DBI)

# My table
iris_table <- tbl(src = my_db_conn, in_schema(schema = "my_schema", table = "iris_table"))

# The grouping variable
grouping_variable <- "Species"

# My tries
iris_table %>%
  group_by(across(any_of(grouping_variable))) %>%
  summarise(sum_petal_length = sum(Petal.Length))
### ==> returns error

iris_table %>%
  group_by(!!!grouping_variable) %>%
  summarise(sum_petal_length = sum(Petal.Length))
### ==> returns grouping by the character "Species"

我的sessionInfo():

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] DBI_1.1.0               dbplyr_1.4.4            dplyr_1.0.0             lubridate_1.7.9        

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.5          rstudioapi_0.11     magrittr_1.5        hms_0.5.3           odbc_1.3.0          tidyselect_1.1.0   
 [7] bit_1.1-15.2        R6_2.4.1            rlang_0.4.7         fansi_0.4.1         blob_1.2.1          tools_3.6.2        
[13] utf8_1.1.4          cli_2.0.2           ellipsis_0.3.1      readxl_1.3.1        bit64_0.9-7.1       assertthat_0.2.1   
[19] tibble_3.0.3        lifecycle_0.2.0     crayon_1.3.4        zip_2.0.4           purrr_0.3.4         tidyr_1.1.0        
[25] vctrs_0.3.2         glue_1.4.1          openxlsx_4.1.5      stringi_1.4.6       cellranger_1.1.0    compiler_3.6.2     
[31] pillar_1.4.6        generics_0.0.2      pkgconfig_2.0.3    

最佳答案

这里有两种可能的方法。

(1) 与您已经使用的方法最相似,我们首先必须告诉 R 字符串应被视为符号:

iris_table %>%
  group_by(!!!syms(grouping_variable)) %>%
  summarise(sum_petal_length = sum(Petal.Length))

注意 !!! 之前的 syms。此方法使用 rlang 包的一些功能,这些功能在其他上下文中可能有用。但是,它不再是使用 dplyr 进行编程的推荐方法。

(2) 执行此类操作的推荐方法 programming with dplyr是:

iris_table %>%
  group_by(.data[[grouping_variable]]) %>%
  summarise(sum_petal_length = sum(Petal.Length))

在使用 dbplyr 时,这两种方法都会为您提供正确的 SQL 转换:

data(iris)
iris_table = tbl_lazy(iris, con = simulate_mssql())
# The grouping variable
grouping_variable <- "Species"

# approach 1
iris_table %>%
  group_by(!!!syms(grouping_variable)) %>%
  summarise(sum_petal_length = sum(Petal.Length))
# translation from approach 1
# <SQL>
# SELECT `Species`, SUM(`Petal.Length`) AS `sum_petal_length`
# FROM `df`
# GROUP BY `Species`

# approach 2
iris_table %>%
  group_by(.data[[grouping_variable]]) %>%
  summarise(sum_petal_length = sum(Petal.Length))
# translation from approach 2
# <SQL>
# SELECT `Species`, SUM(`Petal.Length`) AS `sum_petal_length`
# FROM `df`
# GROUP BY `Species`

关于r - dbplyr 按动态变量名称分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67076958/

相关文章:

r - 从 R 中的时间戳获取年份

r - 环境和惰性评估 : getting rid of symbols and getting values

r - 提取模式之间的多个字符串 block

r - 如何从另一个 df 的值子集创建一个新的 df?

r - 在 SQL Server 中组合 dbplyr 和 case_when

r - 使用 dbplyr 进行数据库计算

r - 在嵌套数据框列上使用 mutate_at() 生成多个非嵌套列

r - 根据存在的数据帧行分配分组变量R

r - 根据多个条件筛选和提取行

r - dplyr 筛选具有大量匹配项的数据库表