 I just did a test using dropLayer on a similar  brick of global
temperatures


class       : RasterBrick
dimensions  : 36, 72, 1332  (nrow, ncol, nlayers)
resolution  : 5, 5  (x, y)
extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84
values      : none



thats 1332 months of GHCN temperature data.  Using dropLayer instead of subset



dropLayer(x,vectorOflayersTodrop)



it worked fairly quickly



Steve


On Fri, Jul 1, 2011 at 7:02 AM, Christian Kamenik <
christian.kamenik@giub.unibe.ch> wrote:

> Dear all,
>
> The package 'raster' provides excellent tools to work on geographic data.
> Subsetting a RasterBrick object, however, takes ages.
>
> I have the following RasterBrick object:
>
>    Temp.CRU.cropped
>
>    class:                  RasterBrick
>    filename:
>    dimensions:        43, 99, 1200  (nrow, ncol, nlayers)
>    ncell:                  4257
>    projection:         +proj=longlat +ellps=WGS84
>    min values:        -14.8 -22.1 -15.0  -8.6  -3.8   4.7   8.7   6.0   1.6
>  -2.4 ...
>    max values:       2.1 -3.9 -2.1  1.8  6.6 15.7 18.0 14.8 10.7  7.7 ...
>    extent:               15, 31.5, 64, 71.16667  (xmin, xmax, ymin, ymax)
>    resolution:         0.1666667, 0.1666667  (x, y)
>
> These are temperature readings from northern Fennoscandia, and each layer
> represents a month, thus each grid cell has monthly readings over the last
> 100 years.
>
> Now I want to extract the readings for only July:
>
>    Months <- rep(month.abb,nlayers(Temp.**CRU.cropped)/12)
>    Temp.CRU.J <- subset(Temp.CRU.cropped,which(**Months=='Jul'))
>
> But as I said, this takes ages. It is much faster to do the subsetting on
> an array:
>
>    CRU.values <- getValues(Temp.CRU.cropped)
>    Temp.CRU.J <- CRU.values[,which(Months=='**Jul')]
>    Temp.CRU.J <- array(Temp.CRU.J,dim=c(nrow(**
> Temp.CRU.cropped),ncol(Temp.**CRU.cropped),nlayers(Temp.CRU.**
> cropped)/12))
>    Temp.CRU.J <- brick(Temp.CRU.J)
>    Temp.CRU.J <- setExtent(Temp.CRU.J,extent(**Temp.CRU.cropped))
>    projection(Temp.CRU.J) <- projection(Temp.CRU.cropped)
>
> It is strange that several lines of code run much faster than the 'subset'
> command, and I was wondering why 'subset' takes at least a 1000 times
> longer.
>
> I would be interested in your opinion.
>
> Many thanks, Christian
>
> --
> ------------------------------**-----------------
> Oeschger Centre for Climate Change Research, Institute of Geography&
>  Institute of Plant Sciences
> University of Bern
>
> http://www.stomatocysts.unibe.**ch/kamenik<http://www.stomatocysts.unibe.ch/kamenik>
> mailto: christian.kamenik@giub.unibe.**ch<christian.kamenik@giub.unibe.ch>
>
> Postal address:
> Dr. Christian Kamenik
> Institute of Geography
> Erlachstrasse 9a, Trakt 3
> 3012 Bern, Switzerland
>
> Tel. +41 (0)31 631 5091
> Fax +41 (0)31 631 43 38
>
> ______________________________**_________________
> R-sig-Geo mailing list
> R-sig-Geo@r-project.org
> https://stat.ethz.ch/mailman/**listinfo/r-sig-geo<https://stat.ethz.ch/mailman/listinfo/r-sig-geo>
>

	[[alternative HTML version deleted]]

