HajarM
HajarM

Reputation: 351

How can extract row names after PCA implementation?

I am reducing the dimensional of a test DataFrame(contain 30rows and 750 colunm) with PCA model with PCA (using the FactoMineR library) as follows:

pca_base <- PCA(test, ncp=5, graph=T)

I used function dimdesc() [in FactoMineR], for dimension description,to identify the most significantly associated variables with a given principal component as follow:

pca_dim<-dimdesc(pca_base)

pca_dim is a list of 3 length.

My question is How can I extract row names of pca_dim from the list[1] and list[2]??.

I try this code:

#to select dim 1,2 use axes
pca_dim<-dimdesc(pca_base,axes = c(1,2))

rownames(pca_dim[[1]])

But the result was NULL.

For instant, I'll use the demo data sets decathlon2 from the factoextra package:data(decathlon2) It contains 27 individuals (athletes) described by 13 variables.

library(factoextra)
data(decathlon2)


decathlon2.active <- decathlon2[1:23, 1:10]
res.pca <- PCA(decathlon2.active,scale.unit = TRUE, graph = FALSE)

res.desc <- dimdesc(res.pca, axes = c(1,2))

Thanks!

Upvotes: 2

Views: 922

Answers (2)

Rui Barradas
Rui Barradas

Reputation: 76432

When you have that kind of issues, to access information on an R object, the best way to solve them is to start by examining the output of function str.

str(pca_dim)
#List of 2
# $ Dim.1:List of 1
#  ..$ quanti: num [1:8, 1:2] 0.794 0.743 0.734 0.61 0.428 ...
#  .. ..- attr(*, "dimnames")=List of 2
#  .. .. ..$ : chr [1:8] "Long.jump" "Discus" "Shot.put" "High.jump" ...
#  .. .. ..$ : chr [1:2] "correlation" "p.value"
# $ Dim.2:List of 1
#  ..$ quanti: num [1:3, 1:2] 8.07e-01 7.84e-01 -4.65e-01 3.21e-06 9.38e-06 ...
#  .. ..- attr(*, "dimnames")=List of 2
#  .. .. ..$ : chr [1:3] "Pole.vault" "X1500m" "High.jump"
#  .. .. ..$ : chr [1:2] "correlation" "p.value"

So the structure of the object is simple, it is a list of two lists. Each of these sublists has just one member, a matrix with the dimnames attribute set.
So you can use standard accessor functions to get those attributes.

rownames(pca_dim$Dim.1$quanti)
#[1] "Long.jump"    "Discus"       "Shot.put"     "High.jump"    "Javeline"    
#[6] "X400m"        "X110m.hurdle" "X100m"

rownames(pca_dim$Dim.2$quanti)
#[1] "Pole.vault" "X1500m"     "High.jump"

Upvotes: 2

Terru_theTerror
Terru_theTerror

Reputation: 5017

You have to move the result of dimdesc to data.frame for each element, like this:

rownames(data.frame(res.desc[1]))
[1] "Long.jump"    "Discus"       "Shot.put"     "High.jump"    "Javeline"     "X400m"        "X110m.hurdle"
[8] "X100m"       
> rownames(data.frame(res.desc[2]))
[1] "Pole.vault" "X1500m"     "High.jump" 

Upvotes: 1

Related Questions