Replace NA values with zeros in R DataFrame Last Updated : 07 Apr, 2021 Comments Improve Suggest changes Like Article Like 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 Comment More infoAdvertise with us Next Article Replace NA values with zeros in R DataFrame M mallikagupta90 Follow Improve Article Tags : R Language R Programs R-DataFrame R DataFrame-Programs Similar Reads Replace 0 with NA in R DataFrame In this article, we are going to discuss how to replace 0 with NA values in dataframe in R Programming Language. NA stands for Null values which can represent Null data / Null elements in a dataframe. The task can be achieved by first defining a dataframe that contains 0 as values. Then we can repla 1 min read Replace Character Value with NA in R In this article, we are going to see how to replace character value with NA in R Programming Language. We can replace a character value with NA in a vector and in a dataframe. Example 1: Replace Character Value with NA in vector In a vector, we can replace it by using the indexing operation. Syntax: 2 min read Replace Inf with NA in Dataframe in R In this article, we will discuss how to replace Inf (Infinity) with NA in Dataframe in R Programming Language. Create a dataframe with for demonstration: R # create a dataframe with 3 columns and 5 rows data=data.frame(column1 = c(Inf, 2, 3, 4, 5), column2 = c(1, Inf, 1, Inf, 1), column3 = c(1,2,3,I 2 min read Merge Two Unequal DataFrames and Replace NA with 0 in R In this article we are going to merge two unequal dataframes and then replace NA values in the resultant dataframe with 0 using R programming language. Dataframes in use: Merging the dataframes We can merge the two dataframes by using merge() function. Syntax: merge(dataframe1, dataframe2, Â by = "c 2 min read Replace Spaces in Column Names in R DataFrame In this article, we will replace spaces in column names of a dataframe in R Programming Language. Let's create a Dataframe with 4 columns with 3 rows: R # create a dataframe with 4 columns and 3 rows data = data.frame("web technologies" = c("php","html","js"), "backend tech" = c("sql","oracle","mong 2 min read Remove NA Values from Vector in R In this article, we are going to discuss how to remove NA values from the vector. Method 1: Using is.na() We can remove those NA values from the vector by using is.na(). is.na() is used to get the na values based on the vector index. !is.na() will get the values except na. Syntax: vector[!is.na(vect 2 min read Replace Blank by NA in R DataFrame In this article, we are going to see how to replace Blank space with NA in dataframe in R Programming Language. Example 1: R program to replace blank by NA in dataframe We will replace the empty cells using a logical condition based on the "==" operator. Syntax: dataframe[dataframe == ""] <- NA R 2 min read Replace values from dataframe column using R In this article, we will discuss how to replace values from a DataFrame column in R Programming Language. Whenever we deal with some data, it is necessary that we draw accurate conclusions from it. Now imagine what if, there are some missing values(this happens when some observation is missing in a 3 min read Count non zero values in each column of R dataframe In this article, we are going to count the number of non-zero data entries in the data using R Programming Language. To check the number of non-zero data entries in the data first we have to put that data in the data frame by using: data <- data.frame(x1 = c(1,2,0,100,0,3,10), x2 = c(5,0,1,8,10,0 2 min read Remove rows with NA in one column of R DataFrame Columns of DataFrame in R Programming Language can have empty values represented by NA. In this article, we are going to see how to remove rows with NA in one column. We will see various approaches to remove rows with NA values.ApproachCreate a data frameSelect the column based on which rows are to 2 min read Like