How to convert a list of vectors with various length into a Data.Frame?

+1 vote

By using as.data.frame() is it easy if it works.

Example:

Input

d1<-list(A=c(1,2,3),B=c(4,5,6,7,8))

Output

   A B
1  1 4
2  2 5
3  3 6
4 NA 7
5 NA 8

A and B are the colnames of the data.frame.

One possible case is sapply(d1, '[', seq(max(sapply(d1, length)))), but it's also complex.

Apr 4, 2018 in Data Analytics by CodingByHeart77
• 3,690 points
135 views

1 answer to this question.

+1 vote

We can easily use this command

as.data.frame(lapply(d1, "length<-",max(lengths(d1))))
answered Apr 4, 2018 by DeepCoder786
• 1,720 points

Could you please explain what does this syntax mean:

"length<-"

Thank you in advance.

In the above code, length <- set the length of vectors (including lists) and factors, and of any other R object

length<-"max(lengths(d1)) set the length of both the list elements as the maximum length of list elements.

> length(d1$A)
[1] 3
> length(d1$B)
[1] 5
> length<-max(lengths(d1))
> length
[1] 5

Then lappy returns a list of the same length by adding extra NA at index with no elements. as.data.frame later converts the list into a data frame.

> lapply(d1, "length<-",max(lengths(d1)))
$A
[1]  1  2  3 NA NA

$B
[1] 4 5 6 7 8

I hope it helps.

Thank you very much indeed, I know generally what lenght() function can do, but this syntax

"length<-",  I have never seen before (I am R-beginner). Do you have maybe more examples how to use such a code, I mean assignment operator <- in quotes like this one: 
"length<-"

best wishes
Hey Andrzej,

A few functions like class, names, etc also allow <- within " ".

I have not used them before, but you can try and share your thoughts.

Related Questions In Data Analytics

0 votes
2 answers

how to convert a data frame into a list in R

Convert whole data frame into a list?? ...READ MORE

answered Sep 4 in Data Analytics by anonymous
• 32,260 points
56 views
0 votes
1 answer

How to convert a list to data frame in R?

Let's assume your list of lists is ...READ MORE

answered Apr 12, 2018 in Data Analytics by nirvana
• 3,060 points

edited Apr 12, 2018 by nirvana 3,934 views
0 votes
1 answer
0 votes
2 answers

How to change column names of a Data frame?

Try this way - names(data frame) <- ...READ MORE

answered Aug 26 in Data Analytics by anonymous
• 32,260 points
96 views
0 votes
2 answers

In data frame how to spilt strings into values?

You can do this using dplyr and ...READ MORE

answered Dec 4, 2018 in Data Analytics by Kalgi
• 45,780 points
68 views
0 votes
1 answer
0 votes
1 answer
0 votes
2 answers

How to sort a data frame by columns in R?

You can use dplyr function arrange() like ...READ MORE

answered Aug 21 in Data Analytics by anonymous
• 32,260 points
163 views
0 votes
1 answer

How to spilt a column of a data frame into multiple columns

it is easily achievable by using "stringr" ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,720 points
119 views
0 votes
1 answer

How to create a list of Data frames?

Basically all we have to do is ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,720 points
113 views