How can I replace infinite values with NA in R?

How can I replace infinite values with NA in R?

In R, infinite values can be replaced with NA (Not Available) using the “is.infinite” function. This function will identify all infinite values and replace them with NA, making the data more manageable for further analysis. This process ensures that the data is accurate and does not affect the results of statistical calculations. By replacing infinite values with NA, users can effectively handle outliers and missing data in their datasets. This method is essential for ensuring the reliability and validity of data in R.

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.

Cite this article

stats writer (2024). How can I replace infinite values with NA in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-replace-infinite-values-with-na-in-r/

stats writer. "How can I replace infinite values with NA in R?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-replace-infinite-values-with-na-in-r/.

stats writer. "How can I replace infinite values with NA in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-replace-infinite-values-with-na-in-r/.

stats writer (2024) 'How can I replace infinite values with NA in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-replace-infinite-values-with-na-in-r/.

[1] stats writer, "How can I replace infinite values with NA in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I replace infinite values with NA in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top