0

If I have a vector of ages and a character vector of age categories, is there a way to recode the age vector numerically in ascending order according to my age categories, such that ages in "Age 0 to 20" = 1 and so on. My actual data has 20+ age categories but I've just used 4 in the example. Any help would be really appreciated, thanks.

age <- rnorm(1000, 50, 20)
agecat <- c("Age 0 to 20", "Age 21-40", "Age 41-60", "Age 61 and over")

Martin
  • 171
  • 1
  • 7
  • 1
    Try `cut(age, breaks = c(0, 20, 40, 60, Inf), labels = agecat, right = TRUE)` – markus Nov 05 '20 at 17:44
  • Do you know if there is a way to do it in general if I had several character vectors categorised differently? – Martin Nov 05 '20 at 18:04

0 Answers0