如何在if-then-else等条件下使用group_by并应用dplyr理念
我需要根据条件按变量x或变量y分组。当我使用magrittr管道时,不会发生这种情况 考虑数据帧df1:如何在if-then-else等条件下使用group_by并应用dplyr理念,r,dplyr,R,Dplyr,我需要根据条件按变量x或变量y分组。当我使用magrittr管道时,不会发生这种情况 考虑数据帧df1: > df1 seat_id student_id seat_state 1 1222 500 9 2 850 500 9 3 850 500 9 4 1225 500 9 5 16502 500
> df1
seat_id student_id seat_state
1 1222 500 9
2 850 500 9
3 850 500 9
4 1225 500 9
5 16502 500 9
6 17792 500 9
7 17792 500 9
8 1219 501 10
9 847 501 9
10 847 501 9
11 1220 501 9
12 17785 501 9
13 17785 501 9
14 1214 502 9
15 842 502 9
16 842 502 9
17 1215 502 9
18 1211 503 9
19 839 503 9
20 839 503 9
现在假设我想用两种方式来总结这一点
1.按学生证或
2.按所在国分列
取决于一个变量
摘要
老路漫漫
if(summary==1)df1%%>%groupby(student\u id)%%>%summary(seats=n())否则if(summary==2)df1%%>%groupby(seats\u state)%%>%summary(seats=n())
但是必须有一种更紧凑的方式,特别是因为我在summary语句后面有几个magrittr管道,因此会将代码的大小增加一倍。my_col%groupby([,my_col])%>%summary(seats=n())
my_col <- 1 # the column number
df1 %>% group_by(.[,my_col]) %>% summarise(seats=n())
在最新版本的dplyr
(0.7.1
)中。我们可以使用quo
和unquote(!!
)来传递分组变量。下面是使用dplyr
中的quo
的函数示例。您可以键入vignette(“编程”)
了解更多信息
# Load package
library(dplyr)
# Create a function
# This function has two arguments. The first one is the data frame
# The second one use to specify condition: 1 means group the student_id,
# while 2 means group the seat_state
my_summary <- function(df1, condition){
if (condition == 1){
group_var <- quo(student_id)
} else if (condition == 2){
group_var <- quo(seat_state)
}
df1 %>%
group_by(!!group_var) %>%
summarise(seats=n())
}
# Test the function
my_summary(df1, 1)
# A tibble: 4 x 2
student_id seats
<int> <int>
1 500 7
2 501 6
3 502 4
4 503 3
my_summary(df1, 2)
# A tibble: 2 x 2
seat_state seats
<int> <int>
1 9 19
2 10 1
#加载包
图书馆(dplyr)
#创建一个函数
#此函数有两个参数。第一个是数据帧
#第二个用于指定条件:1表示将学生id分组,
#而2表示组别状态
my_summary我们可以通过对quos的列表进行子集设置来替换if/else
f1 <- function(df, cond) {
grp <- quos(student_id, seat_state)[[cond]]
df %>%
group_by(UQ(grp)) %>%
summarise(seats = n())
}
f1(df1, 1)
# A tibble: 4 x 2
# student_id seats
# <int> <int>
#1 500 7
#2 501 6
#3 502 4
#4 503 3
f1(df1, 2)
# A tibble: 2 x 2
# seat_state seats
# <int> <int>
#1 9 19
#2 10 1
f1%
总结(座位=n()
}
f1(df1,1)
#一个tibble:4x2
#学生证座位
#
#1 500 7
#2 501 6
#3 502 4
#4 503 3
f1(df1,2)
#一个tibble:2x2
#国家席位
#
#1 9 19
#2 10 1
我想你可以加上'my\u col