Reputation: 1040
I have the below list in a column of a data frame.
As you can see, the variables change through the items. The column affilications
is not always present.
I have been trying to flatten
the list to a data frame or to a list of 3
, but I am geeting a single columg with all elements of every column.
Is there a way I can tell R
that each element has 3 columns and that the first one is not always present and to fill it with let's say null
.
[[1]]
NULL
[[2]]
affiliations author_id author_name
1 Punjabi University 780E3459 munish puri
2 Punjabi University 48D92C79 rajesh dhaliwal
3 Punjabi University 7D9BD37C r s singh
[[3]]
author_id author_name
1 7FF872BC barbara eileen ryan
[[4]]
author_id author_name
1 0299B8E9 fraser j harbutt
[[5]]
author_id author_name
1 7DAB7B72 richard m freeland
[[6]]
NULL
This is what I'm getting when I try and flatten it.
authors
1 Punjabi University
2 Punjabi University
3 Punjabi University
4 780E3459
5 48D92C79
6 7D9BD37C
7 munish puri
8 rajesh dhaliwal
9 r s singh
10 7FF872BC
But what I really need would be:
[[1]] NULL
[[2]]affiliations author_id author_name
1 Punjabi University 780E3459 munish puri
2 Punjabi University 48D92C79 rajesh dhaliwal
3 Punjabi University 7D9BD37C r s singh
[[3]] NULL author_id author_name
1 NULL 7FF872BC barbara eileen ryan
Upvotes: 0
Views: 41
Reputation: 21507
I i understand you correctly you have data as follows:
require(tidyverse)
list(
NULL,
tibble(a=c(2, 2), b=c(2, 2), c=c(2, 2)),
tibble(b=3, c=3)
)
So:
[[1]]
NULL
[[2]]
# A tibble: 2 x 3
a b c
<dbl> <dbl> <dbl>
1 2 2 2
2 2 2 2
[[3]]
# A tibble: 1 x 2
b c
<dbl> <dbl>
1 3 3
Using bind_rows
results in:
bind_rows(list(
NULL,
tibble(a=c(2, 2), b=c(2, 2), c=c(2, 2)),
tibble(b=3, c=3)
))
# A tibble: 3 x 3
a b c
<dbl> <dbl> <dbl>
1 2 2 2
2 2 2 2
3 NA 3 3
Upvotes: 1