[R-sig-Geo] assigning names to iteratively generated rasters

Hodgess, Erin HodgessE at uhd.edu
Thu Dec 6 08:38:33 CET 2012


Hi Terry, 

Try this:

assign(paste("d",i,sep=""),crop(x,extent(x,r1=r[1],r2=r[2],c1=c[1],c=c[2]))
________________________________________
From: r-sig-geo-bounces at r-project.org [r-sig-geo-bounces at r-project.org] on behalf of Beutel, Terry S [Terry.Beutel at daff.qld.gov.au]
Sent: Thursday, December 06, 2012 1:27 AM
To: r-sig-geo at r-project.org
Subject: [R-sig-Geo] assigning names to iteratively generated rasters

Hi everyone

I want to know how to iteratively assign names to rasters as I generate them, taking those names from a vector of preset names.

For example, say I have a list of the names I want my rasters to inherit. ie

> raster.names<-paste("d",1:8,sep="")
> raster.names
[1] "d1" "d2" "d3" "d4" "d5" "d6" "d7" "d8"

Now I take a raster. eg


> library(raster)

> x<-raster()

> x

class       : RasterLayer

dimensions  : 180, 360, 64800  (nrow, ncol, ncell)

resolution  : 1, 1  (x, y)

extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)

coord. ref. : +proj=longlat +datum=WGS84

I can iteratively crop random sections from the raster eg

for(i in 1:8){

> r<-sort(sample(1:180,2,replace=F))

> c<-sort(sample(1:360,2,replace=F))

> out<-crop(x,extent(x,r1=r[1],r2=r[2],c1=c[1],c=c[2]))

}



My question is this. Instead of saving each random crop as "out" in each iteration (and thus overwriting the previous iteration of "out"), how do I save sequential iterations with those names listed in raster.names. So for example in the first loop "out" would be saved as an object called d1, in the second as an object called d2 etc. Just to be clear, the random cropping here is just to serve an example, my real interest is in how I permanently keep rasters I generate from an iterative process under names I choose for them.



Thanks for any help.

> Dr Terry Beutel

Senior Scientist

Agri-Science Queensland



        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list