How can I replace all ‘NA‘ with ‘NULL‘ in an R dataframe without specifying column names?
I found replace_na function from tidyr which has an example as:
# Replace NAs in a data frame
df <- tibble(x = c(1, 2, NA), y = c("a", NA, "b"))
df %>% replace_na(list(x = 0, y = "unknown"))
but my table has more than 10 columns and it could change. Can’t specify column names like in the example above.
>Solution :
Base R way to do this:
apply(df, 2, function(x) { x[ is.na(x) ] <- 'NULL'; x})
Note that you can’t really insert NULL as it has length 0, you can insert: '' or NA