Replace 0 with NA in R (Example) | Changing Zero in Data Frame & Vector
In this article, I’ll illustrate how to substitute 0 with NA in the R programming language.
The tutorial will contain this:
Let’s dive into it!
Creation of Example Data
We’ll use the following data frame for the example of this R programming tutorial:
data <- data.frame(x1 = c(2, 0, 7, 4, 0, 5), # Create example data x2 = c(0, 0, 0, 1, 1, 0)) data # Print example data # x1 x2 # 1 2 0 # 2 0 0 # 3 7 0 # 4 4 1 # 5 0 1 # 6 5 0
Our example data consists of six rows and two variables x1 and x2. Some of the values of these two rows are zero.
Example: Replace 0 with NA
Replacing 0 by NA in R is a simple task. We simply have to run the following R code:
data[data == 0] <- NA # Replace 0 with NA data # Print updated data # x1 x2 # 1 2 NA # 2 NA NA # 3 7 NA # 4 4 1 # 5 NA 1 # 6 5 NA
As you can see based on the RStudio console output, we replaced all 0 values with NA values. Note that we could apply the same code to a vector or a single data frame column.
Video, Further Resources & Summary
Do you want to learn more about the manipulation of data in R? Then you could have a look at the following video of my YouTube channel. I show the R code of this tutorial in the video.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
In addition, you might want to have a look at the other tutorials on https://statisticsglobe.com/. You can find a selection of tutorials below:
- How to Replace NA with 0
- What are
Values? - str_replace_na Function in R
- is.na Function
- Replace NA with Last Observed Value
- Remove All-NA Columns from Data Frame
- Count NA Values in R
- Find Missing Values
- The R Programming Language
This article explained how to change zeros in a data frame or vector to not available values in the R programming language. Tell me about it in the comments section, in case you have any further questions and/or comments.
Statistics Globe Newsletter
4 Comments. Leave new
But what if I only want to change the 0’s to NA in one column only of my data set?
Hey Jessie,
You may access the values of a single column using the $ operator. Please try the following R code:
Regards
Joachim
I like the answer, and it works, but I’m here searching for this right now because I have a string of magrittr pipes ‘%>%’ written, and I have to break out of it to use that convention above. I have a column in a dataframe where I have NAs that will stay NA, and other values that I want to be NAs.
This FAILS:
But this works (arbitrary 5 instead of NA):
Any thoughts on how to make the first case work without breaking off the pipe chain?
Hey Scott,
Thank you, glad you found the tutorial helpful!
I’m not sure why this code doesn’t work for you. However, would it be an alternative to use the Base R ifelse() function instead of the dplyr if_else function?
For example:
Regards,
Joachim