R cor returns NaN sometimes

我只是一个虾纸丫 提交于 2019-12-01 17:38:47

Several comments and notes:

  • Nobody is able to reproduce your problem
  • it cannot be a problem with the 120.csv file which is all fine.
  • Really, using another use=".." option is just a workaround
  • The underlying C code in R's sources uses ISNAN(.) everywhere for detecting if a value is NA or NaN and this in term goes to your (system internal) C library's isnan(.) function.
  • you (and only you) sometimes get NaN because ISNAN(.) does not return "true" in some cases it should, and the floating point arithmetic computes with the NAs and correctly returns NaN's.

As an "old" R core member, I can assure you that ISNAN(.) is used in many many fundamental places inside R's core computations, and the observations that for you it sometimes seems to not detect NA/NaN so that they propagate into the result is very problematic. As Duncan Murdoch has said, answering your R bug report https://bugs.r-project.org/bugzilla/show_bug.cgi?id=16058 this must be a problem with your specific "system" one way or the other... As I assume you simply downloaded R from CRAN, also for R 3.1.2, and you still see the problem, I'd tend to say that your system software (Windows) or - less likely - your hardware must be slightly broken / corrupt.

Giovanni, It its working fine for me. Maybe you should try to change the parameters of cor to use = "complete.obs" and see if that helps. Also you should check your CSV file weather they are corrupted or not.

I hope it helps.

Puzzled as I was, I started playing around with the various options on the use= argument of cor. I found that I can have consistent results if I use cor(t$nitrate,t$sulfate,use="pairwise.complete.obs"):

> x<-NULL; for (i in 1:100){x<-c(x,cor(t$nitrate,t$sulfate,use="pairwise.complete.obs"))};x
  [1] 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441
 [12] 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441
 [23] 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441
 [34] 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441
 [45] 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441
 [56] 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441
 [67] 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441
 [78] 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441
 [89] 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441 0.2967441
[100] 0.2967441

I still don't get why other options of use passed by other users didn't cause the weird behavior.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!