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
Create 3D array using the dim() function in R Arrays in R Programming Language are the data objects which can store data in more than two dimensions. 3-D array is also known as a Multidimensional array. We can create a multidimensional array with dim() function.
Creating a Multidimensional ArrayAn array is created using the array() function. It takes vectors as input and uses the values in the dim parameter to create an array. A multidimensional array can be created by defining the value of 'dim' argument as the number of dimensions that are required.
Inserting values in 3D array: In 3D array, if a user want to enter the values then three for loops are used. First for loop represents the blocks of a 3D array. Second for loop represents the number of rows. Third for loop represents the number of columns.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With