Mean every 12 layers (months)

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Mean every 12 layers (months)

Jackson Rodrigues
Hi fellows,

My name is Jackson.

I have 2 questions. I am gratful for any help for anyone.

----------------------
*Question 1*
Regarding extracting multiple mean from long raster file.
 I want to reduce 6000 layers (1 per month) to 500 (1 per year)

library(raster)
>TS20 = brick("trace.20.10200-09701BP.cam2.h0.TS.nc", level=1, var="TS")
#It extents from 10200 BP to 9701 BP- a total of 6000 layers (1 per month)
>TS20 = rotate(TS20)
>TS20
class       : RasterBrick
dimensions  : 48, 96, 4608, 6000  (nrow, ncol, ncell, nlayers)
resolution  : 3.75, 3.708898  (x, y)
extent      : -178.125, 181.875, -89.01354, 89.01354  (xmin, xmax, ymin,
ymax)

>indices<-rep(1:6000,each=12) #found somewhere 6000 layers / 12 months =
500 annual layers
>s.mean<-stackApply(TS20, indices, fun = mean)
Warning message:
In ind[] <- indices :
  number of items to replace is not a multiple of replacement length

#Why do I get this warning message?  My raster has 6000 layers, so my
indices should work, or not?
---------------------------
*Question 2*
how could rename my layers combining years and months ?
For example?
Layer 1 should be TS20$10200.Jan
Layer 2 should be TS20$10200.Feb
...
Layer 6000 should be TS20$9701.Dec

In the end I will calculate seasonal mean. So, this is why I would like to
rename them.

Thank you all,

Jackson

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Mean every 12 layers (months)

Nikos Mastrantonas
Hi Jackson,

Regarding your first question, your indices vector has 6000X12 elements,
since you repeat each number 12 times.
You could maybe do:
indices <- rep(1:500,each=12), so you end up with 6000 elements

For the second question, one way you could do that is to add the zoo
package and do the following:
library(zoo)
TS20@z$date <- as.yearmon(...) # give a vector with the dates of the layers
with this way you will have the date of each layer in a 'date' format and
thus you will be able to easily select the layers you want on a later stage
(eg, choose specific months, specific years, etc).
Otherwise, you could use:
names(TS20) <- ... # give a vector with the new layer names

Hope the info can be helpful to you.

Thanks,
Nikos

*-------------------------------------------------------*
*Nikolaos Mastrantonas*

*Phone: *+44 (0) 7599 843061
*E-mail: [hidden email] <[hidden email]>*
*Linkedin*: nikolaosmastrantonas
<http://www.linkedin.com/in/nikolaosmastrantonas>


On Sun, 4 Nov 2018 at 00:14, Jackson Rodrigues <[hidden email]>
wrote:

> Hi fellows,
>
> My name is Jackson.
>
> I have 2 questions. I am gratful for any help for anyone.
>
> ----------------------
> *Question 1*
> Regarding extracting multiple mean from long raster file.
>  I want to reduce 6000 layers (1 per month) to 500 (1 per year)
>
> library(raster)
> >TS20 = brick("trace.20.10200-09701BP.cam2.h0.TS.nc", level=1, var="TS")
> #It extents from 10200 BP to 9701 BP- a total of 6000 layers (1 per month)
> >TS20 = rotate(TS20)
> >TS20
> class       : RasterBrick
> dimensions  : 48, 96, 4608, 6000  (nrow, ncol, ncell, nlayers)
> resolution  : 3.75, 3.708898  (x, y)
> extent      : -178.125, 181.875, -89.01354, 89.01354  (xmin, xmax, ymin,
> ymax)
>
> >indices<-rep(1:6000,each=12) #found somewhere 6000 layers / 12 months =
> 500 annual layers
> >s.mean<-stackApply(TS20, indices, fun = mean)
> Warning message:
> In ind[] <- indices :
>   number of items to replace is not a multiple of replacement length
>
> #Why do I get this warning message?  My raster has 6000 layers, so my
> indices should work, or not?
> ---------------------------
> *Question 2*
> how could rename my layers combining years and months ?
> For example?
> Layer 1 should be TS20$10200.Jan
> Layer 2 should be TS20$10200.Feb
> ...
> Layer 6000 should be TS20$9701.Dec
>
> In the end I will calculate seasonal mean. So, this is why I would like to
> rename them.
>
> Thank you all,
>
> Jackson
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo