R function to list labels of columns of .sav-Dataset

69 Views Asked by At

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.

1

There are 1 best solutions below

1
On

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)