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

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

Build an Image Segmentation Model using Amazon SageMaker
In this Machine Learning Project, you will learn to implement the UNet Architecture and build an Image Segmentation Model using Amazon SageMaker

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.

Build a Speech-Text Transcriptor with Nvidia Quartznet Model
In this Deep Learning Project, you will leverage transfer learning from Nvidia QuartzNet pre-trained models to develop a speech-to-text transcriptor.

Skip Gram Model Python Implementation for Word Embeddings
Skip-Gram Model word2vec Example -Learn how to implement the skip gram algorithm in NLP for word embeddings on a set of documents.

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.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Build a Churn Prediction Model using Ensemble Learning
Learn how to build ensemble machine learning models like Random Forest, Adaboost, and Gradient Boosting for Customer Churn Prediction using Python

Learn Hyperparameter Tuning for Neural Networks with PyTorch
In this Deep Learning Project, you will learn how to optimally tune the hyperparameters (learning rate, epochs, dropout, early stopping) of a neural network model in PyTorch to improve model performance.

Learn to Build an End-to-End Machine Learning Pipeline - Part 1
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, addressing a major challenge in the logistics industry.

Time Series Project to Build a Multiple Linear Regression Model
Learn to build a Multiple linear regression model in Python on Time Series Data