im trying to use the labels from a dataset as the values instead. The dataset was imported from a .sav file using haven. I tried (and suceeded with different data) with this:
df %>%
mutate(state = get_labels(state)[state])
Now i have two problems:
- This does not work for the example below, apparently (if i got that right) because some of the values are negative? Is there a way around that? I get the error
Error in `mutate()`:
! Problem while computing `vote = get_labels(vote)[vote]`.
Caused by error in `get_labels(vote)[vote]`:
! only 0's may be mixed with negative subscripts
- Some of the columns in the dataframe are not labelled. Is there a way to only execute this if they happen to have one and else leave them be? I tried the above code on a unlabelled column and it was deleted...
Many thanks in advance, and i hope my example below is reproducible!
Example data:
structure(c(215, 1, -97, 4, -97, 5, -97, -98, 6, -97, -97, 5,
-99, -97, 7, -98, 1, -97, 1, 1), labels = c(`keine Angabe` = -99,
`weiss nicht` = -98, `trifft nicht zu` = -97, `nicht in Auswahlgesamtheit` = -94,
`ungueltig waehlen` = -83, `CDU/CSU` = 1, SPD = 4, FDP = 5, GRUENE = 6,
`DIE LINKE` = 7, BP = 126, `DIE GRAUEN` = 149, `Die PARTEI` = 151,
`DIE VIOLETTEN` = 152, DVU = 168, FAMILIE = 171, `FREIE WAEHLER` = 180,
NPD = 206, oedp = 209, PBC = 214, PIRATEN = 215, RENTNER = 224,
REP = 225, RRP = 226, Tierschutzpartei = 237, Volksabstimmung = 249,
AfD = 322, BGE = 338, DiB = 349, `andere Partei` = 801, `keine Partei; keiner Partei` = 808,
`Interview abgebrochen` = -93, `Fehler in Daten` = -92, Mehrfachnennungen = -73,
DKP = 156, MLPD = 202, SSW = 234, NM = 326, LKR = 331, `V-Partei³` = 344,
Tierschutzallianz = 345, `Die Humanisten` = 350, `Buendnis C` = 351,
`Die Grauen` = 361, Volt = 364, dieBasis = 372, `Team Todenhoefer` = 373,
BUENDNIS21 = 374, BUERGERBEWEGUNG = 378, Sonstige = 800), label = "Wahlabsicht: BTW, Zweitstimme (Version B)", class = c("haven_labelled",
"vctrs_vctr", "double"))