r - How can I obtain the colnames from part of a sorted matrix column? -


i have following in r:

> require("pls") > set.seed(42) > = matrix(rnorm(12), ncol=4) >            [,1]       [,2]        [,3]       [,4] [1,]  1.3709584  0.6328626  1.51152200 -0.0627141 [2,] -0.5646982  0.4042683 -0.09465904  1.3048697 [3,]  0.3631284 -0.1061245  2.01842371  2.2866454  > b <- c(1, 2, 3) > mymodel <- pcr(b ~ a) > mymodel$loadings  loadings:    comp 1 comp 2 a1  0.654  0.165 a2  0.136 -0.255 a3  0.415  0.732 a4 -0.618  0.609  > comp1 <- mymodel$loadings[, 1] > comp1         a1         a2         a3         a4  0.6539036  0.1362937  0.4146222 -0.6179988 > sort(comp1, decreasing=true)         a1         a3         a2         a4  0.6539036  0.4146222  0.1362937 -0.6179988 > sort(comp1, decreasing=true)[1]        a1 0.6539036 

i puzzled comp1 is:

> colnames(comp1) null > rownames(comp1) null > dim(comp1) null > str(comp1)  named num [1:4] 0.654 0.136 0.415 -0.618  - attr(*, "names")= chr [1:4] "a1" "a2" "a3" "a4" > typeof(comp1) [1] "double" 

questions:

  1. what data structure comp1?
  2. what should obtain column names sorted comp1 "a1", "a3", "a2", "a4"?

a question in lots of ways, since these small important distinctions vex many new , intermediate r users. in case call pcr returned list called mymodel. question showed use str() method examine return objects, , best place start. can consult page ?pcr explains return list of named components plus "all components returned underlying fit function". pcr(), underlying fit function svdpc.fit, , page function details remaining items in my model list.

as can see below, mymodel$loadings numeric vector 2 named dimensions (through list dimnames), known matrix. use of [ operator slice out first column (in code, named "comp 1" since both more clear , less break should column order ever change) returns simple numeric vector because have chosen single column matrix. can tell not matrix because has no dimensions, length.

> str(mymodel$loadings)  loadings [1:4, 1:2] 0.654 0.136 0.415 -0.618 0.165 ...  - attr(*, "dimnames")=list of 2   ..$ : chr [1:4] "a1" "a2" "a3" "a4"   ..$ : chr [1:2] "comp 1" "comp 2" > is.matrix(mymodel$loadings) [1] true  > str(mymodel$loadings[, "comp 1"])  named num [1:4] 0.654 0.136 0.415 -0.618  - attr(*, "names")= chr [1:4] "a1" "a2" "a3" "a4"  > dim(mymodel$loadings[, "comp 1"]) null 

the way r drops dimensions when extract single-column slice not behaviour prefer, if want write general code returns same object type. 1 way around this, colnames() would work you, use drop = false argument in [. (see ?"[") keeps matrix dimensions , dimnames:

> altslice <- mymodel$loadings[, "comp 1", drop = false] > colnames(altslice) [1] "comp 1" > dim(altslice) [1] 4 1 > is.matrix(altslice) [1] true 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -