Home > Blockchain >  Using label as value while having negative values
Using label as value while having negative values

Time:08-19

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:

  1. 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
  1. 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"))

CodePudding user response:

If all you are trying to do is to use labels instead of the values, use unlabelled() from labelled package. You can pass your entire dataframe at once.

I transformed the dataframe in tibble because it is more organized this way. But this step is not needed.

Packages need

library(dplyr)
library(labelled)

Solution

df %>% as_tibble() %>% unlabelled()

Output

# # A tibble: 20 × 1
# value          
# <fct>          
# 1 PIRATEN        
# 2 CDU/CSU        
# 3 trifft nicht zu
# 4 SPD            
# 5 trifft nicht zu
# 6 FDP            
# 7 trifft nicht zu
# 8 weiss nicht    
# 9 GRUENE         
# 10 trifft nicht zu
# 11 trifft nicht zu
# 12 FDP            
# 13 keine Angabe   
# 14 trifft nicht zu
# 15 DIE LINKE      
# 16 weiss nicht    
# 17 CDU/CSU        
# 18 trifft nicht zu
# 19 CDU/CSU        
# 20 CDU/CSU   

 
  • Related