i want to select June, July and August data from the 3D array (ssta_sst, 360*180*362).
the loop works but the output of ssta_winter has the identical values for ssta_winter[,,i].seen below. I have set it up as an array of (360,180,29).
I think the problem is the variable temp, i want to define it as an array first but i don’t know what size it should be (360,180,3) or (360,180,3*29) and how to keep a loop counter in temp when it passes down to finding mean stage?
ssta_winter = array( data=NA, dim = c(360,180,29))
temp = array( data=NA, dim = c(360,180,3))
for (yr in 1982:2010) {
temp <- ssta_sst[,,year_sst==yr & (month_sst>=6 & month_sst<=8)]
for (i in 1:360) {
for (j in 1:180) {
ssta_winter[i,j,] <- mean(temp[i,j,])
}
}
}
> for (i in 1:29){
+ print(ssta_winter[180,166,i])
+ }
[1] 0.2222583
[1] 0.2222583
[1] 0.2222583
[1] 0.2222583
[1] 0.2222583
[1] 0.2222583
[1] 0.2222583
[1] 0.2222583
[1] 0.2222583
[1] 0.2222583
[1] 0.2222583
[1] 0.2222583
[1] 0.2222583
[1] 0.2222583
[1] 0.2222583
[1] 0.2222583
[1] 0.2222583
[1] 0.2222583
[1] 0.2222583
[1] 0.2222583
[1] 0.2222583
[1] 0.2222583
[1] 0.2222583
[1] 0.2222583
[1] 0.2222583
[1] 0.2222583
[1] 0.2222583
[1] 0.2222583
[1] 0.2222583
This :
mean(temp[i,j,])will only produce a single value, so the LHS of that assignment should bessta_winter[i,j], and that means your dimensions for the means of winter (in the Southern Hemisphere) months should be only c(360,180). (And that explains the pattern you are puzzled by.)If you want a single value for the “year” index, then you need to use that seq_along applied to the index inside your loop on the LHS of the assingment: