I have a 3D array with lon(1:144)(range 0-360 with a step of 2.5), lat(1:29) (20-90 with a step of 2.5) and one variable in time (1:180)(30 years containing 6 months per year).
lon <- seq(from = 0, to = 360, by = 2.5)
lat <- seq(from = 17.5, to = 90, by = 2.5)
time <- c(1:180)
var <- c(28532:31298)
I transformed my data to a 2D matrix by multiplying lon*lat:
new2Ddata <- matrix(oldmatrix, prod(dim(oldmatrix)[1:2]), dim(oldmatrix)[3])
then I calculated svd:
svd.result <- svd(new2Ddata)
I got svd.result with three components and made a new dataframe using component U [1:4176 (lon*lat), 1:180 (variable)]
modes <- as.data.frame(svd.result$u)
I have to transform it again to 3D (lon, lat, time, and variable by time). I tried:
dim(modes) <- c(144,29,180)
I got an error:Error in dim(modes) <- c(144, 29, 180) : dims [product 751680] do not match the length of object [180] Any ideas on how to do it? Thanks for the help!
The problem lies in this line of code modes <- as.data.frame(svd.result$u)
. In order to convert the results back to a 3D array, you need modes
to actually be a matrix rather than a dataframe. Your code should work if you simply change it to modes <- svd.result$u
.