Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

arrays

r

element

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

like image 588
Fabian Stolz Avatar asked Jun 09 '12 12:06

Fabian Stolz


People also ask

How do I create a 3 dimensional array in R?

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.

How do I create a multidimensional array in R?

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.

How do you assign a 3D array?

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.


Video Answer


1 Answers

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);   
like image 199
Havelock Avatar answered Sep 22 '22 12:09

Havelock