【问题标题】:R - convert Nan to NA [duplicate]R - 将 Nan 转换为 NA [重复]
【发布时间】:2019-02-28 15:03:43
【问题描述】:

我有一个在某些列中包含 Nans 的数据框。如何将所有列中的 nan 转换为 NA?

我试过df[is.nan(df)]<-NA,但它显示了这个错误:

Error in is.nan(df) : default method not implemented for type 'list'

【问题讨论】:

    标签: r nan na


    【解决方案1】:

    如果需要,您可以这样做,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
    

    看起来有点奇怪,因为你正在创建一个你没有调用的函数,它被称为Method Dispatch,请参阅上面答案中@Hong-Ooi 的评论。

    【讨论】:

      猜你喜欢
      • 2012-03-25
      • 2020-01-07
      • 2019-09-10
      • 2018-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-01
      相关资源
      最近更新 更多