Finding the mean of raster layers using for loop in R

I am trying to compute the average of raster layers (mean temperature layers) from PRISM data base using a loop.

Number of layers in the stack is 70

``````tmean10<-list()

tmeanR<-stack(tmean_52_61_R,tmean_62_71_R, tmean_72_81_R, tmean_82_91_R, tmean_92_01_R,
tmean_02_11_R,tmean_12_21_R)

for (i in 1:60) {
tmean10<-calc(tmeanR[[i+1:i+10]], fun = mean)
}
``````

But it shows the following error:

Error in h(simpleError(msg, call)) : error in evaluating the argument 'x' in selecting a method for function 'calc': not a valid subset.

Where am I making the mistake?

• Assuming this is R, I've added the R tag Feb 10, 2023 at 22:58
• Thanks so much @Spacedman. IT IS WORKING NOW. Thanks a lot.... Number of layers in the stack is 70. The problem was with the expression > i = 1 > i+1:i+10. What I was doing was: For the year 1962 - I have to calculate the mean from 1953 to 1962. Then for the year 1963 - Mean from 1954 - 1963. It is like for a given year = mean (given year + preceding 9 years) Feb 10, 2023 at 23:22

You may need parentheses in this expression:

``````> i = 1
> i+1:i+10
[1] 12
``````

Because R does the `:` operator first. The above is like doing:

``````> i + (1:i) + 10
``````

Which I don't think is what you want. I think you want:

``````> (i+1):(i+10)
[1]  2  3  4  5  6  7  8  9 10 11
``````

To generate a sequence from i+1 to i+10. Not sure why you are not using the first layer - should your loop start at zero?

And so you might be going outside the limits of your stack. Does it really have 60 layers? Is that enough? Let's make a test and see if I can replicate the error message:

``````> r = raster(matrix(runif(12), 3, 4))
> s = stack(r,r,r,r)
``````

`s` is a stack with four layers. I can get two:

``````> s[[1:2]]
class      : RasterStack
dimensions : 3, 4, 12, 2  (nrow, ncol, ncell, nlayers)
resolution : 0.25, 0.3333333  (x, y)
extent     : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
crs        : NA
names      :    layer.1,    layer.2
min values : 0.05741907, 0.05741907
max values :  0.9484756,  0.9484756
``````

But if I go outside 4, I get your error:

``````> s[[1:5]]
Error in .local(x, ...) : not a valid subset
``````

So this is what's happening. You are looping up to 60, and when `i` is 60 your index is:

``````> i = 60
> i+1:i+10
[1]  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89
[20]  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108
[39] 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
[58] 128 129 130
``````

which mean's its expecting a stack with 130 layers. If instead you meant:

``````> (i+1):(i+10)
[1] 61 62 63 64 65 66 67 68 69 70
``````

Then you need a stack with 70 layers. We don't know what you've got but I'm guessing its not got 70 layers. Can't help more without a better description of your data.

• Thanks so much @Spacedman. IT IS WORKING NOW. Thanks a lot.... Number of layers in the stack is 70. The problem was with the expression > i = 1 > i+1:i+10 Feb 10, 2023 at 23:10