-2

Say I've got a list of various character vectors, with lengths between 0 and 10. I want to turn this into a column of a dataframe. Is this possible?

Andrew
  • 1

1 Answers1

0

Sure, with tibble::tibble() it is, the only complication being that columns that are not of length 1 or 10 (in your case) will have to be list-columns. For example:

df <- tibble::tibble(
  first = list(rep("a", 6)),
  second = list(rep("b", 7)),
  third = list(rep("c", 4)),
  fourth = rep("d", 10)
)

Result:

> df
# A tibble: 10 x 4
       first    second     third fourth
      <list>    <list>    <list>  <chr>
 1 <chr [6]> <chr [7]> <chr [4]>      d
 2 <chr [6]> <chr [7]> <chr [4]>      d
 3 <chr [6]> <chr [7]> <chr [4]>      d
 4 <chr [6]> <chr [7]> <chr [4]>      d
 5 <chr [6]> <chr [7]> <chr [4]>      d
 6 <chr [6]> <chr [7]> <chr [4]>      d
 7 <chr [6]> <chr [7]> <chr [4]>      d
 8 <chr [6]> <chr [7]> <chr [4]>      d
 9 <chr [6]> <chr [7]> <chr [4]>      d
10 <chr [6]> <chr [7]> <chr [4]>      d

If you had literally a list of character vectors, you could put them into a column in a tibble with c().

RobertMyles
  • 2,673
  • 3
  • 30
  • 45