0

I have data that looks like this:

ID      Age
55687   18
55688   24
55689   26
55690   33
55691   36
55692   43
55693   46
55694   54
55695   57
55696   63
55697   65
55698   70
55699   76
55700   83
55701   85
55702   88
55703   89+
55704   89+

What I want to do is convert "Age" into the following age Brackets: 18-24, 25-34, 35-44, 45-54, 55-64, 65-74, 75-84, 85-89, 89+ and store into a third column ("Age Brackets") as follows:

ID      Age  Age Bracket
55687   18   18-24
55688   24   18-24
55689   26   25-34
55690   33   25-34
55691   36   35-44
55692   43   35-44
55693   46   45-54
55694   54   45-54
55695   57   55-64
55696   63   55-64
55697   65   65-74
55698   70   65-74
55699   76   75-84
55700   83   75-84
55701   85   85-89
55702   88   85-89
55703   89+  89+
55704   89+  89+
Melderon
  • 365
  • 1
  • 16
  • 1
    `dat$Age2 <- cut(as.numeric(dat$Age), c(18, 25, 35, 45, 55, 65, 75, 85, 89, Inf), labels = c("18-24", "25-34", "35-44", "45-54", "55-64", "65-74", "75-84", "85-89", "89+"), include.lowest = TRUE)` gets you all but the `89+`, since those are not really numbers ... those you can do a simple `grepl` to replace. (Note that this returns a `factor`, so `levels` and/or `as.character` as applicable/appropriate.) – r2evans Aug 19 '20 at 17:41
  • Your data may have `factor`s instead of strings, that's one problem with posting data like this: it's ambiguous. Try replacing `as.numeric(dat$Age)` with `as.numeric(as.character(dat$Age))`. – r2evans Aug 19 '20 at 18:15
  • 1
    Nevermind, it worked. I didn't call the right column name. Thanks so much! So, all I need to do is convert NA's to 89+. Thanks a ton! – Melderon Aug 19 '20 at 18:16

0 Answers0