I'm very new to all of this and am (trying!) to complete an assignment for a quantitative methods module at uni. I want to recode a quantitative variable measuring number of minutes spent doing an activity. I want to create a new variable with five categories up to 1300 minutes using the case_when function.
I've tried numerous times to do this and it's just not working. R is only reading the first line of code and wont create a new variable.
Here's my code:
italy$nwspol_re <- case_when(italy$nwspol < 260 ~ "0-260 mins",
italy$nwspol >= 261 & italy$nwspol < 520 ~ "261-520",
italy$nwspol >= 521 & italy$nwspol < 780 ~ "521-780",
italy$nwspol >= 781 & italy$nwspol < 1040 ~ "781-1040",
italy$nwspol >= 1041 & italy$nwspol < 1300 ~ "1041-1300")
in the console, R just returns the first line and nothing else happens.
I have also tried the mutate function as shown, however R just returns the first line containing italy and nothing else happens, i.e., no new variable is created.
italy %\>%
mutate(nwspol_re = case_when(nwspol \>= 260 \~ '0-260 minutes'
,nwspol \>= 520 & nwspol \< 780 \~ '521-780'
,nwspol \>= 781 & nwspol \< 1040 \~ '781-1040'
,nwspol \>= 1041 & nwspol \< 1285 \~ '1041-1300'
,TRUE \~ 'F'
)
)
Any help would be appreciated.