I have a long script with a lot of data manipulations, which would be too long to share completely, but I will share some parts as to clarify what I am out for. There is one function within the script that generates random points, so the final objects I create are always different. What I want to do is loop the entire script 100 times, and store the many resulting objects in a list.
Here is some of the script
area <- read_sf("area_erase.shp")
#List of rasters
agrilist <- sprintf('p%s_agriprop.tif', 1:6)
results_agri <- lapply(agrilist, raster)
results_agri <- setNames(results_agri, agrilist)
#List of second rasters
poplist <- sprintf('p%s_pop.tif', 1:6)
results_pop <- lapply(poplist, raster)
results_pop <- setNames(results_pop, poplist)
#resample one of the rasters
results_agri[[6]] <- resample(results_agri[[6]],results_agri[[5]],method="bilinear")
#stack them
allrasters <- mapply(raster::stack,results_agri,results_pop)
#load in existing points
pointslist <- sprintf('p%s_utm_n.csv', 1:6)
results_points <- lapply(pointslist, read.csv)
results_points <- setNames(results_points, pointslist)
#prepare for extract (lon lat cols only)
extr <- function(xts.obj){xts.obj[,c(2:3)]}
points_xy <- lapply(results_points, extr)
etc, etc
The 'random' part, which ensures a different result each time the script is run is as follows
#create random points
pas[[1]] <- randomPoints(results_agri[[1]],n=1082,p=points_xy[[1]],ext=area)
pas[[2]] <- randomPoints(results_agri[[1]],n=2506,p=points_xy[[2]],ext=area)
pas[[3]] <- randomPoints(results_agri[[1]],n=2326,p=points_xy[[3]],ext=area)
pas[[4]] <- randomPoints(results_agri[[1]],n=3168,p=points_xy[[4]],ext=area)
pas[[5]] <- randomPoints(results_agri[[1]],n=3216,p=points_xy[[5]],ext=area)
pas[[6]] <- randomPoints(results_agri[[1]],n=3176,p=points_xy[[6]],ext=area)
And then the final part of the script,
#Final values
st1 <- cellStats(prediction1>tr1,sum)
st2 <- cellStats(prediction2>tr2,sum)
st3 <- cellStats(prediction3>tr3,sum)
st4 <- cellStats(prediction4>tr4,sum)
st5 <- cellStats(prediction5>tr5,sum)
st6 <- cellStats(prediction6>tr6,sum)
These 6 objects are a single numeric value each, and will differ every iteration the script, so I'd like to run these 100 times too, one time at the end of each iteration, and then store the output in a list
How would I go about this? I was thinking about a 'repeat' loop, but am unsure how to store the final objects in a list and break the loop once there's 100 iterations done.
If I'm understanding the problem correctly, you could use a for()
loop from 1 to 100 and each time save the resulting six values as an element in a list (could either be a list or vector):
output <- vector(mode="list", length=100)
for(i in 1:100){
## functions to creat intermediate objects
st1 <- cellStats(prediction1>tr1,sum)
st2 <- cellStats(prediction2>tr2,sum)
st3 <- cellStats(prediction3>tr3,sum)
st4 <- cellStats(prediction4>tr4,sum)
st5 <- cellStats(prediction5>tr5,sum)
st6 <- cellStats(prediction6>tr6,sum)
output[[i]] <- list(st1=st1, st2=st2,
st3=st3, st4=st4,
st5=st5, st6=st6)
}
Or, if you wanted to have each element of output
to be a vector rather than a list, you could replace the relevant lines above with this:
output[[i]] <- c(st1=st1, st2=st2,
st3=st3, st4=st4,
st5=st5, st6=st6)