I'm having some trouble with infs or NaNs in my files. What is the best way to remove them using R?
Asked
Active
Viewed 153 times
2 Answers
3
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

DaveArmstrong
- 18,377
- 2
- 13
- 25
0
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

Sean McKenzie
- 707
- 3
- 13
-
I was able to address this issue. But I have another trouble: "FloatingPointError: divide by zero encountered in true_divide". I would like to know how I can address this in r studio? – Mahan Apr 05 '22 at 18:16
-
Do you think you could post a reproducible example of that error? – Sean McKenzie Apr 05 '22 at 18:21
-
divide by zero encountered in true_divide Traceback (most recent call last): File "mtag.py", line 1577, in
mtag(args) File "mtag.py", line 1458, in mtag save_mtag_results(args, res_temp, Zs, N_raw, Fs, mtag_betas, mtag_se, mtag_factor) File "mtag.py", line 875, in save_mtag_results out_df['mtag_beta'] = mtag_betas[:,p] / weights FloatingPointError: divide by zero encountered in true_divide Analysis terminated from error at Tue Apr 5 18:58:21 2022 Total time elapsed: 14.0m:48.79s – Mahan Apr 05 '22 at 18:24 -
By reproducible example, I meant something I can run on my machine to get the same error. Here's a link for a guide on making these. https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Sean McKenzie Apr 05 '22 at 18:55
-