Breaking Apart List

When a function returns a list, how do I break it apart so that I can access the parts that I want. For example, the function principal returns a lot of lists such as loading. Here's what I see using str():

..- attr(*, "dimnames")=List of 2
.. .. : chr [1:29] "A" "B" "C" "D" ... .. .. : chr [1:5] "RC1" "RC2" "RC3" "RC4" ...

How do I access the second and third loadings ("RC2" and "RC3") for the variable "C"?

loading is not a list. It's dimension names are a list, it is in fact a matrix. See below an example similar to yours using some example data in the package:

library(psych)

pc <- principal(Harman74.cor$cov,4,rotate="varimax")

str(pc)
#> List of 28
#>  $ values      : num [1:24] 8.14 2.1 1.69 1.5 1.03 ...
#>  $ rotation    : chr "varimax"
#>  $ n.obs       : logi NA
#>  $ communality : Named num [1:24] 0.604 0.367 0.472 0.451 0.701 ...
#>   ..- attr(*, "names")= chr [1:24] "VisualPerception" "Cubes" "PaperFormBoard" "Flags" ...
#>  $ loadings    : 'loadings' num [1:24, 1:4] 0.1566 0.0869 0.1428 0.2476 0.7861 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:24] "VisualPerception" "Cubes" "PaperFormBoard" "Flags" ...
#>   .. ..$ : chr [1:4] "RC1" "RC3" "RC2" "RC4"
#>  $ fit         : num 0.917
#>  $ fit.off     : num 0.968
#>  $ fn          : chr "principal"
#>  $ Call        : language principal(r = Harman74.cor$cov, nfactors = 4, rotate = "varimax")
#>  $ uniquenesses: Named num [1:24] 0.396 0.633 0.528 0.549 0.299 ...
#>   ..- attr(*, "names")= chr [1:24] "VisualPerception" "Cubes" "PaperFormBoard" "Flags" ...
#>  $ complexity  : Named num [1:24] 1.39 1.09 1.16 1.36 1.28 ...
#>   ..- attr(*, "names")= chr [1:24] "VisualPerception" "Cubes" "PaperFormBoard" "Flags" ...
#>  $ chi         : num NA
#>  $ EPVAL       : num NA
#>  $ R2          : Named num [1:4] 1 1 1 1
#>   ..- attr(*, "names")= chr [1:4] "RC1" "RC3" "RC2" "RC4"
#>  $ objective   : num 2.17
#>  $ residual    : num [1:24, 1:24] 0.3961 -0.1408 -0.0972 -0.0346 0.024 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:24] "VisualPerception" "Cubes" "PaperFormBoard" "Flags" ...
#>   .. ..$ : chr [1:24] "VisualPerception" "Cubes" "PaperFormBoard" "Flags" ...
#>  $ rms         : num 0.0579
#>  $ factors     : int 4
#>  $ dof         : num 186
#>  $ null.dof    : num 276
#>  $ null.model  : num 11.4
#>  $ criteria    : Named num [1:3] 2.17 NA NA
#>   ..- attr(*, "names")= chr [1:3] "objective" "" ""
#>  $ PVAL        : logi NA
#>  $ weights     : num [1:24, 1:4] -0.08029 -0.06611 -0.05358 -0.00913 0.24714 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:24] "VisualPerception" "Cubes" "PaperFormBoard" "Flags" ...
#>   .. ..$ : chr [1:4] "RC1" "RC3" "RC2" "RC4"
#>  $ r.scores    : num [1:4, 1:4] 1.00 -4.86e-17 -1.11e-15 -1.26e-15 -9.71e-17 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:4] "RC1" "RC3" "RC2" "RC4"
#>   .. ..$ : chr [1:4] "RC1" "RC3" "RC2" "RC4"
#>  $ rot.mat     : num [1:4, 1:4] 0.5991 -0.6178 -0.5092 -0.0105 0.4582 ...
#>  $ Vaccounted  : num [1:5, 1:4] 4.159 0.173 0.173 0.31 0.31 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:5] "SS loadings" "Proportion Var" "Cumulative Var" "Proportion Explained" ...
#>   .. ..$ : chr [1:4] "RC1" "RC3" "RC2" "RC4"
#>  $ Structure   : 'loadings' num [1:24, 1:4] 0.1566 0.0869 0.1428 0.2476 0.7861 ...
#>   ..- attr(*, "dimnames")=List of 2
#>   .. ..$ : chr [1:24] "VisualPerception" "Cubes" "PaperFormBoard" "Flags" ...
#>   .. ..$ : chr [1:4] "RC1" "RC3" "RC2" "RC4"
#>  - attr(*, "class")= chr [1:2] "psych" "principal"

loadings1<- pc$loadings # extract using name

# loadings1 is not a list, it is a matrix 

str(loadings1)
#>  'loadings' num [1:24, 1:4] 0.1566 0.0869 0.1428 0.2476 0.7861 ...
#>  - attr(*, "dimnames")=List of 2
#>   ..$ : chr [1:24] "VisualPerception" "Cubes" "PaperFormBoard" "Flags" ...
#>   ..$ : chr [1:4] "RC1" "RC3" "RC2" "RC4"

# If I want the second and third loadings for variable Cubes
# Here is one way to extract it
loadings1["Cubes", c("RC2", "RC3")]
#>        RC2        RC3 
#> 0.08144115 0.59303264

Created on 2020-06-12 by the reprex package (v0.3.0)

Thanks. That's helping me with my project.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.