title: R cor returns NaN sometimes
body: <p>I've been working on some data, available here: <a href="https://www.dropbox.com/s/i0ou1rywjvkuwkq/120.csv?dl=0" rel="noreferrer">Dropbox' csv file</a> (please be kind to use it to replicate the error).</p>
  
  <p>When I run the code:</p>
  
  <pre><code>t&lt;-read.csv("120.csv")
  x&lt;-NULL
  for (i in 1:100){
    x&lt;-c(x,cor(t$nitrate,t$sulfate,use="na.or.complete"))
  }
  sum(is.nan(x))
  </code></pre>
  
  <p>I get random values of the last expression, usually around 55 to 60. I expect <code>cor</code> to give repetible results, so I expect <code>x</code> to be a vector of length=100 made of identical values. See, for example, the output of two independent runs:</p>
  
  <pre><code>&gt; x&lt;-NULL; for (i in 1:100){x&lt;-c(x,cor(t$nitrate,t$sulfate,use="na.or.complete"))}
  &gt; sum(is.nan(x))
  [1] 62
  &gt; head(x,10)
   [1]       NaN       NaN 0.2967441       NaN 0.2967441       NaN       NaN       NaN
   [9] 0.2967441       NaN
  &gt; x&lt;-NULL; for (i in 1:100){x&lt;-c(x,cor(t$nitrate,t$sulfate,use="na.or.complete"))}
  &gt; sum(is.nan(x))
  [1] 52
  &gt; head(x,10)
   [1] 0.2967441       NaN       NaN       NaN       NaN 0.2967441 0.2967441       NaN
   [9] 0.2967441 0.2967441
  &gt; 
  </code></pre>
  
  <p>I wonder if I'm doing something wrong here, or if it's a[n] [un]known bug. If that's the case, I appreciate if somebody more profficient than I helps me reporting it to CRAN.</p>
  
  <p>I read a very old (2001) post where the same behavior was exhibited by cor.test (see <a href="http://grokbase.com/t/r/r-help/019ny8n653/r-cor-test-x-x-sometimes-produce-nan" rel="noreferrer">cor.test produces NaN sometimes</a>.</p>
  
  <p>I appreciate your kind explanations, as I'm a nOOb to R. Thanks!</p>
  
  <p>Per Ben's Suggestion:</p>
  
  <pre><code>&gt; sessionInfo()
  R version 3.1.1 (2014-07-10)
  Platform: x86_64-w64-mingw32/x64 (64-bit)
  
  locale:
  [1] LC_COLLATE=Spanish_Colombia.1252  LC_CTYPE=Spanish_Colombia.1252    LC_MONETARY=Spanish_Colombia.1252 LC_NUMERIC=C                     
  [5] LC_TIME=Spanish_Colombia.1252    
  
  attached base packages:
  [1] stats     graphics  grDevices utils     datasets  methods   base     
  
  other attached packages:
   [1] stringr_0.6.2     digest_0.6.4      RCurl_1.95-4.3    bitops_1.0-6      qpcR_1.4-0        Matrix_1.1-4      robustbase_0.91-1 rgl_0.95.1157    
   [9] minpack.lm_1.1-8  MASS_7.3-35       plyr_1.8.1        swirl_2.2.16      ggplot2_1.0.0     lattice_0.20-29  
  
  loaded via a namespace (and not attached):
   [1] colorspace_1.2-4 DEoptimR_1.0-2   grid_3.1.1       gtable_0.1.2     httr_0.5         labeling_0.3     munsell_0.4.2    proto_0.3-10     Rcpp_0.11.3     
  [10] reshape2_1.4     scales_0.2.4     testthat_0.9.1   tools_3.1.1      yaml_2.1.13  
  </code></pre>
  
  <p>Results of find("cor"):</p>
  
  <pre><code>&gt; find("cor")
  [1] "package:stats"
  </code></pre>
  
  <p>---------- ### Second Edit ###--------</p>
  
  <p>I restarted the session (I didn't quite find how to pass the --vanilla argument. I'm using Rstudio), and this is the new sessionInfo:</p>
  
  <pre><code>&gt; sessionInfo()
  R version 3.1.1 (2014-07-10)
  Platform: x86_64-w64-mingw32/x64 (64-bit)
  
  locale:
  [1] LC_COLLATE=Spanish_Colombia.1252  LC_CTYPE=Spanish_Colombia.1252    LC_MONETARY=Spanish_Colombia.1252 LC_NUMERIC=C                     
  [5] LC_TIME=Spanish_Colombia.1252    
  
  attached base packages:
  [1] stats     graphics  grDevices utils     datasets  methods   base     
  
  loaded via a namespace (and not attached):
  [1] tools_3.1.1
  </code></pre>
  
  <p>I run the commands again in the new session, and still get sum(is.nan(x))=52 :(</p>
  
  <p>Just in case it's useful:</p>
  
  <pre><code>&gt; cor
  function (x, y = NULL, use = "everything", method = c("pearson", 
      "kendall", "spearman")) 
  {
      na.method &lt;- pmatch(use, c("all.obs", "complete.obs", "pairwise.complete.obs", 
          "everything", "na.or.complete"))
      if (is.na(na.method)) 
          stop("invalid 'use' argument")
      method &lt;- match.arg(method)
      if (is.data.frame(y)) 
          y &lt;- as.matrix(y)
      if (is.data.frame(x)) 
          x &lt;- as.matrix(x)
      if (!is.matrix(x) &amp;&amp; is.null(y)) 
          stop("supply both 'x' and 'y' or a matrix-like 'x'")
      if (!(is.numeric(x) || is.logical(x))) 
          stop("'x' must be numeric")
      stopifnot(is.atomic(x))
      if (!is.null(y)) {
          if (!(is.numeric(y) || is.logical(y))) 
              stop("'y' must be numeric")
          stopifnot(is.atomic(y))
      }
      Rank &lt;- function(u) {
          if (length(u) == 0L) 
              u
          else if (is.matrix(u)) {
              if (nrow(u) &gt; 1L) 
                  apply(u, 2L, rank, na.last = "keep")
              else row(u)
          }
          else rank(u, na.last = "keep")
      }
      if (method == "pearson") 
          .Call(C_cor, x, y, na.method, FALSE)
      else if (na.method %in% c(2L, 5L)) {
          if (is.null(y)) {
              .Call(C_cor, Rank(na.omit(x)), NULL, na.method, method == 
                  "kendall")
          }
          else {
              nas &lt;- attr(na.omit(cbind(x, y)), "na.action")
              dropNA &lt;- function(x, nas) {
                  if (length(nas)) {
                    if (is.matrix(x)) 
                      x[-nas, , drop = FALSE]
                    else x[-nas]
                  }
                  else x
              }
              .Call(C_cor, Rank(dropNA(x, nas)), Rank(dropNA(y, 
                  nas)), na.method, method == "kendall")
          }
      }
      else if (na.method != 3L) {
          x &lt;- Rank(x)
          if (!is.null(y)) 
              y &lt;- Rank(y)
          .Call(C_cor, x, y, na.method, method == "kendall")
      }
      else {
          if (is.null(y)) {
              ncy &lt;- ncx &lt;- ncol(x)
              if (ncx == 0) 
                  stop("'x' is empty")
              r &lt;- matrix(0, nrow = ncx, ncol = ncy)
              for (i in seq_len(ncx)) {
                  for (j in seq_len(i)) {
                    x2 &lt;- x[, i]
                    y2 &lt;- x[, j]
                    ok &lt;- complete.cases(x2, y2)
                    x2 &lt;- rank(x2[ok])
                    y2 &lt;- rank(y2[ok])
                    r[i, j] &lt;- if (any(ok)) 
                      .Call(C_cor, x2, y2, 1L, method == "kendall")
                    else NA
                  }
              }
              r &lt;- r + t(r) - diag(diag(r))
              rownames(r) &lt;- colnames(x)
              colnames(r) &lt;- colnames(x)
              r
          }
          else {
              if (length(x) == 0L || length(y) == 0L) 
                  stop("both 'x' and 'y' must be non-empty")
              matrix_result &lt;- is.matrix(x) || is.matrix(y)
              if (!is.matrix(x)) 
                  x &lt;- matrix(x, ncol = 1L)
              if (!is.matrix(y)) 
                  y &lt;- matrix(y, ncol = 1L)
              ncx &lt;- ncol(x)
              ncy &lt;- ncol(y)
              r &lt;- matrix(0, nrow = ncx, ncol = ncy)
              for (i in seq_len(ncx)) {
                  for (j in seq_len(ncy)) {
                    x2 &lt;- x[, i]
                    y2 &lt;- y[, j]
                    ok &lt;- complete.cases(x2, y2)
                    x2 &lt;- rank(x2[ok])
                    y2 &lt;- rank(y2[ok])
                    r[i, j] &lt;- if (any(ok)) 
                      .Call(C_cor, x2, y2, 1L, method == "kendall")
                    else NA
                  }
              }
              rownames(r) &lt;- colnames(x)
              colnames(r) &lt;- colnames(y)
              if (matrix_result) 
                  r
              else drop(r)
          }
      }
  }
  &lt;bytecode: 0x0000000008ce0158&gt;
  &lt;environment: namespace:stats&gt;
  </code></pre>
  
  <p>Thanks again.</p>