How do you replace Inf Values with NA in R?

In R, Inf (infinity) values can be replaced with NA (Not Available) using the is.finite and is.infinite functions. The is.finite function will return a logical vector of TRUE and FALSE values, where TRUE corresponds to finite values in the vector and FALSE corresponds to Inf values. The is.infinite function can then be used to invert the logical vector so that FALSE corresponds to finite values in the vector and TRUE corresponds to Inf values. Finally, the replace function can be used to replace the Inf values with NA.


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.

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

x