使用跨替换 group_by_at(NULL)

dan*_*res 6 r dplyr tidyverse rlang

以前,我曾经group_by_at按字符串向量或按 NULL 分组:

library(tidyverse)

grouping_1 <- c("cyl", "vs")
grouping_2 <- NULL

mtcars %>% group_by_at(grouping_1) 
mtcars %>% group_by_at(grouping_2) 
Run Code Online (Sandbox Code Playgroud)

的帮助group_by_at表明该函数已被取代,across应改为使用该函数。但是,按 NULL 分组会出现错误

mtcars %>% group_by(across(grouping_1)) # this works
mtcars %>% group_by(across(grouping_2)) # this gives an error
Run Code Online (Sandbox Code Playgroud)

对我来说,group_by_at以所描述的方式使用很有用,因为在我的函数中,我可以使用相同的代码,而无需每次都检查分组参数是否为空 (NULL)。

Tim*_*Fan 2

symsuse将字符串拼接成group_byusing还是可以的!!!

\n\n
library(tidyverse)\n\ngrouping_1 <- c("cyl", "vs")\ngrouping_2 <- NULL\n\nsym_gr_1 <- syms(grouping_1)\nsym_gr_2 <- syms(grouping_2)\n\nmtcars %>% group_by(!!! sym_gr_1) # this works\n\n#> # A tibble: 32 x 11\n#> # Groups:   cyl, vs [5]\n#>      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb\n#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>\n#>  1  21       6  160    110  3.9   2.62  16.5     0     1     4     4\n#>  2  21       6  160    110  3.9   2.88  17.0     0     1     4     4\n#>  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1\n#>  4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1\n#>  5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2\n#>  6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1\n#>  7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4\n#>  8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2\n#>  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2\n#> 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4\n#> # \xe2\x80\xa6 with 22 more rows\n\n\nmtcars %>% group_by(!!! sym_gr_2) # this works\n\n#> # A tibble: 32 x 11\n#>      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb\n#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>\n#>  1  21       6  160    110  3.9   2.62  16.5     0     1     4     4\n#>  2  21       6  160    110  3.9   2.88  17.0     0     1     4     4\n#>  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1\n#>  4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1\n#>  5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2\n#>  6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1\n#>  7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4\n#>  8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2\n#>  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2\n#> 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4\n#> # \xe2\x80\xa6 with 22 more rows\n
Run Code Online (Sandbox Code Playgroud)\n

由reprex 包(v0.3.0)于 2020-06-20 创建

\n
\n

使用dplyr::across()另一个选项(在下面答案中发布的官方方法之上all_of)是将包含变量名称的字符串包装在c(). 当对象为 NULL 时,这甚至可以工作。不过结果会附注,提醒使用者更好地使用all_of

\n\n
grouping_1 <- c("cyl", "vs")\ngrouping_2 <- NULL\n\nmtcars %>% group_by(across(c(grouping_1))) \n\n#> Note: Using an external vector in selections is ambiguous.\n#> \xe2\x84\xb9 Use `all_of(grouping_1)` instead of `grouping_1` to silence this message.\n#> \xe2\x84\xb9 See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>.\n#> This message is displayed once per session.\n\n#> # A tibble: 32 x 11\n#> # Groups:   cyl, vs [5]\n#>      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb\n#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>\n#>  1  21       6  160    110  3.9   2.62  16.5     0     1     4     4\n#>  2  21       6  160    110  3.9   2.88  17.0     0     1     4     4\n#>  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1\n#>  4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1\n#>  5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2\n#>  6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1\n#>  7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4\n#>  8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2\n#>  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2\n#> 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4\n#> # \xe2\x80\xa6 with 22 more rows\nmtcars %>% group_by(across(c(grouping_2))) \n\n#> Note: Using an external vector in selections is ambiguous.\n#> \xe2\x84\xb9 Use `all_of(grouping_2)` instead of `grouping_2` to silence this message.\n#> \xe2\x84\xb9 See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>.\n#> This message is displayed once per session.\n\n#> # A tibble: 32 x 11\n#>      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb\n#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>\n#>  1  21       6  160    110  3.9   2.62  16.5     0     1     4     4\n#>  2  21       6  160    110  3.9   2.88  17.0     0     1     4     4\n#>  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1\n#>  4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1\n#>  5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2\n#>  6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1\n#>  7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4\n#>  8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2\n#>  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2\n#> 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4\n#> # \xe2\x80\xa6 with 22 more rows\n
Run Code Online (Sandbox Code Playgroud)\n

由reprex 包(v0.3.0)于 2021-05-30 创建

\n