How to Exchange NA with FALSE in R (Example Code)
In this article, I’ll show how to replace NA by FALSE in the R programming language.
Creation of Example Data
my_df <- data.frame(col_a = c(NA, TRUE, NA, TRUE, NA), # Example data in R col_b = c(TRUE, NA, TRUE, NA, TRUE)) my_df # Display example data in RStudio console # col_a col_b # 1 NA TRUE # 2 TRUE NA # 3 NA TRUE # 4 TRUE NA # 5 NA TRUE |
my_df <- data.frame(col_a = c(NA, TRUE, NA, TRUE, NA), # Example data in R col_b = c(TRUE, NA, TRUE, NA, TRUE)) my_df # Display example data in RStudio console # col_a col_b # 1 NA TRUE # 2 TRUE NA # 3 NA TRUE # 4 TRUE NA # 5 NA TRUE
Example: Substitute NA by FALSE Using is.na() Function
my_df[is.na(my_df)] <- FALSE # Exchange NA with FALSE my_df # Display replaced data frame # col_a col_b # 1 FALSE TRUE # 2 TRUE FALSE # 3 FALSE TRUE # 4 TRUE FALSE # 5 FALSE TRUE |
my_df[is.na(my_df)] <- FALSE # Exchange NA with FALSE my_df # Display replaced data frame # col_a col_b # 1 FALSE TRUE # 2 TRUE FALSE # 3 FALSE TRUE # 4 TRUE FALSE # 5 FALSE TRUE
Further Resources
In addition, you could have a look at some of the other tutorials on my website.