I have a nested List with many elements and x number of rasters inside each element. The simplified structure of the list looks like this:
## create main List
r <- raster(ncol=3, nrow=3)
values(r) <- 1:ncell(r)
s <- stack(r,r*2,r*3)
List <- split(as.list(s), 1) # here List has a single element but in the real example has >10
I need to stack rasters stored in another list (named Ref_List) to the rasters stored in the main List, accordingly:
stack(List[[1]][[1]], Ref_list[[1]])
stack(List[[1]][[2]], Ref_list[[2]])
stack(List[[1]][[3]], Ref_list[[3]])
...
The number of rasters contained in Ref_list[[i]]
are the same as contained in List[[i]][[j]]
.
I have tried the following loop to perform the stack operation but the result is NULL.
Any ideas about how to solve this issue? Thanks!
List_stack <- lapply(List,function(i){ # i specifies elements within the list
lapply(i,function(j){ # j specifies the elements of each element, within the list
st <- stack(j)
## create REF list
z <- raster(ncol=3, nrow=3)
values(z) <- 0.5:ncell(z)
t <- stack(z,z*2,z*3)
Ref_list <- as.list(t)
## STACK REF list with MainList
for(i in 1:length(Ref_list)){
final <- stack(Ref_list[[i]], st)
}
})
})