Home > Software engineering >  What is the best way to remove infs or NaNs in R?
What is the best way to remove infs or NaNs in R?

Time:04-06

I'm having some trouble with infs or NaNs in my files. What is the best way to remove them using R?

CodePudding user response:

If you're trying to get rid of NaN and Inf of -Inf, you're better off using is.finite. In the context of a data frame, you could eliminate the offending rows with:

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
  dat <- data.frame(
   x1 = c(1, 2, NA, NaN, 3, Inf),
   x2 = c(1,2,3,4, NaN, -Inf)
 )
dat
#>    x1   x2
#> 1   1    1
#> 2   2    2
#> 3  NA    3
#> 4 NaN    4
#> 5   3  NaN
#> 6 Inf -Inf
dat %>% filter(if_all(everything(), is.finite))
#>   x1 x2
#> 1  1  1
#> 2  2  2

Created on 2022-04-05 by the reprex package (v2.0.1)

Note that !is.na() and !is.nan() don't solve the whole problem:

dat %>% filter(if_all(everything(), ~!is.nan(.x)))
#>    x1   x2
#> 1   1    1
#> 2   2    2
#> 3  NA    3
#> 4 Inf -Inf
dat %>% filter(if_all(everything(), ~!is.na(.x)))
#>    x1   x2
#> 1   1    1
#> 2   2    2
#> 3 Inf -Inf

CodePudding user response:

You can use !is.nan(). Here is an example:

x1<-sample(1:50, 10)
x2<-sample(c(0, 5,4), 10, replace=TRUE) 
x3<-x2*0.5
x4<-x1/x2 #will generate Inf because we are dividing by 0 
x5<-x1/x3 #will generate Inf because we are dividing by 0
x6<-x5-x4 #will generate NaNs because we are subtracting Inf from Inf
x7<-x6[!is.na(x6)]#Will remove all NaN elements
  • Related