Replace NA values with zeros in R DataFrame
In this article, we will discuss how to replace NA values with zeros in DataFrame in R Programming Language. The NA value in a data frame can be replaced by 0 using the following functions.
Method 1: using is.na() function
is.na() is an in-built function in R, which is used to evaluate a value at a cell in the data frame. It returns a true value in case the value is NA or missing, otherwise, it returns a boolean false value. In this approach, we loop over all the cells of the data frame, and in case the value is NA, we replace it by 0. The changes are made to the original data frame.
Syntax: Dataframe[is.na(Dataframe)] = 0
Arguments : Dataframe is the data frame we wish to perform replacement of values on.
 "Original Data Frame" C1 C2 C3 1 1 NA 9 2 2 NA 7 3 NA 3 -1 4 0 8 NA  "Modified data frame" C1 C2 C3 1 1 0 9 2 2 0 7 3 0 3 -1 4 0 8 0
Method 2: Using replace() method
An alternative to the reassignment of the data frame cells having NA is to use the in-built R method to replace these values. is.na() method is used to evaluate whether the data element has a missing or NA value and then replace method is used to replace this value with a 0. These changes are not made to the original data frame, but have to be explicitly stored in there. The time incurred to carry out this operation is polynomial in terms of the data frame size.
modified_data_frame <- replace(data_frame,is.na(data_frame),0)
 "Original Data Frame" C1 C2 C3 1 1 NA A 2 2 NA V 3 NA 3 j 4 0 8 y  "Modified Data Frame" C1 C2 C3 1 1 0 A 2 2 0 V 3 0 3 j 4 0 8 y