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.