purrr rbind每组数据帧列表

时间:2019-03-27 09:21:48

标签: r purrr

使用purrr和朋友阅读大量的csvs之后,我得到了一个tibble,看起来像这样:

library(tidyverse)

df <- 
  tibble(
    df_name = c("A", "B", "A", "A", "B"),
    data = list(iris)
  )

df

# A tibble: 5 x 2
  df_name data                  
  <chr>   <list>                
1 A       <data.frame [150 × 5]>
2 B       <data.frame [150 × 5]>
3 A       <data.frame [150 × 5]>
4 A       <data.frame [150 × 5]>
5 B       <data.frame [150 × 5]>

我想用公用的rbind df_name(或等效的)所有数据。我希望输出是一个命名列表。我可以使用tapply

desired = tapply(df$data, df$df_name, function(y) do.call(rbind,y))   

List of 2
 $ A:'data.frame':  450 obs. of  5 variables:
  ..$ Sepal.Length: num [1:450] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
  ..$ Sepal.Width : num [1:450] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
  ..$ Petal.Length: num [1:450] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
  ..$ Petal.Width : num [1:450] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
  ..$ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ B:'data.frame':  300 obs. of  5 variables:
  ..$ Sepal.Length: num [1:300] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
  ..$ Sepal.Width : num [1:300] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
  ..$ Petal.Length: num [1:300] 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
  ..$ Petal.Width : num [1:300] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
  ..$ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
 - attr(*, "dim")= int 2
 - attr(*, "dimnames")=List of 1
  ..$ : chr [1:2] "A" "B"

我不知道如何对purrr动词执行相同的操作。我认为也许我需要先设置列表名称:

df_p <- 
  df %>%
  mutate(data = setNames(data, df_name))

我发现了this个问题,但我不知道如何在这种情况下申请。

3 个答案:

答案 0 :(得分:3)

我们可以使用tidyr::unnest

library(tidyverse)
df %>% split(.$df_name) %>% map(.%>%unnest() %>% select(-df_name))

#OR
df %>% split(.$df_name) %>% map(~unnest(.) %>% select(-df_name))
df %>% unnest(data) %>% split(.$df_name) 

@kath指出我们可以直接使用unnest

df %>% split(.$df_name) %>% map(unnest) 

答案 1 :(得分:2)

您可以使用import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.databind.PropertyNamingStrategy; import com.fasterxml.jackson.databind.annotation.JsonNaming; @JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class) @JsonIgnoreProperties(ignoreUnknown = true) public class Product { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; private String name; private int price; private String vendor; private String description; private Boolean returnPolicy; 中的reducepurrr中的bind_rows(类似于rbind)。

dplyr

对于与您的df_list <- df %>% group_by(df_name) %>% summarize(data = list(reduce(data, bind_rows))) df_list # A tibble: 2 x 2 # df_name data # <chr> <list> # 1 A <data.frame [450 x 5]> # 2 B <data.frame [300 x 5]> 版本完全相同的结构,我们需要添加以下内容:

tapply

答案 2 :(得分:1)

我将使用unnestgroup_split

df %>% unnest(data) %>% group_split(df_name)

# [[1]]
# # A tibble: 450 x 6
#   df_name Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#   <chr>          <dbl>       <dbl>        <dbl>       <dbl> <fct>  
# 1 A                5.1         3.5          1.4         0.2 setosa 
# 2 A                4.9         3            1.4         0.2 setosa 
# 3 A                4.7         3.2          1.3         0.2 setosa 
# 4 A                4.6         3.1          1.5         0.2 setosa 
# 5 A                5           3.6          1.4         0.2 setosa 
# 6 A                5.4         3.9          1.7         0.4 setosa 
# 7 A                4.6         3.4          1.4         0.3 setosa 
# 8 A                5           3.4          1.5         0.2 setosa 
# 9 A                4.4         2.9          1.4         0.2 setosa 
# 10 A                4.9         3.1          1.5         0.1 setosa 
# # ... with 440 more rows
# 
# [[2]]
# # A tibble: 300 x 6
#   df_name Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#   <chr>          <dbl>       <dbl>        <dbl>       <dbl> <fct>  
# 1 B                5.1         3.5          1.4         0.2 setosa 
# 2 B                4.9         3            1.4         0.2 setosa 
# 3 B                4.7         3.2          1.3         0.2 setosa 
# 4 B                4.6         3.1          1.5         0.2 setosa 
# 5 B                5           3.6          1.4         0.2 setosa 
# 6 B                5.4         3.9          1.7         0.4 setosa 
# 7 B                4.6         3.4          1.4         0.3 setosa 
# 8 B                5           3.4          1.5         0.2 setosa 
# 9 B                4.4         2.9          1.4         0.2 setosa 
# 10 B                4.9         3.1          1.5         0.1 setosa 
# # ... with 290 more rows