[R] turning a list of vectors into a data.frame (as rows of the DF)?
R. Michael Weylandt
michael.weylandt at gmail.com
Wed Jan 11 21:34:34 CET 2012
Most methods take the rows of data.frame()s to be very significant
(indicating multiple values from a single observation) so what you're
doing seems like it may be "against the spirit of R", but if you want
a simple NA padding at the end, this should do it:
listToDF <- function(inputList, fill = NA){
# Use fill = NULL for regular recycling behavior
maxLen = max(sapply(inputList, length))
for(i in seq_along(inputList))
inputList[[i]] <- c(inputList[[i]], rep(fill, maxLen -
length(inputList[[i]])))
return(as.data.frame(inputList))
}
listToDF(list(x = 1:4, y = 1:2))
Michael
On Wed, Jan 11, 2012 at 2:40 PM, Chris Conner <connerpharmd at yahoo.com> wrote:
> As a newer R practicioner, it seems I stump myself weekly (at least) with issues that have spinning my wheels. Here is yet another... I'm trying to turn a list of numeric vectors (of uneual length) inot a dataframe. Each vector held in the list represents a row, and there are some rows of unequal length. I would like NAs as placeholders for "missing" data in the shorter vectors. I think I'm missing something quite basic.
>
> v1 <- c(1,2,3,4)
> v2 <- c(1,2)
> lst1 <- list(v1,v2)
>
> Of course there is the intuitive:
>
> as.data.frame(lst1)
>
> However, the recycling rule (expectedly) reclycles 1,2 versus using NAs as placeholders.
>
> Then, looking into Teetor's R Cookbook, there is a piece of code that looked (from the description) like it might do the trick:
>
> do.call(rbind, Map(as.data.frame,lst1)
>
> But I get the error:
> Error in match.names(clabs, names(xi)) :
> names do not match previous names
>
> Thinking the source of the error had to do with the vectors of unequal lenght, I tried Hadley's rbind.fill thusly:
>
> library(reshape)
> do.call(rbind.fill, Map(as.data.frame,lst1)
>
> Which produced a dataset, but gain, not in the desired format.
>
> Thanks in advance to anyone that can bring my frustrations to end!
> C
> [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list