Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

convert similarly named unequally length list elements to data frame R

$
0
0

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?


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>