Open In App

Replace NA values with zeros in R DataFrame

Last Updated : 07 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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. 

Example:

R




# declaring a data frame in R
data_frame = data.frame(C1 = c(1, 2, NA, 0),
                        C2 = c( NA, NA, 3, 8), 
                        C3 = c(9, 7, -1, NA))
  
# printing the data frame
print("Original Data Frame")
print (data_frame)
  
# replacing NA values in data frame
data_frame[is.na(data_frame)] = 0
  
# printing modified data frame
print("Modified data frame")
print (data_frame)


Output

[1] "Original Data Frame"
 C1 C2 C3
1  1 NA  9
2  2 NA  7
3 NA  3 -1
4  0  8 NA
[1] "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)

Example:

R




# declaring a data frame in R
data_frame = data.frame(C1 = c(1, 2, NA, 0),
                        C2 = c( NA, NA, 3, 8), 
                        C3 = c("A", "V", "j", "y"))
  
# printing the data frame
print("Original Data Frame")
print (data_frame)
  
# using replace method change the na value to 0
modified_data_frame <- replace(data_frame,is.na(data_frame),0)
  
print("Modified Data Frame")
print (modified_data_frame)


Output

[1] "Original Data Frame"
 C1 C2 C3
1  1 NA  A
2  2 NA  V
3 NA  3  j
4  0  8  y
[1] "Modified Data Frame"
 C1 C2 C3
1  1  0  A
2  2  0  V
3  0  3  j
4  0  8  y


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads