How to find if a vector has nan value in it in R

This recipe helps you find if a vector has nan value in it in R

Recipe Objective

An NAN value in R represents “NOT A NUMBER”, It is basically any numeric calculation with an undefined result, such as ‘0/0’. This exists only in vectors with numeric datatype. Even one NAN 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 number in a vector and for this we use is.nan() function.

This recipe demonstrates how to find if a vector has NAN values.

Build Expedia Hotel Recommendation System using Machine Learning

Step 1: Creating 2 vectors

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

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

Step 2: Checking for NaN values

we use any() and is.nan() function to check for NaN values. It returns a logical value TRUE even if 1 Nan value is present.

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

TRUE

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

FALSE

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Tensorflow Transfer Learning Model for Image Classification
Image Classification Project - Build an Image Classification Model on a Dataset of T-Shirt Images for Binary Classification

Personalized Medicine: Redefining Cancer Treatment
In this Personalized Medicine Machine Learning Project you will learn to classify genetic mutations on the basis of medical literature into 9 classes.

Llama2 Project for MetaData Generation using FAISS and RAGs
In this LLM Llama2 Project, you will automate metadata generation using Llama2, RAGs, and AWS to reduce manual efforts.

Ola Bike Rides Request Demand Forecast
Given big data at taxi service (ride-hailing) i.e. OLA, you will learn multi-step time series forecasting and clustering with Mini-Batch K-means Algorithm on geospatial data to predict future ride requests for a particular region at a given time.

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.

Stock Price Prediction Project using LSTM and RNN
Learn how to predict stock prices using RNN and LSTM models. Understand deep learning concepts and apply them to real-world financial data for accurate forecasting.

Multi-Class Text Classification with Deep Learning using BERT
In this deep learning project, you will implement one of the most popular state of the art Transformer models, BERT for Multi-Class Text Classification

Build Real Estate Price Prediction Model with NLP and FastAPI
In this Real Estate Price Prediction Project, you will learn to build a real estate price prediction machine learning model and deploy it on Heroku using FastAPI Framework.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

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.