Skip to content
Prev 389012 / 398506 Next

How to globally convert NaN to NA in dataframe?

Hello,


I would use something like:


x <- c(1:5, NaN) |> sample(100, replace = TRUE) |> matrix(10, 10) |>
as.data.frame()
x[] <- lapply(x, function(xx) {
    xx[is.nan(xx)] <- NA_real_
    xx
})


This prevents attributes from being changed in 'x', but accomplishes the
same thing as you have above, I hope this helps!

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