How to Convert a List Consisting of Vector of Different Lengths to a Usable Data Frame in R

How to convert a list consisting of vector of different lengths to a usable data frame in R?

try this:

word.list <- list(letters[1:4], letters[1:5], letters[1:2], letters[1:6])
n.obs <- sapply(word.list, length)
seq.max <- seq_len(max(n.obs))
mat <- t(sapply(word.list, "[", i = seq.max))

the trick is, that,

c(1:2)[1:4]

returns the vector + two NAs

List of different length character vectors into data frame

The easiest would be stri_list2matrix

library(stringi)
df <- as.data.frame(stri_list2matrix(folders, byrow = TRUE), stringsAsFactors=FALSE)
names(df) <- paste0("Level", seq_along(df))
df
# Level1 Level2 Level3
#1 Main <NA> <NA>
#2 Main Mid <NA>
#3 Main Mid Sub

But, this can also be solved with base R

m1 <- max(lengths(folders))
d1 <- as.data.frame(do.call(rbind, lapply(folders, `length<-`, m1)), stringsAsFactors= FALSE)
names(d1) <- paste0("Level", seq_along(d1))

The simplest way to convert a list with various length vectors to a data.frame in R

We can use

data.frame(lapply(aa, "length<-", max(lengths(aa))))

Or using tidyverse

library(dplyr)
library(tibble)
library(tidyr)
enframe(aa) %>%
unnest(value)

Convert list of named vectors of different length to tibble

Try stack

> stack(example)
values ind
1 1 a
2 2 a
3 3 a
4 2 b
5 3 b
6 3 c
7 4 c
8 5 c
9 6 c

Function to transform lists of different lengths into data frame utilizing recycling

The issue is you need to apply listExtend to each vector of fullList in the DF function.

x <- c(1:7)
y <- c("a","b","c","d","e")
z <- c(TRUE,TRUE,FALSE,FALSE)
fullList <- list(x,y,z)


DF <- function(x) {
maxLength <- max(lengths(x))
newList <- lapply(x, function(l) rep(l, length.out = maxLength))
finalDF <- data.frame(newList)
return(finalDF)
}


outDF <- DF(fullList)
colnames(outDF) <- c('x', 'y', 'z')
outDF
#------
x y z
1 1 a TRUE
2 2 b TRUE
3 3 c FALSE
4 4 d FALSE
5 5 e TRUE
6 6 a TRUE
7 7 b FALSE

How do you convert large list with vectors of different lenght to dataframe?

You could do:

t(as.data.frame(lapply(lst, "length<-", max(lengths(lst)))))

# [,1] [,2] [,3] [,4] [,5]
#a 1 2 4 5 6
#c 7 8 9 NA NA
#c.1 10 11 NA NA NA

Or as @Andrew pointed out, you can do:

t(sapply(lst, "length<-", max(lengths(lst))))

# [,1] [,2] [,3] [,4] [,5]
#a 1 2 4 5 6
#c 7 8 9 NA NA
#c 10 11 NA NA NA

How to put elements of a list of vectors of different lengths into a data frame, with elements of vectors being separated as different columns

The solution below (1) converts each list element into a dataframe where t() is used to get the element names to be column names in the dataframe, and (2) row-binds those dataframes together where data.table's fill argument is helpful for the inconsistent vector lengths/names.

mylist <- list(
one = c(SW=0.49, GHS=0.46, GS=0.03),
two = c(GHS=0.07, GW = 0.03, LLW=0.03, GS=0.00),
six = c(GHS=0.41, SW=0.42, TD=0.09)
)

temp <- lapply(mylist, function(x) data.frame(t(x)))
data.table::rbindlist(temp, fill=TRUE)

output:

     SW  GHS   GS   GW  LLW   TD
1: 0.49 0.46 0.03 NA NA NA
2: NA 0.07 0.00 0.03 0.03 NA
3: 0.42 0.41 NA NA NA 0.09


Related Topics



Leave a reply



Submit