saving results from a loop
Well, yes: summary(results[[i]]) gives you the summary for the i-th element of the list results, which is presumably the last of your loop iterations. summary(results[[1]]) summary(results[[2]]) summary(results[[3]]) or better, lapply(results, summary) You'd be well advised to read some intro to R materials, especially the sections about lists. Sarah
On Tue, Dec 4, 2012 at 2:07 PM, alemante zd <mintibezabih at gmail.com> wrote:
Thanks Sarah. The code runs fine but I still dont get 3 different resukts (corresponding to the 3 zs). Here is what I get instead:
summary(results[[i]])
Min. 0.5900 1st Qu. 0.5911 Median 0.5917 Mean 0.5918 3rd Qu. 0.5924 Max. 0.5936
summary (IDWRaster.temp)
Min. 0.5900
1st Qu. 0.5911
Median 0.5917
Mean 0.5918
3rd Qu. 0.5924
Max. 0.5936
#### the code
library(gstat)
library(raster)
x <- 1:10
#y <- 1:10
z<- 1:10
results <- list()
setwd("f:/Grids")
r2 <- raster("idgd_wgs.asc")
z.list <- list(z1<-runif(10), z2 <- runif(10), z3 <- runif(10))
da1<-data.frame(x=1:10, y=1:10)
for (i in 1:length(z.list)) {
da.temp<-data.frame(da1, z=z.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)
results[[i]] <- IDWRaster.temp
}
Thanks again.
minti
On Tue, Dec 4, 2012 at 6:19 PM, Sarah Goslee <sarah.goslee at gmail.com> wrote:
I would save the information you're interested in as a list:
library(gstat)
library(raster)
x <- 1:10
#y <- 1:10
z<- 1:10
results <- list()
setwd("f:/Grids")
r2 <- raster("idgd_wgs.asc")
z.list <- list(z1<-runif(10), z2 <- runif(10), z3 <- runif(10))
da1<-data.frame(x=1:10, y=1:10)
for (i in 1:length(z.list)) {
da.temp<-data.frame(da1, z=z.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)
# I want to save the results from IDWRaster.temp
results[[i]] <- IDWRaster.temp
}
On Tue, Dec 4, 2012 at 1:15 PM, alemante zd <mintibezabih at gmail.com>
wrote:
Dear R-sig-geo users,
I have the following code in a loop. I was wondering how I can get the
results saved from the loop. The result I am interested in
is IDWRaster.temp
library(gstat)
library(raster)
x <- 1:10
#y <- 1:10
z<- 1:10
setwd("f:/Grids")
r2 <- raster("idgd_wgs.asc")
z.list <- list(z1<-runif(10), z2 <- runif(10), z3 <- runif(10))
da1<-data.frame(x=1:10, y=1:10)
for (i in 1:length(z.list)) {
da.temp<-data.frame(da1, z=z.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)
# I want to save the results from IDWRaster.temp
}
Thanks in advance for any help.
Miniti
[[alternative HTML version deleted]]
-- Sarah Goslee http://www.functionaldiversity.org