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

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

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.

Loan Default Prediction Project using Explainable AI ML Models
Loan Default Prediction Project that employs sophisticated machine learning models, such as XGBoost and Random Forest and delves deep into the realm of Explainable AI, ensuring every prediction is transparent and understandable.

Build a Review Classification Model using Gated Recurrent Unit
In this Machine Learning project, you will build a classification model in python to classify the reviews of an app on a scale of 1 to 5 using Gated Recurrent Unit.

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.

End-to-End Snowflake Healthcare Analytics Project on AWS-1
In this Snowflake Healthcare Analytics Project, you will leverage Snowflake on AWS to predict patient length of stay (LOS) in hospitals. The prediction of LOS can help in efficient resource allocation, lower the risk of staff/visitor infections, and improve overall hospital functioning.

End-to-End ML Model Monitoring using Airflow and Docker
In this MLOps Project, you will learn to build an end to end pipeline to monitor any changes in the predictive power of model or degradation of data.

Learn to Build a Siamese Neural Network for Image Similarity
In this Deep Learning Project, you will learn how to build a siamese neural network with Keras and Tensorflow for Image Similarity.

Build Regression Models in Python for House Price Prediction
In this Machine Learning Regression project, you will build and evaluate various regression models in Python for house price prediction.

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.

Deploy Transformer-BART Model on Paperspace Cloud
In this MLOps Project you will learn how to deploy a Tranaformer BART Model for Abstractive Text Summarization on Paperspace Private Cloud