2
votes

After using purrr and friends to read in a load of csvs I have ended up with a tibble that looks something like this:

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]>

I want to rbind (or equivalent) all data with a common df_name. I'd like the output to be a named list. I can do this with 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"

I can't figure out how to do the same with purrr verbs. I think perhaps I need to start by setting the list names:

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

I found this question but I can't figure out how to apply in this situation.

3

3 Answers

4
votes

We can use 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) 

As @kath pointed out that we can use unnest directly

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

You can use reduce from purrr and bind_rows (similar to rbind) from 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]>

For the exact same structure as in your tapply-version we would need to add the following:

df_list2 <- df_list %>% 
  split(.$df_name) %>% 
  map(~ .x$data[[1]])

str(df_list2)
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 ...
1
votes

I would use unnest and group_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