如何在巨大的数据框中用零替换 NaN 值? [英] How to replace NaN value with zero in a huge data frame?

查看:36
本文介绍了如何在巨大的数据框中用零替换 NaN 值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用以下脚本将 NaN 值替换为零:

I tried to replace NaN values with zeros using the following script:

rapply( data123, f=function(x) ifelse(is.nan(x),0,x), how="replace" )
# [31]   0.00000000  -0.67994832   0.50287454   0.63979527   1.48410571  -2.90402836

NaN 值显示为零,但是当我输入数据框的名称并尝试查看它时,该值仍然是 NaN.

The NaN value was showing to be zero but when I typed in the name of the data frame and tried to review it, the value was still remaining NaN.

data123$contri_us
# [31]          NaN  -0.67994832   0.50287454   0.63979527   1.48410571  -2.90402836

我不确定 rapply 命令是否实际在数据框中应用了调整,或者只是按照所示替换了值.

I am not sure whether the rapply command was actually applying the adjustment in the data frame, or just replaced the value as per shown.

知道如何将 NaN 值实际更改为零吗?

Any idea how to actually change the NaN value to zero?

推荐答案

is.na<不同,is.nan 似乎实际上没有数据帧的方法/代码>.所以,让我们解决这个问题!

It would seem that is.nan doesn't actually have a method for data frames, unlike is.na. So, let's fix that!

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

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

这篇关于如何在巨大的数据框中用零替换 NaN 值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆