1

I have a dataframe that has Nans in some columns. How do I convert the nans in all columns into NAs?

I've tried df[is.nan(df)]<-NA but it shows this error:

Error in is.nan(df) : default method not implemented for type 'list'
abi
  • 285
  • 1
  • 7
  • 17

1 Answers1

19

You can do this, if you need to, as explained in this answer by @Hong-Ooi:

is.nan.data.frame <- function(x)
  do.call(cbind, lapply(x, is.nan))

df[is.nan(df)] <- NA

Is looks a bit strange, because you are creating a function you are not calling, and this is called Method Dispatch, see the comment by @Hong-Ooi in the answer above.

Axeman
  • 32,068
  • 8
  • 81
  • 94
FvD
  • 3,697
  • 1
  • 35
  • 53