7.7 C
London
Sunday, March 9, 2025
HomeStatistics TutorialRHow to Replace Inf Values with NA in R

How to Replace Inf Values with NA in R

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the following methods to replace Inf values with NA values in R:

Method 1: Replace Inf with NA in Vector

x[is.infinite(x)] 

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

df[sapply(df, is.infinite)] 

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

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

This tutorial explains how to use each method in practice with the following data frame:

#create data frame
df frame(team=c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'),
                 position=c('G', 'G', 'F', 'F', 'G', 'G', 'F', 'F'),
                 points=c(10, 10, 8, 14, 15, 15, 17, 17))

#view data frame
df

  team position points
1    A        G     10
2    A        G     10
3    A        F      8
4    A        F     14
5    B        G     15
6    B        G     15
7    B        F     17
8    B        F     17

Example 1: Replace Inf with NA in Vector

The following code shows how to replace all Inf values with NA values in a vector:

#create vector with some Inf values
x #replace Inf values with NA
x[is.infinite(x)] #view updated vector
x

 [1]  4 12 NA  8 NA  9 12  3 22 NA

Notice that all Inf values from the original vector have been replaced with NA values.

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

The following code shows how to replace Inf values with NA values in every column of a data frame:

#create data frame
df frame(x=c(4, 5, 5, 4, Inf, 8, Inf),
                 y=c(10, Inf, Inf, 3, 5, 5, 8),
                 z=c(Inf, 5, 5, 6, 3, 12, 14))

#view data frame
df

    x   y   z
1   4  10 Inf
2   5 Inf   5
3   5 Inf   5
4   4   3   6
5 Inf   5   3
6   8   5  12
7 Inf   8  14

#replace Inf values with NA values in all columns
df[sapply(df, is.infinite)] #view updated data frame
df

   x  y  z
1  4 10 NA
2  5 NA  5
3  5 NA  5
4  4  3  6
5 NA  5  3
6  8  5 12
7 NA  8 14

Notice that the Inf values in each column of the data frame have been replaced with NA values.

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

The following code shows how to replace Inf values with NA values in specific columns of a data frame:

#create data frame
df frame(x=c(4, 5, 5, 4, Inf, 8, Inf),
                 y=c(10, Inf, Inf, 3, 5, 5, 8),
                 z=c(Inf, 5, 5, 6, 3, 12, 14))

#view data frame
df

    x   y   z
1   4  10 Inf
2   5 Inf   5
3   5 Inf   5
4   4   3   6
5 Inf   5   3
6   8   5  12
7 Inf   8  14

#replace Inf values with NA values in columns 'x' and 'z' only
df[c('x', 'z')][sapply(df[c('x', 'z')], is.infinite)] #view updated data frame
df

   x   y  z
1  4  10 NA
2  5 Inf  5
3  5 Inf  5
4  4   3  6
5 NA   5  3
6  8   5 12
7 NA   8 14

Notice that the Inf values in the ‘x’ and ‘y’ columns have been replaced with NA values.

However, the Inf values in column ‘y’ have remain untouched.

Additional Resources

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

How to Use is.na in R
How to Use na.omit in R
How to Replace Blanks with NA in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories