0

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?

lasagna
  • 135
  • 1
  • 10
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. Does the data.frame actually have labels? Normally you need the column names to fit a regression, not the labels. Does `names(df)` not give you what you need? – MrFlick Mar 24 '22 at 02:01
  • Well, I need the labels cause the name of variables are confuse so label give me a description of what they are. But seems that its my R that its giving me problems, when I try ```names(df)``` the console give me something like this, with any dataframe ```"\x64\x69\x72\x65\x63\x74\x6f\x72\x69\x6f" ``` @MrFlick – lasagna Mar 24 '22 at 02:06
  • It wounds like the data was imported with the wrong encoding. Hard to say without any sort of reproducible example to actually test with here. – MrFlick Mar 24 '22 at 02:12
  • You can try running `make.names("\x64\x69\x72\x65\x63\x74\x6f\x72\x69\x6f")`, which will resolve to *directorio* in this particular case. – Konrad Mar 24 '22 at 09:50

2 Answers2

1

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
lasagna
  • 135
  • 1
  • 10
1

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)

Konrad
  • 17,740
  • 16
  • 106
  • 167