Here I have a list with different length vectors. And I'd want to get a data.frame. I've seen lots of posts about it in SO (see ref), but none of them are as simple as I expected because this is really a common task in data preprocessing. Thank you.
Here simplest means as.data.frame(aa)
if it works. So one function from the base package of R will be great. sapply(aa, "length<-", max(lengths(aa)))
has four functions actually.
An example is shown below.
Input:
aa <- list(A=c(1, 3, 4), B=c(3,5,7,7,8))
Output:
A B
1 3
3 5
4 7
NA 7
NA 8
A and B are the colnames of the data.frame.
One answer is sapply(aa, '[', seq(max(sapply(aa, length))))
, but it's also complex.
ref:
data.frame(lapply(aa, "length<-", max(lengths(aa))))
It is also faster when compared tosapply(aa, length)
– Adjacentlibrary(stringi); stri_list2matrix(aa)
but the character elements needs to be converted tonumeric
though. I am not sure whethersimple
meanscompact
code for you though. – Adjacentstri_list2matrix
is a simple answer, though I think there should be a function in the base package in R. In my opinion,simple
means easy to use and to be understood. – Lustigsimple
for you. – Adjacentsapply(aa, "length<-", max(lengths(aa)))
works as well. Here it seemslength<-
meanslength(x) <- max(lengths(aa))
? – Lustigdo.call(qpcR:::cbind.na, aa)
could be interesting, but is not fully base R though. – Oology