使用purrr迭代两个列表,然后在数据帧列表中输入dplyr :: filter

时间:2018-01-08 02:47:46

标签: r dplyr tidyverse purrr

library(tidyverse)
library(purrr)

这是问题的延续:"使用Purrr迭代两个列表然后管道进入Dplyr :: Filter"。

使用下面的示例数据,我首先创建一个数据框(wanted),其中包含我要提供给dplyr::filter的值。然后,我使用下面的代码创建结果的数据框。

map2_dfr(wanted$School, wanted$Code, ~filter(DF, School == .x, Code == .y)) %>% 
group_by(School, Code) %>% 
summarise_all(sum)

但是,我的实际数据来自三个不同的数据集,来自三个不同的时间段。对于这个例子,我刚刚制作了两个DF副本,然后将它们放入一个列表

DF2 <- DF
DF3 <- DF

DFList <- list(DF, DF2, DF3)

现在,为了处理列表中的每个数据框,我必须使用purrr:::map和类似下面的代码......

DFList %>%
   map(~filter(.x, School == "School1", Code == "B344")) %>%
   map(~group_by(.x, School, Code)) %>%
   map(~summarise(.x, Count = sum(Question1)))

这就是我被困的地方。我想结合上面的两种方法来迭代wanted,将这些值提供给dplyr::filter,但现在我必须在数据帧列表中执行此操作并输出三个数据帧的列表。

我正在努力解决类似下面代码的问题......哪些不起作用。有什么建议?使用这么多maps并不是最好的方式......

map2_dfr(Wanted$School, Wanted$Code, 
    ~DFList %>% 
        map(~filter(.x, School == .x, Code == .y) %>% 
        map(~group_by(.x, Code, School) %>% 
        map(~summarise(.x, 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)

wanted <- data_frame(School = c("School2", "School1"),
                     Code = c("S300", "B344"))

1 个答案:

答案 0 :(得分:2)

由于列表中的数据帧格式相同,只需将它们强制转换为dplyr::bind_rows的单个数据帧,通过传递.id参数保存元素名称,该参数可用于通过加入wanted进行过滤后分组:

library(tidyverse)

DF <- data_frame(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))

wanted <- data_frame(School = c("School2", "School1"),
                     Code = c("S300", "B344"))

DFList <- list(DF, DF, DF)

DFList %>% 
    bind_rows(.id = 'id') %>% 
    inner_join(wanted) %>% 
    group_by(id, School, Code) %>% 
    summarise_all(sum)
#> Joining, by = c("Code", "School")
#> # A tibble: 6 x 5
#> # Groups: id, School [?]
#>   id    School  Code  Question1 Question2
#>   <chr> <chr>   <chr>     <dbl>     <dbl>
#> 1 1     School1 B344       7.00      8.00
#> 2 1     School2 S300      15.0      14.0 
#> 3 2     School1 B344       7.00      8.00
#> 4 2     School2 S300      15.0      14.0 
#> 5 3     School1 B344       7.00      8.00
#> 6 3     School2 S300      15.0      14.0