Remove NA values in a nested column using stringr? Remove empty values in a nested column?

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?

NA is not a character string. It's typeof logical

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(purrr)
df_example = data.frame(column1 = 'a') |> 
  mutate(column1 = list(c('a', NA, 'b', NA)))
df_example <- rbind(df_example,df_example)
df_example %>%
  mutate(column1 = map(column1, ~na.omit(.x)))
#>   column1
#> 1    a, b
#> 2    a, b

Created on 2023-11-26 with reprex v2.0.2

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.