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

前端 未结 3 515
日久生厌
日久生厌 2021-02-02 07:54

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

3条回答
  •  情歌与酒
    2021-02-02 08:25

    Try this simple example (have made the example a fairly small one so it's clear what's going on - I explain below how to tweak it for your precise question!)...

    m = array(1:60, dim=c(3,4,5))
    
    m[2,1,5]
    [1] 50
    
    m[2,1,5] = -50
    
    m[2,1,5]
    [1] -50
    

    Type m to see the whole 3d array :)

    In your example, you'd set up your initial array as m = array(NA, dim=c(365,6,4)) (this will fill it with NAs to start with - do you have data to fill it up with?) And the assignment is m[304,5,2] = 204, of course :)

提交回复
热议问题