How to Replace Inf Values with NA in R


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)] <- NA

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

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

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

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

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

#create data frame
df <- data.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 <- c(4, 12, Inf, 8, Inf, 9, 12, 3, 22, Inf)

#replace Inf values with NA
x[is.infinite(x)] <- NA

#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 <- data.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)] <- NA

#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 <- data.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)] <- NA

#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

Leave a Reply

Your email address will not be published.