Search code examples
rlistpca

Running PCA on list of dataframes in R


I have a small problem. I have three dataframes which the column names are identical to each other. I tried to run three different PCAs using lapply for these three dataframes. However it didnt work. I have attach my code here. Any help is appreciated.

library(factoextra)
library(FactoMineR)
mtcars
listA<-mtcars%>%
  nest(-cyl)
listA$data # Here I created 3 list of dataframes based on cylinder capacity
lapply(listA$data,function(x[,1:6]) fviz_pca_biplot(PCA(x), label = "var",  # Then tried to run separate PCA for each list; selecting column 1:6 in each dataframe
                                          geom.ind="point",
                                          pointsize=4,
                                          alpha.ind = 0.8,
                                          col.ind =x[[9]], # Here I tried to make color by 'gear type"
                                          col.var = 'black',
                                          select.var = list(contrib=30),
                                          repel=TRUE,
                                          mean.point=FALSE,
                                          #habillage = as.factor(B$Class),
                                          theme_classic()))

Solution

  • We need to specify the argument name i.e. ggtheme for the theme_classic()

    out <- lapply(listA$data,function(x) fviz_pca_biplot(PCA(x[, 1:6]), label = "var",  # Then tried to run separate PCA for each list; selecting column 1:6 in each dataframe
                                               geom.ind="point",
                                               pointsize=4,
                                               alpha.ind = 0.8,
                                               col.ind = x[[9]], # Here I tried to make color by 'gear type"
                                               col.var = 'black',
                                               select.var = list(contrib=30),
                                               repel=TRUE,
                                               mean.point=FALSE,
                                               habillage = as.factor(x[[9]]),
                                               ggtheme = theme_classic()))