I a having a little trouble with vector or array operations.
I have three 3D arrays and i wanna find the average of them. How can i do that? we can't use mean()
as it only returns a single value.
The more important is some of the cells in the arrays are NA whic mean if i just add them like
A = (B + C + D)/3
The results of will show NA as well.
How can i let it recognise if the cell is NA then just skip it.
Like
A = c(NA, 10, 15, 15, NA)
B = c(10, 15, NA, 22, NA)
C = c(NA, NA, 20, 26, NA)
I wanna the output of average these vectors be
(10, (10+15)/2, (15+20)/2, (15+22+26)/3, NA)
We also can't use na.omit
, because it will move the order of indexes.
This is the corresponding code. i wish it would be helpful.
for (yr in 1950:2011) {
temp_JFM <- sst5_sst2[,,year5_sst2==yr & (month5_sst2>=1 & month5_sst2<=3)]
k = 0
jfm=4*k+1
for (i in 1:72) {
for (j in 1:36) {
iposst5_sst2[i,j,jfm] <- (temp_JFM[i,j,1]+temp_JFM[i,j,2]+temp_JFM[i,j,3])/3
}
}
}
Thnk you.
It already been solved.
The easiest way to correct it can be shown below.
iposst5_sst2[i,j,jfm] <- mean(temp_JFM[i,j,],na.rm=TRUE)