mardi 15 janvier 2019

How to add categorical variable to dataframe consisting of different intervalls?

I have a data frame with the variable winprop, it ranges from 0 to 1 and shows the win probability for a candidate. Now I want to add a categorical variable based on winprop to my dataset which is 'very clear' if winprop is between 0 and 0.2 and => 0.9, "clear" if winprop is between 0.2 and <= 0.4 and >0.6 and <0.9, "close race" if winprop is between 0.4 and 0.6.

I've tried to use the ifelse statement but receive error messages

vote_pol$closeness <- ifelse(vote_pol$winprop < 0.2 & vote_pol$winprop >= 0.9, 'very clear'),
                         ifelse(vote_pol$winprop 0.2:<0.4 & vote_pol$winprop 0.6:<0.9, 'clear',
                                ifelse(vote_pol$winprop 0.4:0.6, 'open race')

candidate winprop closeness
A         0.154   very clear
B         0.548   open race
C         0.992   very clear
D         0.745   clear  
E         0.325   clear

Aucun commentaire:

Enregistrer un commentaire