0

I have a data frame* with column labels. How can I use, or show, those column labels instead of column names when, for instance, using prcomp? (The function get_label below, is from the package sjlabelled.)

> get_label(spss.wvs) %>% head(2)
           wvs_e069_01                wvs_e069_02 
"Confidence: churches" "Confidence: armed forces" 

What I get (through prcomp) is:

> pca.wvs$rotation %>% head(2)
                PC1        PC2
wvs_e069_01 -0.08513771 0.45688379 
wvs_e069_02 -0.23062304 0.05508813 

What I want is:

> pca.wvs$rotation %>% head(2)
                PC1        PC2
"Confidence: churches" -0.08513771 0.45688379 
"Confidence: armed forces" -0.23062304 0.05508813 

* The dataset is imported from (dropbox link to .sav) through package haven in the following fashion:

library(haven)
haven.imp <- read_spss("qog_std.sav")
library(dplyr)
spss.wvs <- haven.imp %>% dplyr:: select(starts_with("wvs_e069_0"), starts_with("wvs_e069_1"), grep("wvs_e069_20", names(haven.imp)))

(I'm sure there are more effective ways of getting "everything between wvs_e0690_00 and wvs_e0690_20", but at least this does the trick. Pointers are appreciated though. As you can tell, I am a beginner at .)

Output of str(spss.wvs[1]) is:

Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   193 obs. of  1 variable:
 $ wvs_e069_01: num  NA NA NA 3.17 NA ...
  ..- attr(*, "label")= chr "Confidence: churches"
  ..- attr(*, "format.spss")= chr "F8.2"
krissen
  • 63
  • 10
  • Where does function `label` come from? And can you post sample data? Please edit **the question** with the output of `dput(spss.wvs)`. Or, if it is too big with the output of `dput(head(spss.wvs, 20))`. – Rui Barradas Feb 12 '19 at 15:10
  • @RuiBarradas Thanks for taking the time to comment! Posted link to dataset. Also changed from (Hmisc) function `label` to (sjlabelled) `get_label`. I'd add the output of `dput(head...)`, but I'm not sure it would add anything, since, for instance, labels aren't shown in that output? – krissen Feb 12 '19 at 17:14
  • Added output of `str(...)` instead of `dput(...)`; seemed it might be more helpful, as it contains the attribute label, which is what I'd like to work with, so to say. – krissen Feb 12 '19 at 17:26

1 Answers1

0

Ah! I can do:

> library(sjlabelled)
> rownames(spss.wvs$rotation) <- get_label(wvs)

Which gives me

> spss.wvs$rotation %>% head(2)
                                 PC1        PC2
Confidence: churches     -0.08513771 0.45688379
Confidence: armed forces -0.23062304 0.05508813
krissen
  • 63
  • 10