I have a list in R containing values of leaf area by layer by month, like this
ls <- list(month = c(1,1,2,2,3,3,4,4),
layer = c(0,1,0,1,0,1,0,1),
LA = c(runif(8)))
I would like to create an array showing a snapshot of the LA by layer for each month, with layer 0 corresponding to the bottom of each matrix in the array, and layer 1 corresponding to the top. I have created an array
canopy <- array(dim = c(2,1,4))
and am trying to populate it with a for loop
for (i in 1:4) {
if (ls$month == i){
if (ls$layer == 0){
canopy[2,1,i] <- ls$LA
} else if (ls$layer == 1){
canopy[1,1,i] <- ls$LA
}}}
However, this yields an error
Error in canopy[2, 1, i] <- ls$LA :
number of items to replace is not a multiple of replacement length
In addition: Warning messages:
1: In if (ls$month == i) { :
the condition has length > 1 and only the first element will be used
2: In if (ls$layer == 0) { :
the condition has length > 1 and only the first element will be used
How can i clean this up?