在此之前,我使用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) 在group_by_at的帮助下,该函数已被取代,应改为使用across。但是,按NULL分组会产生错误
mtcars %>% group_by(across(grouping_1)) # this works
mtcars %>% group_by(across(grouping_2)) # this gives an error对我来说,以上述方式使用的group_by_at非常有用,因为在我的函数中,我可以使用相同的代码,而无需每次检查分组参数是否为空(NULL)。
发布于 2020-08-01 17:01:51
使用all_of
library(tidyverse)
mtcars %>% group_by(across(all_of(grouping_1))) # this works
mtcars %>% group_by(across(all_of(grouping_2))) # this workshttps://stackoverflow.com/questions/62277996
复制相似问题