What is the difference between NA and NAN in R?

This recipe explains what is the difference between NA and NAN in R

Recipe Objective

A NAN value in R represents “NOT A NUMBER”, It is basically any numeric calculations with an undefined result, such as ‘0/0’.This exists only in vectors with numeric datatype.

A NA value in R represents "NOT AVAILABLE". This can exist in any sort of numeric or character vectors. It is generally interpreted as missing values.

Even one NAN or NA value in a vector causes difficulty to carry out calculations. Hence, we need to remove or replace it before carrying out any calculation. Before we actually remove or replace them, we need to check whether there is an NAN or NA number in a vector and for this we use is.nan() and is.na() function.

This recipe demonstrates how to find if a vector has NAN or/and NA values.

​Should I Learn Python or R for Data Science? Unlock the Answer

Step 1: Creating 2 vectors

We create two vectors, one with NAN values and the other with NA.

a = c(2,5,8,20,NaN, 35,NaN) ​ b = c(2,5,8,20,75,35,100, NA)

Step 2: Checking for NaN/NA values

We use any() along with is.nan() and is.na() function to check for NaN and NA values.

The function is.nan() is used to check specifically for NaN but is.na() also returns TRUE for NaN.

1. Checking in a

#checking for NaN values in "a" vector any(is.nan(a))

TRUE

#checking for NA values in "a" vector any(is.na(a))

TRUE

Note: Both of the condition has returned TRUE. That means there is NAN or NA values present in "a" vector

2. Checking in b

#checking for NaN values in "b" vector any(is.nan(b))

FALSE

#checking for NA values in "b" vector any(is.na(b))

TRUE

Note: any(is.na()) is True which means there is NA values present in "a" vector nut no NAN values

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

LLM Project to Build and Fine Tune a Large Language Model
In this LLM project for beginners, you will learn to build a knowledge-grounded chatbot using LLM's and learn how to fine tune it.

FEAST Feature Store Example for Scaling Machine Learning
FEAST Feature Store Example- Learn to use FEAST Feature Store to manage, store, and discover features for customer churn prediction machine learning project.

Classification Projects on Machine Learning for Beginners - 2
Learn to implement various ensemble techniques to predict license status for a given business.

Deep Learning Project for Text Detection in Images using Python
CV2 Text Detection Code for Images using Python -Build a CRNN deep learning model to predict the single-line text in a given image.

Build a Autoregressive and Moving Average Time Series Model
In this time series project, you will learn to build Autoregressive and Moving Average Time Series Models to forecast future readings, optimize performance, and harness the power of predictive analytics for sensor data.

OpenCV Project to Master Advanced Computer Vision Concepts
In this OpenCV project, you will learn to implement advanced computer vision concepts and algorithms in OpenCV library using Python.

Build an optimal End-to-End MLOps Pipeline and Deploy on GCP
Learn how to build and deploy an end-to-end optimal MLOps Pipeline for Loan Eligibility Prediction Model in Python on GCP

A/B Testing Approach for Comparing Performance of ML Models
The objective of this project is to compare the performance of BERT and DistilBERT models for building an efficient Question and Answering system. Using A/B testing approach, we explore the effectiveness and efficiency of both models and determine which one is better suited for Q&A tasks.

Learn Object Tracking (SOT, MOT) using OpenCV and Python
Get Started with Object Tracking using OpenCV and Python - Learn to implement Multiple Instance Learning Tracker (MIL) algorithm, Generic Object Tracking Using Regression Networks Tracker (GOTURN) algorithm, Kernelized Correlation Filters Tracker (KCF) algorithm, Tracking, Learning, Detection Tracker (TLD) algorithm for single and multiple object tracking from various video clips.

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.