I want to turn a dataframe into a list of lists, as in this example:
df <- data.frame(var1=c("A","A","A","B","B","C"),var2=seq(1,6))
> var1 var2
1 A 1
2 A 2
3 A 3
4 B 4
5 B 5
6 C 6
split(df$var2,df$var1)
$A
[1] 1 2 3
$B
[1] 4 5
$C
[1] 6
However, I wonder if it is possible to do it using a %>%
pipe.
I don't know how to use split
in a pipe, where df
is given as an input. Or maybe there's another way.
There are a couple of options, some of them already mentioned in the comments:
df <- data.frame(var1=c("A","A","A","B","B","C"),var2=seq(1,6))
# magrittr pipe
df %>%
{split(.$var2, .$var1)}
#> $A
#> [1] 1 2 3
#>
#> $B
#> [1] 4 5
#>
#> $C
#> [1] 6
# base pipe
df |>
(\(x) split(x$var2, x$var1))()
#> $A
#> [1] 1 2 3
#>
#> $B
#> [1] 4 5
#>
#> $C
#> [1] 6
# dplyr
library(dplyr)
library(purrr)
df %>%
group_by(var1) %>%
group_map(~ pull(.x, var2)) %>%
set_names(unique(df$var1))
#> $A
#> [1] 1 2 3
#>
#> $B
#> [1] 4 5
#>
#> $C
#> [1] 6
Created on 2023-03-04 with reprex v2.0.2