na_if R Function of dplyr Package (2 Examples) | Convert Value to NA
In this article you’ll learn how to replace NA values with the na_if function of the dplyr add-on package in the R programming language.
The article will contain this:
- Example 1: Apply na_if Function to Vector
- Example 2: Apply na_if Function to Data Frame or Tibble
- Video, Further Resources & Summary
Let’s jump right to the examples.
Example 1: Apply na_if Function to Vector
As a first step, we need to install and load the dplyr package to R:
install.packages("dplyr") # Install dplyr add-on package library("dplyr") # Load dplyr add-on package
Furthermore, we have to create an example vector, to which we can apply the na_if function later on:
x <- 1:10 # Create example vector x # Print example vector # 1 2 3 4 5 6 7 8 9 10
Now, we can use the na_if function to replace a certain value of our example vector with NA:
na_if(x, 5) # Apply na_if to vector # 1 2 3 4 NA 6 7 8 9 10
As you can see based on the previous R code and the output of the RStudio console, we replaced the value 5 of our vector with NA.
Example 2: Apply na_if Function to Data Frame or Tibble
We can also use the na_if command to replace certain values of a data frame or tibble with NA. Let’s create an example data frame first:
data <- data.frame(x1 = 1:5, # Create example data x2 = 5:9) data # Print example data # x1 x2 # 1 1 5 # 2 2 6 # 3 3 7 # 4 4 8 # 5 5 9
Our data frame contains five rows and two numeric variables. Now, we can apply the na_if function to this data frame as shown below:
na_if(data, 5) # Apply na_if to data frame # x1 x2 # 1 1 NA # 2 2 6 # 3 3 7 # 4 4 8 # 5 NA 9
The previous R syntax replaced each 5 in our data set with NA.
Video, Further Resources & Summary
Have a look at the following video that I have published on my YouTube channel. I’m explaining further functions of the dplyr package in the video:
Furthermore, I can recommend to have a look at some of the other articles of my website. You can find some tutorials here:
- Replace NA with 0
- Replace NA with Last Observed Value
- str_replace_na Function in R
- Introduction to dplyr
- R Functions List (+ Examples)
- The R Programming Language
In summary: In this tutorial you learned how to convert values to NA with the dplyr package in the R programming language. If you have further questions or comments, please let me know in the comments below.
Subscribe to the Statistics Globe Newsletter
Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.
Thank you!
Welcome to the Statistics Globe newsletter. From now on, I’ll send you regular emails about statistics, data science, AI, and programming with R and Python.
I’m Joachim Schork. On this website, I provide statistics tutorials as well as code in Python and R programming.
Statistics Globe Newsletter
Get regular updates on the latest tutorials, offers & news at Statistics Globe. I hate spam & you may opt out anytime: Privacy Policy.
Thank you!
Please check your email inbox and click the confirmation link to complete your subscription. If you don’t see the email within a few minutes, please also check your spam/junk folder.






