Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • How to use image function in R
    How to use the image function in R R
  • A Side-by-Side Boxplot in R
    A Side-by-Side Boxplot in R: How to Do It R
  • Convert Multiple Columns to Numeric in R
    Convert Multiple Columns to Numeric in R R
  • How to Find Optimal Clusters in R, K-means clustering is one of the most widely used clustering techniques in machine learning.
    How to Find Optimal Clusters in R? R
  • How to Turn Off Scientific Notation in R
    How to Turn Off Scientific Notation in R? R
  • How to put margins on tables or arrays in R?
    How to put margins on tables or arrays in R? R
  • Best Online Course For Statistics
    Free Best Online Course For Statistics Course
  • How to Find Unmatched Records in R
    How to Find Unmatched Records in R R
How to Replace Inf Values with NA in R

How to Replace Inf Values with NA in R

Posted on October 15October 15 By Jim No Comments on How to Replace Inf Values with NA in R
Tweet
Share
Share
Pin

Replace Inf Values with NA in R, you can substitute NA values for Inf values using the techniques listed below:

Method 1: Replace Inf with NA in Vector

x[is.infinite(x)] <- NA

Example 1: Substitute NA for Inf in the Vector

The code below demonstrates how to swap out all Inf values in a vector for NA values.

x <- c(14, 102, Inf, 8, Inf, 9, 12, 3, 202, Inf)
x[is.infinite(x)] <- NA
x
[1] 14 102 NA 8 NA 9 12 3 202 NA

You’ll see that the original vector’s Inf values have all been swapped out for NA values.

How to Find Quartiles in R? – Data Science Tutorials

Method 2: Replace Inf with NA in All Columns of the Data Frame

df[sapply(df, is.infinite)] <- NA

Example 2: Change all columns in the data frame from Inf to NA

In every column of a data frame, Inf values can be changed to NA values using the code below:

Let’s create a data frame

df <- data.frame(x=c(14, 15, 25, 34, Inf, 18, Inf),
                 y=c(10, Inf, Inf, 43, 45, 55, 88),
                 z=c(Inf, 55, 45, 65, 35, 102, 14))
df
   x   y   z
1  14  10 Inf
2  15 Inf  55
3  25 Inf  45
4  34  43  65
5 Inf  45  35
6  18  55 102
7 Inf  88  14

Now we can replace Inf values with NA values in all columns

Algorithm Classifications in Machine Learning – Data Science Tutorials

df[sapply(df, is.infinite)] <- NA
df
  x  y   z
1 14 10  NA
2 15 NA  55
3 25 NA  45
4 34 43  65
5 NA 45  35
6 18 55 102
7 NA 88  14

Observe that NA values have been substituted for the Inf values in each column of the data frame.

Method 3: Replace Inf with NA in Specific Columns of the Data Frame

df[c('col1', 'col2')][sapply(df[c('col1', 'col2')], is.infinite)] <- NA

Example 3: In certain columns of a data frame, replace Inf with NA

The code below demonstrates how to change Inf values in particular columns of a data frame to NA values.

Boosting in Machine Learning:-A Brief Overview (datasciencetut.com)

Now let’s create a data frame

df <- data.frame(x=c(44, 55, 15, 34, Inf, 88, Inf),
                 y=c(110, Inf, Inf, 33, 55, 58, 88),
                 z=c(Inf, 75, 85, 6, 33, 12, 114))
df
   x   y   z
1  44 110 Inf
2  55 Inf  75
3  15 Inf  85
4  34  33   6
5 Inf  55  33
6  88  58  12
7 Inf  88 114

Now replace Inf values with NA values in columns ‘x’ and ‘z’ only

df[c('x', 'z')][sapply(df[c('x', 'z')], is.infinite)] <- NA
df
   x   y   z
1 44 110  NA
2 55 Inf  75
3 15 Inf  85
4 34  33   6
5 NA  55  33
6 88  58  12
7 NA  88 114

The NA values have been substituted for the Inf values in the ‘x’ and ‘y’ columns, as you can see.

The Inf values in column ‘y’ have not changed, nevertheless.

Replace NA with Zero in R – Data Science Tutorials

Tweet
Share
Share
Pin
R

Post navigation

Previous Post: Boosting in Machine Learning:-A Brief Overview
Next Post: Extract patterns in R?

Related Posts

  • Extract patterns in R
    Extract patterns in R? R
  • How to plot categorical data in R
    Plot categorical data in R R
  • Ogive Graph in R
    Ogive Graph in R R
  • Two-Way ANOVA Example in R
    Two-Way ANOVA Example in R-Quick Guide R
  • Boosting in Machine Learning
    Boosting in Machine Learning:-A Brief Overview Machine Learning
  • Is Data Science a Dying Profession
    Is Data Science a Dying Profession? R

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • About Us
  • Contact
  • Disclaimer
  • Guest Blog
  • Privacy Policy
  • YouTube
  • Twitter
  • Facebook
  • Defensive Programming Strategies in R
  • Plot categorical data in R
  • Top Data Modeling Tools for 2023
  • Ogive Graph in R
  • Is R or Python Better for Data Science in Bangalore

Check your inbox or spam folder to confirm your subscription.

  • Data Scientist Career Path Map in Finance
  • Is Python the ideal language for machine learning
  • Convert character string to name class object
  • How to play sound at end of R Script
  • Pattern Searching in R
  • How Do Online Criminals Acquire Sensitive Data
    How Do Online Criminals Acquire Sensitive Data Machine Learning
  • How to Add a title to ggplot2 Plots in R
    How to Add a caption to ggplot2 Plots in R? R
  • How to Group and Summarize Data in R
    How to Group and Summarize Data in R R
  • How to Find Unmatched Records in R
    How to Find Unmatched Records in R R
  • How to Analyze Likert Scale Data
    How to Analyze Likert Scale Data? Statistics
  • one-sample-proportion-test-in-r
    One sample proportion test in R-Complete Guide R
  • How to Use Italic Font in R
    How to Use Italic Font in R R
  • Two-Way ANOVA Example in R
    Two-Way ANOVA Example in R-Quick Guide R

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme