[R] matrix indexing in 'for' loop?

Liviu Andronic landronimirc at gmail.com
Wed Dec 22 18:49:25 CET 2010


On Wed, Dec 22, 2010 at 6:39 PM,  <govindas at msu.edu> wrote:
> Thank you both for your suggestions. I have another question - is there a
> specific way to access the individual elements of a 'list' variable? i.e.
>
> dmi = matrix(rnorm(20),4,5)
> soi = matrix(rnorm(20),4,5)
> pe = matrix(rnorm(20),4,5)
> y <- list(dmi, soi, pe)
>
> y[[1]]   gives
> [,1]       [,2]       [,3]       [,4]       [,5]
> [1,] -0.54463900  1.6732445 -0.3807847 -1.0460530 -0.8142748
> [2,] -0.49654004 -0.9634258  0.9074139 -0.1576030 -1.2268558
> [3,] -1.61835766 -0.4240122  0.3626670  0.7182964  0.1576446
> [4,] -0.06313983  0.6743465 -1.9897107  0.8027337 -1.4372131
>
> But, I am interested in accessing the 1st column of 'dmi' matrix here - not
> as dmi[,1] but in terms of 'y'. Is it possible with 'list' function or
> something else?
>

Is this what you need?
> y[[1]]
           [,1]        [,2]        [,3]       [,4]      [,5]
[1,]  0.2523449 -1.26606999  0.27594223 -0.5158524 -0.599899
[2,] -1.9155409  0.01081565  0.44497873  0.1819517  1.187899
[3,]  0.7624774  0.31296468 -0.02187076  0.9389091  0.865527
[4,] -0.8082626 -1.44330148  1.32975075  0.1788399  1.204917

> y[[1]][,1]
[1]  0.2523449 -1.9155409  0.7624774 -0.8082626

Liviu



More information about the R-help mailing list