Hello, I have some NaN values in some elements of a dataframe that I would like to convert to NA. The command `df1$col[is.nan(df1$col)]<-NA` allows to work column-wise. Is there an alternative for the global modification at once of all instances? I have seen from https://stackoverflow.com/questions/18142117/how-to-replace-nan-value-with-zero-in-a-huge-data-frame/18143097#18143097 that once could use: ``` is.nan.data.frame <- function(x) do.call(cbind, lapply(x, is.nan)) data123[is.nan(data123)] <- 0 ``` replacing o with NA, but I got ``` str(df)> logi NA``` when modifying my dataframe df. What would be the correct syntax? Thank you -- Best regards, Luigi
Hi what about data[sapply(data, is.nan)] <- NA Cheers Petr> -----Original Message----- > From: R-help <r-help-bounces at r-project.org> On Behalf Of Luigi Marongiu > Sent: Thursday, September 2, 2021 3:18 PM > To: r-help <r-help at r-project.org> > Subject: [R] How to globally convert NaN to NA in dataframe? > > Hello, > I have some NaN values in some elements of a dataframe that I would liketo> convert to NA. > The command `df1$col[is.nan(df1$col)]<-NA` allows to work column-wise. > Is there an alternative for the global modification at once of allinstances?> I have seen from > https://stackoverflow.com/questions/18142117/how-to-replace-nan-value- > with-zero-in-a-huge-data-frame/18143097#18143097 > that once could use: > ``` > > is.nan.data.frame <- function(x) > do.call(cbind, lapply(x, is.nan)) > > data123[is.nan(data123)] <- 0 > ``` > replacing o with NA, but I got > ``` > str(df) > > logi NA > ``` > when modifying my dataframe df. > What would be the correct syntax? > Thank you > > > > -- > Best regards, > Luigi > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.
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:> Hello, > I have some NaN values in some elements of a dataframe that I would > like to convert to NA. > The command `df1$col[is.nan(df1$col)]<-NA` allows to work column-wise. > Is there an alternative for the global modification at once of all > instances? > I have seen from > > https://stackoverflow.com/questions/18142117/how-to-replace-nan-value-with-zero-in-a-huge-data-frame/18143097#18143097 > that once could use: > ``` > > is.nan.data.frame <- function(x) > do.call(cbind, lapply(x, is.nan)) > > data123[is.nan(data123)] <- 0 > ``` > replacing o with NA, but I got > ``` > str(df) > > logi NA > ``` > when modifying my dataframe df. > What would be the correct syntax? > Thank you > > > > -- > Best regards, > Luigi > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]