I know theres so many list to dataframe questions but I cant find the solution to this easy problem. Lets say I have:
library(tidyverse)
library(janitor)
df <- data.frame( group = c(rep("A",3), rep("B", 6)),
test_value = c(0,1,2, 0,1,2,3,4,5))
df_list <- df %>%
split(.$group) %>%
map(~tabyl(.x$test_value))
df_list
# $A
# .x$test_value n percent
# 0 1 0.3333333
# 1 1 0.3333333
# 2 1 0.3333333
# $B
# .x$test_value n percent
# 0 1 0.1666667
# 1 1 0.1666667
# 2 1 0.1666667
# 3 1 0.1666667
# 4 1 0.1666667
# 5 1 0.1666667
All I want to do is convert it to this named dataframe of the following:
A_test_value A_n A_percent B_test_value B_n B_percent
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 0 1 0.333 0 1 0.167
2 1 1 0.333 1 1 0.167
3 2 1 0.333 2 1 0.167
4 NA NA NA 3 1 0.167
5 NA NA NA 4 1 0.167
6 NA NA NA 5 1 0.167
I've seen this but it is slightly different (Converting nested list (unequal length) to data frame)
Would anyone have a quick solution (maybe dplyr type) please?
Perhaps you want to join?
library(dplyr)
library(purrr)
library(janitor)
df %>%
group_split(group) %>%
map(~tabyl(.x, test_value)) %>%
reduce(full_join, by = "test_value")
test_value n.x percent.x n.y percent.y
1 0 1 0.3333333 1 0.1666667
2 1 1 0.3333333 1 0.1666667
3 2 1 0.3333333 1 0.1666667
4 3 NA NA 1 0.1666667
5 4 NA NA 1 0.1666667
6 5 NA NA 1 0.1666667
For named output indicating group you could do:
df %>%
split(.$group) %>%
map(~tabyl(.x, test_value)) %>%
imap(~set_names(.x, ifelse(names(.x) != "test_value", paste(.y, names(.x), sep = "_"), names(.x)))) %>%
reduce(full_join, by = "test_value")
test_value A_n A_percent B_n B_percent
1 0 1 0.3333333 1 0.1666667
2 1 1 0.3333333 1 0.1666667
3 2 1 0.3333333 1 0.1666667
4 3 NA NA 1 0.1666667
5 4 NA NA 1 0.1666667
6 5 NA NA 1 0.1666667
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With