【问题标题】:Removing Nan from a list of dataframe in R [duplicate]从 R 中的数据框列表中删除 Nan [重复]
【发布时间】:2019-11-19 02:40:10
【问题描述】:

我正在尝试将NaN 转换为零。而且,我使用的代码如下

myfiles6 <- lapply(myfiles6, function(x) {x[is.nan(x)] <- 0; x})

但它给出了一个错误

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

有什么解决办法?

【问题讨论】:

  • 正如错误所说,您 is.nan 适用于矢量,可能您需要 lapply(myfiles6, function(dat) {dat[] &lt;- lapply(dat, function(x) replace(x, is.nan(x), 0)); dat})
  • 这行得通,但如果它可以简化我会很高兴,我猜它是一个嵌套函数。
  • 也许你可以使用map(myfiles6, ~ .x %&gt;% mutate_all(~ replace(., is.nan(.), 0))) from tidyverse
  • 谢谢,这看起来很眼熟。

标签: r list nan


【解决方案1】:

尝试使用以下代码:

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

myfile6[is.nan(myfile6)] <- 0

【讨论】:

  • 产生同样的list 错误。
猜你喜欢
  • 2021-06-14
  • 1970-01-01
  • 2018-02-24
  • 2012-11-13
  • 1970-01-01
  • 2019-09-15
  • 2023-03-09
  • 2020-02-16
  • 1970-01-01
相关资源
最近更新 更多