Skip to content
Prev 389019 / 398506 Next

How to globally convert NaN to NA in dataframe?

It seems like you might've missed one more thing, you need the brackets
next to 'x' to get it to work.


x[] <- lapply(x, function(xx) {
    xx[is.nan(xx)] <- NA_real_
    xx
})

is different from

x <- lapply(x, function(xx) {
    xx[is.nan(xx)] <- NA_real_
    xx
})

Also, if all of your data is numeric, it might be better to convert to a
matrix before doing your calculations. For example:

x <- as.matrix(x)
x[is.nan(x)] <- NA_real_

I'd also suggest this same solution for the other question you posted,

x[x == 0] <- NA

On Thu, Sep 2, 2021 at 10:01 AM Luigi Marongiu <marongiu.luigi at gmail.com>
wrote: