I am running a loop in R to rasterize a list of data frames and for comparison purposes I have to keep the same list length in the output.
I am facing some problems with NULL objects on the list. I managed to handle NULL objects inside the list with tryCatch, however when I have null objects at the end of the list (e.g. last 3 objects) they are not included in the output of the loop, so the output list (fry) get shorter than the original list (fr.fr). See the output below.
I would like to know if there is an efficient way to handle this problem in R and to ensure the same list length. I also looking to more straightforward approaches.
Here is my code:
# Raster
st <- raster(xxx)
# List of data frame to Raster
fry <- list()
for (i in 1:length(fry.fr)){
tryCatch({
fry[[i]] <- rasterize(fry.fr[[i]], st, fry.fr[[i]]$AREA, fun = sum, na.rm=FALSE)
}
,error= function(e) {cat("Error", "\n")})
}
Here is my output:
length(fry.fr) 336
length(fry) 333