group_by 通过使用整洁评估语义的字符向量
group_by by a vector of characters using tidy evaluation semantics
我曾经这样做过,使用group_by_
library(dplyr)
group_by <- c('cyl', 'vs')
mtcars %>% group_by_(.dots = group_by) %>% summarise(gear = mean(gear))
但现在 group_by_
已弃用。我不知道如何使用 tidy evaluation framework.
将字符向量转化为符号列表拼接在
df %>% group_by(!!! syms(group_by))
group_by
有 group_by_at
个变体:
library(dplyr)
group_by <- c('cyl', 'vs')
mtcars %>% group_by_at(group_by) %>% summarise(gear = mean(gear))
上面是广义的简化版本:
mtcars %>% group_by_at(vars(one_of(group_by))) %>% summarise(gear = mean(gear))
在 vars
中,您可以使用 select 变量的任何 dplyr
方式:
mtcars %>%
group_by_at(vars(
one_of(group_by) # columns from predefined set
,starts_with("a") # add ones started with a
,-hp # but omit that one
,vs # this should be always include
,contains("_gr_") # and ones with string _gr_
)) %>%
summarise(gear = mean(gear))
我曾经这样做过,使用group_by_
library(dplyr)
group_by <- c('cyl', 'vs')
mtcars %>% group_by_(.dots = group_by) %>% summarise(gear = mean(gear))
但现在 group_by_
已弃用。我不知道如何使用 tidy evaluation framework.
将字符向量转化为符号列表拼接在
df %>% group_by(!!! syms(group_by))
group_by
有 group_by_at
个变体:
library(dplyr)
group_by <- c('cyl', 'vs')
mtcars %>% group_by_at(group_by) %>% summarise(gear = mean(gear))
上面是广义的简化版本:
mtcars %>% group_by_at(vars(one_of(group_by))) %>% summarise(gear = mean(gear))
在 vars
中,您可以使用 select 变量的任何 dplyr
方式:
mtcars %>%
group_by_at(vars(
one_of(group_by) # columns from predefined set
,starts_with("a") # add ones started with a
,-hp # but omit that one
,vs # this should be always include
,contains("_gr_") # and ones with string _gr_
)) %>%
summarise(gear = mean(gear))