Home > Software engineering >  How print all labels of variables in R
How print all labels of variables in R

Time:03-24

I was trying to get all labels in a data frame, in order to know which variables work for a regression.

I was trying the following code, but didn't work. Assuming that df is my data frame.

library(labelled)
look_for(df)

That give me this error

Error: no more error handlers available (recursive errors?); invoking 'abort' restart

Any other way to get the labels or possible fix?

CodePudding user response:

Ok. So the quick solution its to use the function attr searching for labels, this is possible to apply to all variables using lapply

Assuming that the dataframe is df

l= lapply(df, attr, "label")
l

CodePudding user response:

The labelled package offers val_labels function that is designed for that purpose:

library("labelled")
# On provided test data
val_labels(x_spss_haven_2.0)

Results

Good  Bad 
   1    8 

The obtained results are an ordinary named vector so if your desire is to arrive at a string of labels you can do the following:

library("labelled")
res <- val_labels(x_spss_haven_2.0)
names(res)
#> [1] "Good" "Bad"

Created on 2022-03-24 by the reprex package (v2.0.1)

  •  Tags:  
  • r
  • Related