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,750 points
1,619 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, 2019 in Data Analytics by anonymous
• 33,030 points
1,283 views
0 votes
1 answer

How to combine a list of data frames into one data frame?

Use bind_rows() from the dplyr package: bind_rows(list_of_dataframes, .id ...READ MORE

answered Dec 17, 2020 in Data Analytics by Gitika
• 65,890 points
1,136 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,130 points

edited Apr 12, 2018 by nirvana 22,078 views
0 votes
1 answer
0 votes
2 answers

In data frame how to spilt strings into values?

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

answered Dec 5, 2018 in Data Analytics by Kalgi
• 52,350 points
1,051 views
0 votes
1 answer
0 votes
1 answer
+1 vote
2 answers

How to sort a data frame by columns in R?

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

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
1,768 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
1,722 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
1,287 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP