How to replace NA (Not Available) values in an R data frame with 0 (or some other value).
For an example to work with, here’s code to define a data frame with two columns of data (A and B) with a random set of numbers and NA values mixed in:
df <- data.frame(
A = c(3, 2, NA, 5, 2, 3, 4, 1, 5, NA),
B = c(11, NA, 10, 12, 13, NA, 12, 13, NA, 14)
)
To replace all the NA values with 0, we can execute this code:
df[is.na(df)] <- 0
is.na(df)
Returns a logical vector the same shape as df, with TRUE where the values are NA, and FALSE elsewheredf[is.na(df)]
Selects only the elements of df that are NA
<- 0
Assigns the value 0 to all of those NA positions