Dear all,
I have some questions concerning stacks and bricks from the raster package.
1. is it possible to flip the whole stack vertically? I get an error when I
use the 'flip' function..
2. is it possible to use the calc function (or something else) on a stack to
get a raster, which shows the regression slope of the pixels through the
stack (treated like a kind of time series)? And is it possible to compute a
correlation between two stacks of the same dimensions?
greetings,
Martin
On Tue, Sep 21, 2010 at 12:49 AM, Martin <martin_brandt at gmx.net> wrote:
Dear all,
I have some questions concerning stacks and bricks from the raster package.
1. is it possible to flip the whole stack vertically? I get an error when I
use the 'flip' function..
You can use what Nikhil send you or perhaps (for objects that are not
too big), with RasterStack s
x <- as(s, 'SpatialGridDataFrame')
x <- flipVertical(x)
s2 <- stack(x) # or b <- brick(x)
or get raster 1.5.9 from R-Forge in a couple of hours and then do
x <- flip(s, direction='y')
2. is it possible to use the calc function (or something else) on a stack to
get a raster, which shows the regression slope of the pixels through the
stack (treated like a kind of time series)?
You can do things like this:
myfun = function(v, ...) { d <- data.frame(x=1:length(v), y=v);
lm(y~x, data=d)$coefficients[2] }
a <- calc(stack, fun=myfun)
And is it possible to compute a
correlation between two stacks of the same dimensions?
Not directly, I think, but if you have two stacks, s1, and s2, and
some patience, you can do:
r <- raster(s1)
for (i in 1:ncell(r)) {
r[i] <- cor(as.vector(cellValues(s1, i)), as.vector(cellValues(s2, i)))
}
Robert
Dear Nikhil and dear Robert,
thanks a lot for your help.
I first tried Nikhils function, it works well and helps me to learn how
these functions work.
Good to see that flipping bricks will be implemented in the new raster
package. The raster package is a great piece of work. It makes all those
expensive GIS systems almost useless for me, it's exactly what i was looking
for for so many years..
I also tried the regression and correlation code and it works well. a huge
thanks!
Martin