2

It might sound stupid but:

I have a list for every country BE, AT, DE, etc. that I have performed pca, in a loop:

countries <- c("BE","BG","CZ","DK","DE","EE","IE","EL","ES","FR",
               "HR","IT","CY","LV","LT","HU","MT","NL","AT","PL","PT",
               "RO","SI","SK","FI","SE","UK")

for (x in countries){
  pca_list[[x]] <_ prcomp(pcaData_list[[x]], scale=TRUE)
}

Next I want a nice biplot, so I am using a ggbiplot from github("vqv/ggbiplot"), so I put the ggbiplot in the loop and I have the following:

for (x in countries){
  pca_list[[x]] <- prcomp(pcaData_list[[x]],scale=TRUE)
  ggbiplot(pca_list$x,scale=1,varname.size =0,varname.abbrev=1)
}

However, it doesn't work. I have tried replacing the pca_list$x with paste0("pca_list$",x) in the ggbiplot command but it still doesn't work.

Both tries give me an error:

Expected a object of class prcomp, princomp, PCA, or lda

Further, when I do the same for one country in specific, say AT I do get a result.

ggbiplot(pca_list$AT,scale=1,varname.size =0,varname.abbrev=1)
Z.Lin
  • 28,055
  • 6
  • 54
  • 94

1 Answers1

0

Using the $ operator to access the elements of a list does not allow variable substitution. So when you specify pca_list$x, R searches for an item in the list literally labeled "x", which does not exist (and is therefore of class "NULL"). You probably need to change the code to:

for (x in countries){
  pca_list[[x]] <- prcomp(pcaData_list[[x]],scale=TRUE)
  ggbiplot(pca_list[[x]],scale=1,varname.size =0,varname.abbrev=1) # note the change here
}
jdobres
  • 11,339
  • 1
  • 17
  • 37
  • This also doesn't work, because even for one country, I have to declare it as: pca_list$DE, pca_list[[DE]] does not work for some reason. Thanks for your reply. – GeorgiosStrat Mar 10 '19 at 15:15
  • would it be any possible way to make R search for x in countries such as: ggbiplot(pca_list$paste0("",x),scale=1,varname.size =0,varname.abbrev=1) This also doesn't work btw – GeorgiosStrat Mar 10 '19 at 15:23
  • I think you may have been confused by the code snippet I originally chose. I have updated my answer with a more complete loop. – jdobres Mar 10 '19 at 15:24