+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 754 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
• 1,720 points

Could you please explain what does this syntax mean:

```"length<-"

```

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)
 3
> length(d1\$B)
 5
> length<-max(lengths(d1))
> length
 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  2  3 NA NA

\$B
 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.

## how to convert a data frame into a list in R

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

## 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

## How to use a function to repeat a set of procedures on specific set of columns in a data frame?

You can parse the strings to symbols. ...READ MORE

## In data frame how to spilt strings into values?

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

+1 vote

## How to sort a data frame by columns in R?

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