I have a large array envSST with dimension (720,361,10,276)
I am actually only interested in a small subset of the first two dimensions, given by a 54×2 matrix called lat.lon.ensinds, so I can delete envSST and free up some space.
Two things I have tried:
eSST <- t(sapply(1:nrow(lat.lon.ensinds), function (k)
ensSST[lat.lon.ensinds$lon.inds[k], lat.lon.ensinds$lat.inds[k], , ]))
which results in a matrix of dimension (54,2760) instead of an array of size (54,10,276)
(I tried adding the sapply argument simplify=”array” but it resulted in an error)
eSST <- NULL
eSST <- rbind(eSST, ensSST[lat.lon.ensinds$lon.inds[k],
lat.lon.ensinds$lat.inds[k], , ])
which results in a matrix of size (540,276) instead of an array
Any suggestions?
Peter Guttorp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
This is easy to fix if you understand that the result of `[`()
is coerced to the lowest possible dimension if you don’t use drop=FALSE
.
Consider this example array:
> (A <- array(,c(2, 2, 2, 2)))
, , 1, 1
[,1] [,2]
[1,] NA NA
[2,] NA NA
, , 2, 1
[,1] [,2]
[1,] NA NA
[2,] NA NA
, , 1, 2
[,1] [,2]
[1,] NA NA
[2,] NA NA
, , 2, 2
[,1] [,2]
[1,] NA NA
[2,] NA NA
Where
> dim(A)
[1] 2 2 2 2
Coerced,
> A[1:2, 1, , ]
, , 1
[,1] [,2]
[1,] NA NA
[2,] NA NA
, , 2
[,1] [,2]
[1,] NA NA
[2,] NA NA
not coerced.
> A[1:2, 1, , , drop=FALSE]
, , 1, 1
[,1]
[1,] NA
[2,] NA
, , 2, 1
[,1]
[1,] NA
[2,] NA
, , 1, 2
[,1]
[1,] NA
[2,] NA
, , 2, 2
[,1]
[1,] NA
[2,] NA