0

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.

2
  • 3
    What do you mean by 'NULL'? If you mean the string 'NULL', then you'll need to first convert your numeric columns to character class, then it's as simple as df[is.na(df)] <- "NULL". If you want the actual NULL, then that doesn't really work. NULL has 0-length, so it can't fill a spot in a data frame. c(1, 2, NULL) is the same as c(1, 2) - NULL isn't there. Commented Nov 10, 2022 at 17:34
  • good point. I meant string 'NULL' Commented Nov 10, 2022 at 17:42

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

This will return a matrix, not a data.frame, and if there are a mix of character and numeric columns the numeric columns will be coerced to character even if they don't have any NA's because apply converts its first argument to a matrix.
0

Here are two approaches. One assuming you want 0 in numeric columns and "unknown" in character, and one assuming you want 'NULL' independent of column type.

Approach 1:

library(tidyr); library(dplyr, warn.conflicts = FALSE) df <- tibble(x = c(1, 2, NA), y = c("a", NA, "b")) df %>% mutate( across(where(is.numeric), replace_na, 0), across(where(is.character), replace_na, "unknown") ) #> # A tibble: 3 × 2 #> x y #> <dbl> <chr> #> 1 1 a #> 2 2 unknown #> 3 0 b 

Created on 2022-11-10 by the reprex package (v2.0.1)

Approach 2:

library(tidyr); library(dplyr, warn.conflicts = FALSE) df <- tibble(x = c(1, 2, NA), y = c("a", NA, "b")) df %>% mutate( across(everything(), ~ if(anyNA(.)) replace_na(as.character(.), 'NULL')) ) #> # A tibble: 3 × 2 #> x y #> <chr> <chr> #> 1 1 a #> 2 2 NULL #> 3 NULL b 

Created on 2022-11-10 by the reprex package (v2.0.1)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.