Learn R Programming

berryFunctions (version 1.4)

l2df: list to data.frame

Description

convert list with vectors of unequal length to dataframe, pad with NAs

Usage

l2df(list, byrow = TRUE)

Arguments

list
List with vectors of irregular length.
byrow
Transposed output? DEFAULT: TRUE

Value

  • data.frame

References

http://stackoverflow.com/questions/5531471/combining-unequal-columns-in-r http://stackoverflow.com/questions/15753091/convert-mixed-length-named-list-to-data-frame http://stackoverflow.com/questions/5942760/most-efficient-list-to-data-frame-method http://stackoverflow.com/questions/8799990/converting-given-list-into-dataframe http://stackoverflow.com/questions/4227223/r-list-to-data-frame

See Also

sapply

Examples

Run this code
eglist <- list(BB=c(6,9,2,6), KA=1:8, JE=c(-3,2) )
eglist
l2df(eglist)  # names are even kept
l2df(eglist, byrow=FALSE)
class(  l2df(eglist, byrow=FALSE)  ) # matrix
# So technically, the list is converted to a matrix, not a data.frame
# But I guess people search more often for convert R list to df (or table)

eglist <- list(BB=c(6,9,2,6), KA="no", JE=c(-3,2) )
eglist
l2df(eglist)  # now everything is a character

eg2 <- list(BB=c(6,9,2,6), KA=matrix(1:8, ncol=2), JE=c(-3,2) )
eg2
l2df(eg2, FALSE)
# so a matrix is internally converted to a vector and then used regularly

eg2 <- list(BB=c(6,9,2,6), KA=data.frame(SW=1:8, SB=4:-3), JE=c(-3,2) )
eg2
# l2df(eg2) # it is not possible to do this with a data.frame
# If you have a list with only data.frames, you could use the following:
eg3 <- list(KA=data.frame(SW=1:8, SB=4:-3), LS=data.frame(BB=23:24, JE=c(-3,2)))
eg3
do.call(cbind, eg3) # but this recycles the values of shorter tables!
# check some of the links above if you really have this problem...

Run the code above in your browser using DataLab