Thanks Etienne and Erin. Both good approaches that seem to work
Regards
Terry

________________________________
From: Etienne B. Racine [mailto:etienn...@gmail.com]
Sent: Thursday, 6 December 2012 11:19 PM
To: Hodgess, Erin
Cc: Beutel, Terry S; r-sig-geo@r-project.org
Subject: Re: [R-sig-Geo] assigning names to iteratively generated rasters

Terry,

Maybe a more orthodox way of doing this is to use replace your loop with a list 
(I have nothing against lists, it's just more convenient to manipulate a bunch 
of objects at once) :
raster_list <- lapply(1:8, function(i) {
  r <- sort(sample(1:180, 2, replace = FALSE))
  c <- sort(sample(1:360, 2, replace = FALSE))
  out <- crop(x, extent(x, r1 = r[1], r2 = r[2], c1 = c[1], c = c[2]))
})

names(raster_list) <- raster.names

# view
op <- par(mfrow = c(2, 4))
lapply(raster_list, function(x) plot(extent(x)))
par(op)

If you really want to manipulate objects, you could use attach(raster_list).

Etienne

2012/12/6 Hodgess, Erin <hodge...@uhd.edu<mailto:hodge...@uhd.edu>>
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-boun...@r-project.org<mailto:r-sig-geo-boun...@r-project.org> 
[r-sig-geo-boun...@r-project.org<mailto:r-sig-geo-boun...@r-project.org>] on 
behalf of Beutel, Terry S 
[terry.beu...@daff.qld.gov.au<mailto:terry.beu...@daff.qld.gov.au>]
Sent: Thursday, December 06, 2012 1:27 AM
To: r-sig-geo@r-project.org<mailto:r-sig-geo@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@r-project.org<mailto:R-sig-Geo@r-project.org>
https://stat.ethz.ch/mailman/listinfo/r-sig-geo

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


        [[alternative HTML version deleted]]

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

Reply via email to