r - R中使用lappy时如何处理NA

标签 r if-statement na

我有一个数据框err,由 796 行和 54432 列组成

我必须检查值不超过 20 和 -20 的列。

这是我的方法:

do.call(cbind, (lapply(err, function(x) if((all(x<20)  & all(x>-20))) return(x) )))

我在所有列中都有 NA 值,并且在我得到之后

Error in if ((all(x < 20) & all(x > -20))) return(x) :
  missing value where TRUE/FALSE needed

我使用 !is.na 将命令更新为:

 do.call(cbind, (lapply(err, function(x) if(!is.na(all(x<20)  & all(x>-20))) return(x) )))

但在这种情况下,所有列都会被报告,并且过滤器不起作用。

有什么帮助吗?

最佳答案

由于我没有示例 df,请检查这是否适合您:

do.call("cbind", lapply(err, function(x) if(min(x, na.rm=T) > -20  & max(x, na.rm=T) < 20) return(x) ))

关于r - R中使用lappy时如何处理NA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30805439/

相关文章:

r - 如何更改 r shinydashboard 中数据表中列的对齐方式?

r - 分组区间的科学记数法问题

c++ - If 语句 - 不正确的评估

r - 用条件子集向量(不包括 NA)

r - 错误 : 'NA' does not exist in current working directory (Webscraping)

r - 如何在R中完成行名称?

r - R optim 实现中的 Brent 方法始终返回相同的局部最小值

c++ - C++中的'rand'函数?

c++ - 使用 if(1 || !Foo()) 有什么理由吗?

python - 用 NA 值填充 dict 以允许转换为 pandas 数据帧