A.R. Ebrahimi wrote:> Hi guys,
> I am new to R, so sorry if my problem seems trivial.
>
> Sometimes I encounter some lists, which I cannot index their components
> with [ . ]
> For instance the prcomp() function returns a 'prcomp' object whose
> components are some 'lists'. the second component is a list that
> comtains the following:
>
> > mylist <- churn[2]
>
> > class(mylist)
> [1] "list"
>
> > mylist
>
> PC1 PC2
> PC3 PC4 PC5 PC6
> Account_Length -8.930570e-03 2.383719e-03 -2.414908e-02
> -1.004704e-01 9.941468e-01 2.867606e-02
> Area_Code 1.543767e-02 -1.710566e-02 -6.258129e-03
> 9.945271e-01 1.008478e-01 -8.890155e-03
> Intl_Plan -2.627796e-04 -1.809011e-04
> -9.542965e-05 3.230055e-04 2.148520e-04 -1.622996e-05
> VMail_Plan 4.879640e-06 -1.339026e-04 1.639344e-04
> -1.367628e-05 2.897648e-05 5.548126e-04
> VMail_Message -4.174810e-04 -2.969518e-03 4.791422e-03
> -5.643246e-04 -1.752559e-03 1.433728e-02
> Day_Mins -9.845791e-01 2.049519e-02 -4.038624e-02
> 1.619195e-02 -8.308484e-03 4.217712e-04
> Day_Calls -2.810874e-03 1.428161e-02 2.837638e-03
> -7.647877e-03 2.532260e-02 -9.431350e-01
> Day_Charge -1.673781e-01 3.483165e-03 -6.864827e-03
> 2.752626e-03 -1.412836e-03 7.042886e-05
> Eve_Mins -4.029002e-02 -8.265860e-01 5.544912e-01
> -1.166711e-02 1.426697e-02 -1.087738e-02
>
> It look more like a matrix!
> but I cannot access individual items in this list.
>
> > mylist[1]
> 'returns the whole think'
>
> > mylist[1,1]
> Error in mylist[1, 1] : incorrect number of dimensions
>
> > mylist[2]
> $<NA>
> NULL
>
>
> Would you please help me on this
Below are some possibilities. What exactly are you trying to extract
for the prcomp results?
> pc <- prcomp(USArrests, scale = TRUE)
> str(pc)
List of 5
$ sdev : num [1:4] 1.575 0.995 0.597 0.416
$ rotation: num [1:4, 1:4] -0.536 -0.583 -0.278 -0.543 0.418 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:4] "Murder" "Assault" "UrbanPop"
"Rape"
.. ..$ : chr [1:4] "PC1" "PC2" "PC3"
"PC4"
$ center : Named num [1:4] 7.79 170.76 65.54 21.23
..- attr(*, "names")= chr [1:4] "Murder"
"Assault" "UrbanPop" "Rape"
$ scale : Named num [1:4] 4.36 83.34 14.47 9.37
..- attr(*, "names")= chr [1:4] "Murder"
"Assault" "UrbanPop" "Rape"
$ x : num [1:50, 1:4] -0.976 -1.931 -1.745 0.140 -2.499 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:50] "Alabama" "Alaska" "Arizona"
"Arkansas" ...
.. ..$ : chr [1:4] "PC1" "PC2" "PC3"
"PC4"
- attr(*, "class")= chr "prcomp"
> pc[[2]]
PC1 PC2 PC3 PC4
Murder -0.5358995 0.4181809 -0.3412327 0.64922780
Assault -0.5831836 0.1879856 -0.2681484 -0.74340748
UrbanPop -0.2781909 -0.8728062 -0.3780158 0.13387773
Rape -0.5434321 -0.1673186 0.8177779 0.08902432
> pc[[2]][,1:2]
PC1 PC2
Murder -0.5358995 0.4181809
Assault -0.5831836 0.1879856
UrbanPop -0.2781909 -0.8728062
Rape -0.5434321 -0.1673186
> pc$rotation[,1:2]
PC1 PC2
Murder -0.5358995 0.4181809
Assault -0.5831836 0.1879856
UrbanPop -0.2781909 -0.8728062
Rape -0.5434321 -0.1673186
> Regards
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
--
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894