dplyr使用purrr :: map在小标题列表中计数单个观察值

时间:2018-08-09 22:14:35

标签: r dplyr purrr

我试图计算包含单个观测值的小标题列表中出现的频率,这些分隔符之间用“;”分隔。在purrr::map()中使用purrr::map()时遇到错误。我怀疑我缺少一些简单的东西,因此不胜感激。

以输入不同客户的水果为例,其中同时购买的水果之间用“;”分隔。

# Fruit purchases across days with different number of customers.
day_1 <- as_data_frame(setNames(list(c("oranges;peaches;apples", "pears;apples", "bananas", "oranges;apples", "apples")), "fruits"))
day_2 <- as_data_frame(setNames(list(c("oranges;apples", "peaches","apples;bananas;", "pears", "apples;peaches", "oranges")), "fruits"))
day_3 <- as_data_frame(setNames(list(c("peaches;pears","apples","bananas")), "fruits"))

# Create list of fruit purchases.
fruit_list <- list(day_1, day_2, day_3)

这将返回三个小标题的列表,这是我的数据的一般格式。我可以使用dplyr / purrr来计算每天每种水果的总观察次数:

fruit_list %>% 
  map(function(x) strsplit(x$fruits, ";")) %>% 
  map(unlist) %>% 
  map(table)

但是,当我尝试在map()中使用map()来隔离和汇总所有小菜一碟中的单个水果时,我遇到了错误

  

“错误:.x不是向量(关闭)”

fruit_list %>% 
  map(mutate(fruit_count = map(function(x) strsplit(x$fruits, ";"), length))) %>% 
  filter(fruit_count==1) %>% 
  count(solo_fruits = fruits) 

我可以对单个小标题/ df执行此功能,但不能对整个小标题列表执行此功能。我是否缺少使用map()函数的东西或更明显的东西?谢谢!

第一个小节所需的结果格式:

# A tibble: 2 x 2
  solo_fruits     n
  <chr>       <int>
1 apples          1
2 bananas         1

我如何得出单个样本的上述答案:

day_1_df <- as.data.frame(fruit_list[[1]]) 
day_1_df %>% 
  mutate(fruit_count = map(strsplit(day_1_df$fruits, ";"), length)) %>% 
  filter(fruit_count==1) %>% 
  count(solo_fruits = fruits) 

2 个答案:

答案 0 :(得分:0)

并非完全符合您的要求,但这可能会以不同的方式解决您的问题:

library(tidyverse)

day_1 <- as_data_frame(setNames(list(c("oranges;peaches;apples", "pears;apples", "bananas", "oranges;apples", "apples")), "fruits"))
day_2 <- as_data_frame(setNames(list(c("oranges;apples", "peaches","apples;bananas;", "pears", "apples;peaches", "oranges")), "fruits"))
day_3 <- as_data_frame(setNames(list(c("peaches;pears","apples","bananas")), "fruits"))

df <- tibble(day = 1:3, fruits = c(day_1, day_2, day_3)) %>% 
  unnest() %>% 
  mutate(fruits = strsplit(fruits, ";"), customer = row_number()) %>% 
  unnest()

df %>% 
  group_by(customer) %>% 
  filter(n() == 1) %>% 
  group_by(customer, day, fruits) %>% 
  summarise(n = n())

# # A tibble: 7 x 4
# # Groups:   customer, day [?]
#   customer   day fruits      n
#      <int> <int> <chr>   <int>
# 1        3     1 bananas     1
# 2        5     1 apples      1
# 3        7     2 peaches     1
# 4        9     2 pears       1
# 5       11     2 oranges     1
# 6       13     3 apples      1
# 7       14     3 bananas     1

编辑:误解后更改

答案 1 :(得分:0)

您可以使用str_detect来捕获没有;的行。或者您可以使用str_count来计数;然后添加1。

fruit_list%>%
     map(~filter(.x,!str_detect(fruits,";"))%>%
             mutate(solo_fruits = fruits,count = 1,fruits=NULL))
[[1]]
# A tibble: 2 x 2
  solo_fruits count
  <chr>       <dbl>
1 bananas         1
2 apples          1

[[2]]
# A tibble: 3 x 2
  solo_fruits count
  <chr>       <dbl>
1 peaches         1
2 pears           1
3 oranges         1

[[3]]
# A tibble: 2 x 2
  solo_fruits count
  <chr>       <dbl>
1 apples          1
2 bananas         1

使用str_count的意思是:它将为您提供每行水果的总数。而不是拆分然后使用长度

fruit_list%>%
    map(~mutate(.x,count = str_count(fruits,";") + 1))