I have a single vector of flow data (29 data) and a 3D matrix data(360*180*29)
i want to find the correlation between single vector and 3D vector. The correlation matrix will have a size of 360*180.
> str(ScottsCk_flow_1981_2010_JJA)
num [1:29] 0.151 0.644 0.996 0.658 1.702 ...
> str(ssta_winter)
num [1:360, 1:180, 1:29] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
> summary(ssta_winter)
Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
-2.8 -0.2 0.1 0.2 0.6 6.0 596849.0
This above is the structure of the vector and 3D matrix. 3D matrix has many values as Null.
> for (i in 1:360) {
+ for(j in 1:180){
+ cor_ScottsCk_SF_SST_JJA[i,j] = cor(ScottsCk_flow_1981_2010_JJA,ssta_winter[i,j,])
+ }
+ }
There were 50 or more warnings (use warnings() to see the first 50)
This part of code above is the code to find correlation. But it gives waring as
> warnings()
Warning messages:
1: In cor(ScottsCk_flow_1981_2010_JJA, ssta_winter[i, j, ... :
the standard deviation is zero
2: In cor(ScottsCk_flow_1981_2010_JJA, ssta_winter[i, j, ... :
the standard deviation is zero
3: In cor(ScottsCk_flow_1981_2010_JJA, ssta_winter[i, j, ... :
the standard deviation is zero
4: In cor(ScottsCk_flow_1981_2010_JJA, ssta_winter[i, j, ... :
the standard deviation is zero
5: In cor(ScottsCk_flow_1981_2010_JJA, ssta_winter[i, j, ... :
the standard deviation is zero
also, the result of the correlation matrix is all NULL. how did this happen?
> str(cor_ScottsCk_SF_SST_JJA)
num [1:360, 1:180] NA NA NA NA NA NA NA NA NA NA ...
I have used exact same code bfr with 350 flow vector and 360*180*350 matrix.
This code works perfectly.
A few thoughts.
First, by using
apply(), you can replace that nested loop with something like this:Second, it appears that >31% (
596849/(360*180*29)) of the points inssta_winterareNaNor (possibly)NA_real_. Given the return value of a correlation calculated on vectors that contain even a singleNaN,isn’t it likely that all those
NaNs are causingcor_ScottsCk_SF_SST_JJAto be filled withNAs?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 theNaNs: as the following shows, R doesn’t complain about standard deviations of 0 whenNaNare involved. (Quite sensibly too, since you can’t calculate standard deviations for undefined numbers):