This is the closest to the solution that I could find: r - Remove NAs from nested list data frame - Stack Overflow
However, it's not really applicable and leaves empty strings. I would like to remove them altogether.
Here is example data (but really should be multiple rows):
df_example = data.frame(column1 = 'a') |>
mutate(column1 = list(c('a', NA, 'b', NA)))
This is my desired result:
df_example = data.frame(column1 = 'a') |>
mutate(column1 = list(c('a', 'b')))
If it becomes c('a', NA)
-> 'a'
instead of c('a')
, that's also fine.
I thought something like this would work, but it only works because there is only one row (I couldn't find out how to create another row of data...):
df_example |>
mutate(column1_cleaned = str_subset(unlist(column1), '.+'))
If there are multiple rows, then each cell of column1_cleaned
contains entire column of column1
data.
How can I achieve this using tidyverse
?