How to Handle NaN Values in R (With Examples) - Statology (2024)

Posted on by Zach

In R, NaN stands for Not a Number.

Typically NaN values occur when you attempt to perform some calculation that results in an invalid result.

For example, dividing by zero or calculating the log of a negative number both produce NaN values:

#attempt to divide by zero0 / 0[1] NaN#attempt to calculate log of negative valuelog(-12)[1] NaN

Note that NaN values are different from NA values, which simply represent missing values.

You can use the following methods to handle NaN values in R:

#identify positions in vector with NaN valueswhich(is.nan(x))#count total NaN values in vectorsum(is.nan(x)) #remove NaN values in vectorx_new <- x[!is.nan(x)]#replace NaN values in vectorx[is.nan(x)] <- 0 

The following examples show how to use each of these methods in practice.

Example 1: Identify Positions in Vector with NaN Values

The following code shows how to identify the positions in a vector that contain NaN values:

#create vector with some NaN valuesx <- c(1, NaN, 12, NaN, 50, 30)#identify positions with NaN valueswhich(is.nan(x))[1] 2 4

From the output we can see that the elements in positions 2 and 4 in the vector are NaN values.

Example 2: Count Total NaN Values in Vector

The following code shows how to count the total number of NaN values in a vector in R:

#create vector with some NaN valuesx <- c(1, NaN, 12, NaN, 50, 30)#identify positions with NaN valuessum(is.nan(x))[1] 2

From the output we can see that there are 2 total NaN values in the vector.

Example 3: Remove NaN Values in Vector

The following code shows how to create a new vector that has the NaN values removed from the original vector:

#create vector with some NaN valuesx <- c(1, NaN, 12, NaN, 50, 30)#define new vector with NaN values removedx_new <- x[!is.nan(x)]#view new vectorx_new[1] 1 12 50 30

Notice that both NaN values have been removed from the vector.

Example 4: Replace NaN Values in Vector

The following code shows how to replace NaN values in a vector with zeros:

#create vector with some NaN valuesx <- c(1, NaN, 12, NaN, 50, 30)#replace NaN values with zerox[is.nan(x)] <- 0#view updated vectorx[1] 1 0 12 0 50 30

Notice that both NaN values have been replaced by zeros in the vector.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Interpolate Missing Values in R
How to Find and Count Missing Values in R
How to Use “Is Not NA” in R

How to Handle NaN Values in R (With Examples) - Statology (2024)
Top Articles
Top 5 Evil Witches — Horror Bound
Capitalization
Mvd Eagle Ranch Appointment
Gasbuddy Joliet
Lkq Pull-A-Part
Davaba19
Endicott Final Exam Schedule Fall 2023
Tiraj Rapid New York Midi
Generalausschreibung - The Race Days Stuttgart
Scholar Dollar Nmsu
Craigslist Shelves
Telegram Voyeur
Magicseaweed Capitola
Convert liters to quarts
Are Crazyjamjam Leaks Real or Fake?
Best Internists In Ft-Lauderdale
Ups Store Pineville La
Elmira Star Gazette Obit
Cn/As Archives
Www.publicsurplus.com Motor Pool
Insulated Dancing Insoles
Panic! At The Disco - Spotify Top Songs
SF bay area cars & trucks "chevrolet 50" - craigslist
Aunt Nettes Menu
Rolling-Embers Reviews
Shellys Earth Materials
2005 Volvo XC 70 XC90 V70 SUV Wagon for sale by owner - Banning, CA - craigslist
Hanging Hyena 4X4
Family Naturist Contest
Kate Spade Outlet Altoona
Acadis Portal Missouri
Diablo 3 Metascore
Tények este teljes adás, 2024. április 26., péntek
Psalm 136 Nkjv
Strip Clubs In Hayward Ca
Vogler Funeral Home At Forsyth Memorial Park
Transformers Movie Wiki
Decree Of Spite Poe
Melanie, Singer Who Performed at Woodstock and Topped Charts With ‘Brand New Key,’ Dies at 76
Cashtapp Atm Near Me
Puppies For Sale in Netherlands (98) | Petzlover
Dom Tradingview
Craigslist Boats For Sale By Owner Sacramento
Top Dog Boarding in The Hague with Best Prices on PetBacker
Rydell on LinkedIn: STARTING TODAY you no longer have to wait in a long line to get your oil…
Urgent Care Pelham Nh
Left Periprosthetic Femur Fracture Icd 10
2022 Basketball 247
Computer Repair Arboretum North Carolina
Umn Biology
Great Clips Fremont Ohio
Southwest Airlines Departures Atlanta
Latest Posts
Article information

Author: Nathanael Baumbach

Last Updated:

Views: 6301

Rating: 4.4 / 5 (55 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Nathanael Baumbach

Birthday: 1998-12-02

Address: Apt. 829 751 Glover View, West Orlando, IN 22436

Phone: +901025288581

Job: Internal IT Coordinator

Hobby: Gunsmithing, Motor sports, Flying, Skiing, Hooping, Lego building, Ice skating

Introduction: My name is Nathanael Baumbach, I am a fantastic, nice, victorious, brave, healthy, cute, glorious person who loves writing and wants to share my knowledge and understanding with you.