[R-sig-Geo] Creating a data frame from a set of rasters that includes XY values as columns

Andrew Vitale vitale232 at gmail.com
Tue Feb 24 19:28:05 CET 2015


Something like this should do the trick, depending on the size of the
raster stack and the computing environment of course:

library(raster)

fn <- system.file("external/test.grd", package="raster")
s <- stack(fn, fn)
r <- raster(fn)
s <- stack(r, fn)

dfs <- as.data.frame(s, xy=TRUE)


checkout ?raster:::as.data.frame for more options

-Andrew

On Tue, Feb 24, 2015 at 9:19 AM, Damian Maddalena <damianmaddalena at gmail.com
> wrote:

> Hello.
>
> I am trying to create a data frame from a series of rasters that includes
> a column for each raster and a column for the corresponding XY values. IE:
> I want each row to be the cell values at every XY location.
>
> For example, if I pull down the worldclim bioclim data (below), I would
> like to unpack one or more rasters from the raster stack and assemble them
> as a data frame with X and Y as an attribute. IE: I want a data frame where
> each record is a cell, and the attributes are X,Y,Z1...Zx.
>
> > library(raster)
> > d <- getData('worldclim', var='bio', res=10)
>
> I have done this in GRASS with all data imported but I would like to try
> to do it in R in the most succinct way possible. I am getting tripped up on
> the structure of the raster stack.
>
> Any hints on how to proceed would be appreciated. Should I just pull out
> the raster values, turn them into a vector, slap them into a data frame,
> then append the range for the XY? Would that line up correctly?
>
> Thank you.
>
> -Damian
>
> --
> “Science knows it doesn't know everything; otherwise, it'd stop. But just
> because science doesn't know everything doesn't mean you can fill in the
> gaps with whatever fairy tale most appeals to you.” ~Dara Ó Briain
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



-- 
*Andrew P. Vitale*
Masters Student
Department of Geography
University of Nevada, Reno
vitale232 at gmail.com

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list