Reputation: 81
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!
Upvotes: 0
Views: 62
Reputation: 6226
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)
Upvotes: 1
Reputation: 46
there're many ways to go about this task in R and here's my suggestion
tibble(age=1:99L) %>%
mutate(age_recoded=if_else(age %in% c(1:30),1L,
if_else(age %in% c(31:50),2L,3L))) %>%
count(age_recoded)
. I hope that helps.
Upvotes: 0