1

I have a list abc = list(c(1,2), 3, c(4,5,6))

I want to create a data frame df with 1 column df$col1 which looks like the following:

[[1]]
[1] 1 2

[[2]]
[1] 3

[[3]]
[1] 4 5 6

How do I do this? Running df = data.frame(abc = list(c(1,2), 3, c(4,5,6))) gives the error

Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,  : 
  arguments imply differing number of rows: 2, 1, 3

because data.frame() looks to convert this list into a >1 column data frame, spreading the each list element over different columns, and I can't find any argument in data.frame which stops it from doing so.

Elis
  • 70
  • 10

2 Answers2

1

I guess you may need tibble

> tibble(abc)
# A tibble: 3 × 1
  abc
  <list>
1 <dbl [2]>
2 <dbl [1]>
3 <dbl [3]>

or data.table

> data.table(abc)
     abc
1:   1,2
2:     3
3: 4,5,6
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
1

You have to create a data frame and then add the list:

df <- data.frame(a=1:3)
df$abc <- abc
df
#   a     abc
# 1 1    1, 2
# 2 2       3
# 3 3 4, 5, 6
dcarlson
  • 10,936
  • 2
  • 15
  • 18