I am trying to extract data from a nested tibble. Within the outer tibble, not all tibbles may exist or be complete. In case of an non-existing column I would like to return 0.
df <- tibble(a = tibble(iris),
b = tibble(iris[1:2]),
c = NULL)
now I'd like to extract the column 'species' from each nested tibble, where the generated column is filled with NA if no data are available. So that the result equals:
tibble(a_s = iris$Species,
b_s = NA,
c_s = NA)
Is there any way I could achieve this?
I naively tried:
transmute(df, a_s = a$species,
b_s = b$species,
c_s = c$species)
which of course only works for a_s
,
generates a warning for b_s
and throws an error for c_s
.
I have tried creating a helper function to evaluate the existence of each column, but this didn't work for nested dataframes. Any ideas on how to solve this?
UPDATE: for clarity, I always want to generate the output as specified, while tibble c may or may not be there.