I'm trying to recode age variables into three categroies in R but it's not allocating them properly:
data_2017_18$ageband3 <-
dplyr::recode(data_2017_18$age, '1:30' = 1L,
'31:50' = 2L, '51:99' = 3L)
I'd assume the crosstab with age would be:
ageband 1 2 3
However, when I look at the dataset, it's putting everybody's age value into an 'ageband3' variable.
Grateful for any suggestions.
Thanks!
I think there is no need for recode
. The easiest solution is to use cut
:
data_2017_18$ageband3 <- cut(data_2017_18$age, cut(1:100, breaks = c(0, 30,50, Inf))
Use cut(data_2017_18$age, breaks = c(0, 30,50, Inf), labels = c(1,2,3))
if your prefer labelling your levels 1,2 and 3. But R
handles quite well interval values ([0,30]
for instance)