0

I want to be able to read trough the variable/columns to choose useful variables for my analyis. Therefore i want to have the labels as well, since the variable names are not human-readable.

I downloaded the GLES-Dataset ZA7729 as .sav-file from https://gles.eu/bundestagswahl-2021/ and imported it into R-Studio via "import from Stata". Then i'm able to see the variables/columns with labels, my loaded data but there are so many variables, that i want a more readable format, like having a list of them printed. I got close to what i wanted by using str(ZA7729_v1_0_0) str(ZA7729_v1_0_0) but it prints a lot of additional information that i don't need and make it hard to read. I only want the line with @label or -attr( ,"label") for each variable/column I tried iterating over str(ZA7729_v1_0_0) but then i found out that it only prints and does not return anything, so that's not an option. I pretty much want colnames(ZA7729_v1_0_0) but with the human readable labels instead of just the variable name.

Nick Cox
  • 35,529
  • 6
  • 31
  • 47
Meganton
  • 1
  • 2
  • The fact that the dataset came from Stata seems irrelevant to your question, so I removed that tag. – Nick Cox Jan 31 '23 at 22:54
  • We could help more if you would make your problem reproducible. In this particular case, you could include the 1st row of your dataframe by pasting the results of `dput(ZA7729_v1_0_0)` in a code chunk. In general, good advice about how to make reproducible examples for questions can be found here: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Gregory Jan 31 '23 at 23:34
  • Yeah you wouldn't want that...its several thousand lines and just breaks of because of the length, as far as i can see. But there isn't really a problem, it's just that i'm looking for a feature. I want a human-readable list of the variables (of which there are many) with their label, like for the first row, the variable "study" i want to get the label "Studiennummer" and so on – Meganton Jan 31 '23 at 23:40

1 Answers1

1

I think the names function will do what you want. For example,

df <- data.frame(a = rnorm(5),
                 b = rnorm(5),
                 c = rnorm(5))
names(df)             
#> [1] "a" "b" "c"

Created on 2023-01-31 by the reprex package (v2.0.1)

Gregory
  • 4,147
  • 7
  • 33
  • 44
  • Unfortunately no, this outputs the same as colnames(), i.e. the incomprehensible variable names, not the labels. The output would be > names(ZA7727_v1_0_0) "study" "version" "doi" "field_start" "field_end" "sample" "lfdn" "kp21_010" "kp21_011a" "kp21_011b" "kp21_011c" "kp21_180" ... – Meganton Jan 31 '23 at 23:22