Setting up a 3D matrix in R and accessing certain elements

Fabian Stolz picture Fabian Stolz · Jun 9, 2012 · Viewed 60.5k times · Source

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 me is how I can change one entry in the matrix. Let's say we want to assign the value 204 to the element [304,5,2]. I highly appreciate your answer!

thanks! best, F

Answer

Havelock picture Havelock · Jun 9, 2012

Try this:

ar <- array(someData, c(365, 6, 4));  
ar[304,5,2] <- 204;

where someData might be

someData <- rep(0, 365*6*4);  

or even better maybe

someData <- rep(NaN, 365*6*4);