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?
Asked
Active
Viewed 43 times
-2
-
2please be more explicit with your expected output. yes, list columns are possible. – MichaelChirico Jun 17 '17 at 22:56
-
List columns sound like what I'm looking for. How do you make them? – Andrew Jun 18 '17 at 00:23
-
you assign them just like you would any other column, as long as `length(l)==nrow(DF)` – MichaelChirico Jun 18 '17 at 00:39
-
Found a good solution [here](https://stackoverflow.com/questions/9547518/create-a-data-frame-where-a-column-is-a-list) using `I()` – Andrew Jun 18 '17 at 01:09
1 Answers
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