I am trying to set up a 3D matrix in R. I guess this is an easy one. However, I didn\'t find a solution so far. Let\'s say we want to create a 365x6x4 matrix. Also crucial form
A matrix is a special 2-dimensional case of an array. (Quoting from the help for ?matrix
).
So, you need array
:
x <- array(rep(1, 365*5*4), dim=c(365, 5, 4))
str(x)
num [1:365, 1:5, 1:4] 1 1 1 1 1 1 1 1 1 1 ...
Set a specific value:
x[305, 5, 2] <- 204
Print one slice:
x[305, , ]
[,1] [,2] [,3] [,4]
[1,] 1 1 1 1
[2,] 1 1 1 1
[3,] 1 1 1 1
[4,] 1 1 1 1
[5,] 1 204 1 1