library(tidyverse)
library(purrr)
使用下面的示例数据,我可以创建以下函数:
Funs <- function(DF, One, Two){
One <- enquo(One)
Two <- enquo(Two)
DF %>% filter(School == (!!One) & Code == (!!Two)) %>%
group_by(Code, School) %>%
summarise(Count = sum(Question1))
}
然后我可以使用该函数过滤两个变量——学校和代码——如下所示:
Funs(DF, "School1", "B344")
这一切都很好,但是我的实际数据有很多变量,因此我不想在函数中不断输入“School”和“Code”变量,我想使用 tidyverse 和 purrr 包来循环两个列表(其中一个学校,一个用于代码)并将其输入过滤器。我希望输出是结果列表。
为简单起见,输入 dplyr::filter 的两个列表各只有两个值:School2 将使用 S300,School1 将使用 B344,就像上面的示例一样。
我试过的一些例子:
map2(c(“School2”, ”School1”),
c(“S300”, ”B344”),
function(x,y) {
DF %>% filter(School == .x & Code == .y) %>%
group_by(Code, School) %>%
summarise(Count = sum(Question1))
}
还...
map2(c("School2", "School1")),
c("S300","B344"),
~filter(School == .x & Code == .y) %>%
group_by(Code, School)%>%
summarise(Count = sum(Question1))
还有这个...
list(c("School2", "School1"), c("S300", "B344")) %>%
map2( ~ filter(School == .x & Code == .y) %>%
group_by(Code, School) %>%
summarise(Count = sum(Question1)))
这些似乎都不起作用,因此将不胜感激!
样本数据:
Code <- c("B344","B555","S300","T220","B888","B888","B555","B344","B344","T220","B555","B555","S300","B555","S300","S300","S300","S300","B344","B344","B888","B888","B888")
School <- c("School1","School1","School2","School3","School4","School4","School1","School1","School3","School3","School4","School1","School1","School3","School2","School2","School4","School2","School3","School4","School3","School1","School2")
Question1 <- c(3,4,5,4,5,5,5,4,5,3,4,5,4,5,4,3,3,3,4,5,4,3,3)
Question2 <- c(5,4,3,4,3,5,4,3,2,3,4,5,4,5,4,3,4,4,5,4,3,3,4)
DF <- data_frame(Code, School, Question1, Question2)