In R, I have an operation which creates some Inf values when I transform a dataframe.
I would like to turn these Inf values into NA values. The code I have is slow for large data, is there a faster way of doing this?
Say I have the following dataframe:
dat <- data.frame(a=c(1, Inf), b=c(Inf, 3), d=c("a","b"))
The following works in a single case:
dat[,1][is.infinite(dat[,1])] = NA
So I generalized it with following loop
cf_DFinf2NA <- function(x)
{
for (i in 1:ncol(x)){
x[,i][is.infinite(x[,i])] = NA
}
return(x)
}
But I don’t think that this is really using the power of R.
Option 1
Use the fact that a
data.frameis a list of columns, then usedo.callto recreate adata.frame.Option 2 —
data.tableYou could use
data.tableandset. This avoids some internal copying.Or using column numbers (possibly faster if there are a lot of columns):
Timings
data.tableis the quickest. Usingsapplyslows things down noticeably.