[R-sig-Geo] repeated commands... how to loop?

Alexandre Villers villers.alexandre at gmail.com
Sun Dec 2 08:58:58 CET 2012


Hello,

It's not really clear from your message whether y1, y2, y3 are the y 
part of x/y coordinates, and what is the exact structure of your data 
frame. Without knowing this, the code below is to be adapted to your needs

##load the packages
library(gstat)
library(raster)

#Load the data that are needed
setwd("f:/Grids")
r2 <- raster("idgd_wgs.asc")

y.list <- list(y1<-runif(10), y2 <- runif(10), y3 <- runif(10))
da1<-data.frame(x=1:10, z=1:10)

for (i in 1:length(y.list) {
  da.temp<-data.frame(da1, y=y.list[[i]])
  IDW.temp <- gstat(id = "z", formula = z~1, locations = ~x+y, 
data=da.temp, set=list(idp =2))
  IDWRaster.temp <- interpolate(r2, IDW.temp)
  #add here one or two command lines to save the objects you want to 
save, or check
  rm(da.temp, IDW.temp, IDWRaster.temp) #remove the temporary elements 
(always safer)
}


HTH

Alex


> Dear r users,
>   I have the following script that I would like to run repeatedly for
> different values of y ( the list of which I created below).
> y.list <- list(y1=runif(10), y2 <- runif(10), y3 <- runif(10))
>
> and here is the script I want to run for different values of y.
>
> ############
>
> x <- 1:10
> y <- 1:10
> z<- 1:10
>
> da1<-data.frame(x,y,z)
> ***** this file is an aready existing ascii file that I use to form the
> raster
> library(raster)
>
> setwd("f:/Grids")
> r2 <- raster("idgd_wgs.asc")
> library(raster)
>
> library(gstat)
> IDW <- gstat(id = "z", formula = z~1, locations = ~x+y, data=da1,
> set=list(idp =2))
> IDWRaster <- interpolate(r2, IDW)
>
> Any suggestion appreciated.
> Thanks.
> minti
>
> 	[[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