我可以在dplyr group_by中使用条件语句来基于另一个变量更改要分组的字段。这是一个starwars
的简单示例:
library(dplyr)
my_val <- "Droid"
starwars %>%
group_by(if(my_val == "Human") {eye_color}
else if (my_val == "Droid") {homeworld}) %>%
summarize(mass = sum(mass))
然而,resuls的列名称是条件语句的字符串:if (my_val == "Human") {\n eye_color\n} else if (my_val == "Droid") {\n homeworld\n}
我可以在条件之前输入一列名称:
group_by(Character_Type = if(my_val == "Human") {eye_color}
else if (my_val == "Droid") {homeworld}) %>%
但是我希望列名与条件语句中选择的列匹配,在此示例中为eye_color
或homeworld
。
如何将列名设置为与在条件语句中选择的字段相同?我可以将条件语句从dplyr语句中拉出,但是将其保留在内部会更简单。
答案 0 :(得分:2)
喜欢吗?
library(dplyr)
my_val <- "Droid"
starwars %>%
group_by(!!rlang::sym(ifelse(my_val == "Human","eye_color","homeworld"))) %>%
summarize(mass = sum(mass))
# A tibble: 49 x 2
homeworld mass
<chr> <dbl>
1 Alderaan NA
2 Aleen Minor 15
3 Bespin 79
4 Bestine IV 110
5 Cato Neimoidia 90
6 Cerea 82
7 Champala NA
8 Chandrila NA
9 Concord Dawn 79
10 Corellia 157
# ... with 39 more rows
或
starwars %>%
group_by(!!rlang::sym(case_when(my_val=="Droid"~"homeworld",
my_val=="Human"~"eye_color"))) %>%
summarize(mass = sum(mass))
答案 1 :(得分:0)
我们可以使用group_by_at
,它可以将字符串作为输入
library(dplyr)
starwars %>%
group_by_at(if_else(my_val == "Human", "eye_color", "homeworld")) %>%
summarise(mass = sum(mass))
# A tibble: 49 x 2
# homeworld mass
# <chr> <dbl>
# 1 Alderaan NA
# 2 Aleen Minor 15
# 3 Bespin 79
# 4 Bestine IV 110
# 5 Cato Neimoidia 90
# 6 Cerea 82
# 7 Champala NA
# 8 Chandrila NA
# 9 Concord Dawn 79
#10 Corellia 157
# ... with 39 more rows