Search code examples
rdataframedplyris-empty

Can't remove empty `character(0)` or `list()` values from R data frame


I have an R data frame that has character(0) and list() values inside the cells. I want to replace these with NA values.

In the following example, the field "teaser" has this issue, but it can be anywhere in the data frame.

df <- structure(list(body = "BAKER TO VEGAS 2022The Office fielded two squads this year in the 36th Annual Baker to Vegas (“B2V”) Challenge Cup Relay on April 9-10.  Members of our 2022 B2V Team include many staff and AUSAs who were joined by office alums and a cadre of friends and family who helped out during some rather brutal conditions this year with temperatures around 100 degrees for much of the days. Most importantly, everyone had fun… and nobody got hurt!  It was a great opportunity to meet (and run past) various members of our law enforcement community and to see the amazing logistics of the yearly event. Congratulations to all the participants.", 
    changed = structure(19156, class = "Date"), created = structure(19156, class = "Date"), 
    date = structure(19090, class = "Date"), teaser = "character(0)", 
    title = "Baker to Vegas 2022", url = "https://www.justice.gov/usao-cdca/blog/baker-vegas-2022", 
    uuid = "cd7e1023-c3ed-4234-b8af-56d342493810", vuuid = "8971702d-6f96-4bbd-ba8c-418f9d32a486", 
    name = "USAO - California, Central,"), row.names = 33L, class = "data.frame")

I've tried numerous things that don't work, including the following:

df <- na_if(df, "character(0)")

Error in charToDate(x) : 
  character string is not in a standard unambiguous format

Thanks for your help.


Solution

  • We could use

    library(dplyr)
    df %>%
       mutate(across(where(is.character), ~ na_if(.x, "character(0)")))