I have a tibble with a column containing (nested) tibbles. The nested tibbles have duplicated data (same names, same values):
df <- tibble(id = 1:2, data = list(tibble(id = 1, var1 = "a", var2 = "b"), tibble(id = 2, var1 = "c", var2 = "d")))
df
# # A tibble: 2 x 2
# id data
# <int> <list>
# 1 1 <tibble [1 x 3]>
# 2 2 <tibble [1 x 3]>
Calling df %>% unnest(data)
results in
Error: Names must be unique.
.
I would like to write a function that drops these columns beforehand but don't know how. My goal is to be run to use something along the lines of:
df %>%
drop_duplicated_cols(data) %>%
unnest(data)
Which would result in:
#> # A tibble: 2 x 4
#> id var1 var2
#> <int> <chr> <chr>
#> 1 1 a b
#> 2 2 c d