What is the difference between NA and NAN in R?

What is the difference between NA and NAN in R?

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.

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))

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


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))

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


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

Relevant Projects

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

Build a Similar Images Finder with Python, Keras, and Tensorflow
Build your own image similarity application using Python to search and find images of products that are similar to any given product. You will implement the K-Nearest Neighbor algorithm to find products with maximum similarity.

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.