为 group_by 动态选择多列

当存在多个分组变量时,group_by 的数据屏蔽不起作用。

下面粘贴代码

grpByCols <- "model"

mpg%>%
  group_by(.data[[grpByCols]])

grpByCols <- c("model", "manufacturer")

mpg%>%
  group_by(.data[[grpByCols]])

第一个 group_by 有效,第二个失败。

粘贴下面的运行输出

> grpByCols <- "model"
> 
> mpg%>%
+   group_by(.data[[grpByCols]])
# A tibble: 234 x 11
# Groups:   model [38]
   manufacturer model      displ  year   cyl trans      drv     cty   hwy fl    class  
   <chr>        <chr>      <dbl> <int> <int> <chr>      <chr> <int> <int> <chr> <chr>  
 1 audi         a4           1.8  1999     4 auto(l5)   f        18    29 p     compact
 2 audi         a4           1.8  1999     4 manual(m5) f        21    29 p     compact
 3 audi         a4           2    2008     4 manual(m6) f        20    31 p     compact
 4 audi         a4           2    2008     4 auto(av)   f        21    30 p     compact
 5 audi         a4           2.8  1999     6 auto(l5)   f        16    26 p     compact
 6 audi         a4           2.8  1999     6 manual(m5) f        18    26 p     compact
 7 audi         a4           3.1  2008     6 auto(av)   f        18    27 p     compact
 8 audi         a4 quattro   1.8  1999     4 manual(m5) 4        18    26 p     compact
 9 audi         a4 quattro   1.8  1999     4 auto(l5)   4        16    25 p     compact
10 audi         a4 quattro   2    2008     4 manual(m6) 4        20    28 p     compact
# … with 224 more rows
> 
> grpByCols <- c("model", "manufacturer")
> 
> mpg%>%
+   group_by(.data[[grpByCols]])
Error: Problem with `mutate()` input `..1`.
x Must subset the data pronoun with a string.
? Input `..1` is `<unknown>`.
Run `rlang::last_error()` to see where the error occurred.
> 

如果您有任何想法来完成这项工作,请告诉我

回答

一个简单的方法是使用across()函数 from dplyr

mpg %>% group_by(across(all_of(grpByCols)))
# A tibble: 234 × 11
# Groups:   model, manufacturer [38]
manufacturer model      displ  year   cyl trans drv     cty   hwy fl    class
<chr>        <chr>      <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
1 audi         a4           1.8  1999     4 auto… f        18    29 p     comp…
2 audi         a4           1.8  1999     4 manu… f        21    29 p     comp…
3 audi         a4           2    2008     4 manu… f        20    31 p     comp…
4 audi         a4           2    2008     4 auto… f        21    30 p     comp…
5 audi         a4           2.8  1999     6 auto… f        16    26 p     comp…
6 audi         a4           2.8  1999     6 manu… f        18    26 p     comp…
7 audi         a4           3.1  2008     6 auto… f        18    27 p     comp…
8 audi         a4 quattro   1.8  1999     4 manu… 4        18    26 p     comp…
9 audi         a4 quattro   1.8  1999     4 auto… 4        16    25 p     comp…
10 audi         a4 quattro   2    2008     4 manu… 4        20    28 p     comp…
  • 这应该是`across(all_of(grpByCols))`。您没有看到有关直接传递不明确字符向量的警告?

以上是为 group_by 动态选择多列的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>