R - Warning message: "In cor(...): the standard deviation is zero" R - Warning message: "In cor(...): the standard deviation is zero" r r

R - Warning message: "In cor(...): the standard deviation is zero"


A few thoughts.

First, by using apply(), you can replace that nested loop with something like this:

cor_ScottsCk_SF_SST_JJA <-     apply(ssta_winter, MARGIN = 1:2, FUN = cor, ScottsCk_flow_1981_2010_JJA)

Second, it appears that >31% (596849/(360*180*29)) of the points in ssta_winter are NaN or (possibly) NA_real_. Given the return value of a correlation calculated on vectors that contain even a single NaN,

cor(c(1:3, NaN), c(1:4))# [1] NA

isn't it likely that all those NaNs are causing cor_ScottsCk_SF_SST_JJA to be filled with NAs?

Third, as the warning messages plainly tell you, some of the vectors you are passing to cor() have zero variance. They have nothing to do with the NaNs: as the following shows, R doesn't complain about standard deviations of 0 when NaN are involved. (Quite sensibly too, since you can't calculate standard deviations for undefined numbers):

cor(c(NaN, NaN, NaN, NaN), c(1,1,1,1))# [1] NAcor(c(1,1,1,1), c(1,2,3,4))# [1] NA# Warning message:# In cor(c(1, 1, 1, 1), c(1, 2, 3, 4)) : the standard deviation is zero


This error might also be shown if a column has the same values for all observations. So, you might want to remove those rows.


The following uses library("psych")

partial.r(sd,c("GPA","SAT"),"GRADE1",use = "complete.obs")Warning Message: In cor(data, use = use, method = method) : the standard deviation is zero

sd contains NA for SAT.

partial.r(subset,c("GPA","SAT"),"GRADE1", use = "complete.obs")no warnings

subset has NA's removed