Explain the difference between NA and NAN values in R?

This recipe explains what the difference between NA and NAN values 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.

Explore the BERT Variants - ALBERT vs DistilBERT

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

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

Detectron2 Object Detection and Segmentation Example Python
Object Detection using Detectron2 - Build a Dectectron2 model to detect the zones and inhibitions in antibiogram images.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Recommender System Machine Learning Project for Beginners-2
Recommender System Machine Learning Project for Beginners Part 2- Learn how to build a recommender system for market basket analysis using association rule mining.

BERT Text Classification using DistilBERT and ALBERT Models
This Project Explains how to perform Text Classification using ALBERT and DistilBERT

Build a Multi Touch Attribution Machine Learning Model in Python
Identifying the ROI on marketing campaigns is an essential KPI for any business. In this ML project, you will learn to build a Multi Touch Attribution Model in Python to identify the ROI of various marketing efforts and their impact on conversions or sales..

Digit Recognition using CNN for MNIST Dataset in Python
In this deep learning project, you will build a convolutional neural network using MNIST dataset for handwritten digit recognition.

Deep Learning Project for Time Series Forecasting in Python
Deep Learning for Time Series Forecasting in Python -A Hands-On Approach to Build Deep Learning Models (MLP, CNN, LSTM, and a Hybrid Model CNN-LSTM) on Time Series Data.

MLOps Project for a Mask R-CNN on GCP using uWSGI Flask
MLOps on GCP - Solved end-to-end MLOps Project to deploy a Mask RCNN Model for Image Segmentation as a Web Application using uWSGI Flask, Docker, and TensorFlow.

Multilabel Classification Project for Predicting Shipment Modes
Multilabel Classification Project to build a machine learning model that predicts the appropriate mode of transport for each shipment, using a transport dataset with 2000 unique products. The project explores and compares four different approaches to multilabel classification, including naive independent models, classifier chains, natively multilabel models, and multilabel to multiclass approaches.

Learn How to Build a Linear Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple linear regression model in PyTorch to predict the number of days subscribed.