convert similarly named unequally length list elements to data frame R

convert similarly named unequally length list elements to data frame R

convert list with different length to dataframe r
convert list to dataframe r
combining vectors of different lengths in r
r create dataframe from vectors of different length
r convert list to dataframe with column names
merge two data frames in r different lengths
list in r
r convert list of vectors to dataframe

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

convert similarly named unequally length list elements to data frame R, Perhaps you want to join? library(dplyr) library(purrr) library(janitor) df %>% group_split(group) %>% map(~tabyl(.x, test_value))  6.3 Data frames. A data frame is a list with class "data.frame". There are restrictions on lists that may be made into data frames, namely The components must be vectors (numeric, character, or logical), factors, numeric matrices, lists, or other data frames.


You could first, add columns suffixes according to the sublists names in the main list, add second a duplicate of the value column equally named e.g. "by" to merge (aka join) by later.

df_list <- Map(function(x) {
  out <- `names<-`(
    df_list[[x]], paste0(x, "_", c("test_value", "n", "percent")))
  out <- cbind(out, by=out[,1])
  }, names(df_list))

res <- merge(df_list$A, df_list$B, all=TRUE)[, -1]
res
#   A_test_value A_n A_percent B_test_value B_n B_percent
# 1            0   1 0.3333333            0   1 0.1666667
# 2            1   1 0.3333333            1   1 0.1666667
# 3            2   1 0.3333333            2   1 0.1666667
# 4           NA  NA        NA            3   1 0.1666667
# 5           NA  NA        NA            4   1 0.1666667
# 6           NA  NA        NA            5   1 0.1666667

The simplest way to convert a list with various length vectors to a , If you want to learn about R Programming visit this R Programming Online Training. Let us take a scenario where your list of lists is called l. Then do: df <- data.frame(matrix(unlist(l), nrow=length(l), byrow=T)). The above will convert all character columns to factors, to avoid this you can add a parameter to  “R Data Import/Export” (R-data) is a guide to importing and exporting data to and from R. “The R Language Definition” (R-lang), a first version of the “Kernighan & Ritchie of R”, explains evaluation, parsing, object oriented programming, computing on the language, and so forth. “R Installation and Administration” (R-admin).


Base R solution:

# Create the data: 

df <- data.frame(group = c(rep("A",3), rep("B", 6)),

                  test_value = c(0,1,2, 0,1,2,3,4,5))

# Create the dataframe list, splitting on group: 

df_list <- lapply(split(df, df$group), data.frame)

# Create the extra vars: 

df_list <- mapply(cbind,  df_list,  "n" = 1,  "percent" = 1/sapply(df_list, nrow), SIMPLIFY = FALSE)

# Row bind the dataframe list together into a single dataframe: 

df2 <- data.frame(do.call(rbind, df_list), row.names = NULL, stringsAsFactors = FALSE)

# Spread by the test_value:

df2 <- reshape(df2, idvar = 'test_value', ids = unique(df2$test_value), direction = 'wide', timevar = 'group')

5 Data Structures, Vectors have a names property, the same length as the vector itself, that gives If vector elements have names, then you can select them by name Lists can even contain other structured objects, such as lists and data frames; It strikes us as very odd that we can turn a list into a matrix just by giving the list a dim attribute. df is a data frame, y is any object, expr is a list of object names separated by ‘+’ (e.g., a + b + c). The first two forms produce distributional plots of the variables in a data frame (first form) or of a number of named objects (second form). The third form plots y against every object named in expr.


[PDF] R Language, Enumerating combinations of a specified length Convert data stored in a list to a single data frame using do.call Convert a list to a vector while keeping empty list elements The similarly named Rprofile.site, located in R's home directory, is logical: when TRUE, rows with unequal length are filled with blank fields. R FAQ Frequently Asked Questions on R Version 1.9-24 2004-06-15 ISBN 3-900051-01-1 Kurt Hornik


7.7 Reshaping a data frame, Name -- -- Class -- -- Size -- ## afl.finalists factor 400 ## afl.margins numeric 176 old list 66 ## oneCorPlot function ## out.0 data.frame 100 x 2 ## out.1 data.​frame At other times, you may need to convert a numeric variable into a different in meaningful units (i.e., it's interval scale), but if you cut it into unequal bin sizes  > accountants <- data.frame(home=statef, loot=incomes, shot=incomef) A list whose components conform to the restrictions of a data frame may be coerced into a data frame using the function as.data.frame() The simplest way to construct a data frame from scratch is to use the read.table() function to read an entire data frame from an external file.


Introduction To Programming In R, Assign the regular expression you found to the name 'girl.file.regex' in R. List elements can be extracted and replaced in the same way as elements of Write a function that returns the class , typeof , and length of its argument. map List of 20 ## $ 1996:Classes 'tbl_df', 'tbl' and 'data.frame': 4957 obs. of 3 variables: ## . Named vectors are considered vectors in R but not in S (e.g., is.vector(c(a = 1:3)) returns FALSE in S and TRUE in R). Data frames are not considered as matrices in R (i.e., if DF is a data frame, then is.matrix(DF) returns FALSE in R and TRUE in S). R by default uses treatment contrasts in the unordered case, whereas S uses the Helmert ones.