Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
340 views
in Technique[技术] by (71.8m points)

r - How to replace NaN value with zero in a huge data frame?

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

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

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...