Creating age categories

Hi there, i'm new to R and this is my first question!
I'm trying to create a new variable 'Age_category' by cutting ClientAge into groups, however I keep getting error messages that the object is not found. I used
Better_health1$ClientAge <- as.numeric(Better_health1$ClientAge) to ensure that ClientAge was numeric, and it identifies ClientAge when typing the code. Can anyone help me out with this??

Better_health1 %>%
Better_health1$Age_category <- cut(ClientAge, c(-1, 9, 18, 40, 60, 80, 110), labels=c("0-9", "10-18", "18-40", "40-60", "60-80", "80+"))
#> Error in cut(ClientAge, c(-1, 9, 18, 40, 60, 80, 110), labels = c("0-9", : object 'ClientAge' not found


shows all the variables. If ClientAge is missing, that would account for the error.

1 Like

Thanks @technocrat. I've just done that check, and the variable is definitely there. I tested it with some other variables too, which didn't work either.
Any other ideas?

Hi, Can you provide a reproducible example?

1 Like

Hi @williaml
Thanks for this. Yes i have tried to do the reprex, and have made one but can't work out how to copy/paste it over. Do you have a tip for this?
I think i have worked out a solution using if else!

Better_health1$Age_category1 <- as.factor(ifelse(Better_health1$ClientAge <10, "0-9",
ifelse(Better_health1$ClientAge <19, "10-18",
ifelse(Better_health1$ClientAge <41, "18-40",
ifelse(Better_health1$ClientAge <61, "40-60",
ifelse(Better_health1$ClientAge <81, "60-80",
ifelse(Better_health1$ClientAge >80.99, "80+", 0)))))))
#> Error in ifelse(Better_health1$ClientAge < 10, "0-9", ifelse(Better_health1$ClientAge < : object 'Better_health1' not found

Hi Alice, perhaps try this. Paste the output of dput(head(Better_health1)) or datapasta::dpasta(head(Better_health1)).

1 Like

I like case_when as an ifelse alternative in cases like these.

1 Like

Hi @williaml. I'm a little confused- is this for how to copy/paste a reprex? Or inital question?
My output is a long list, this is the ClientAge and Age_category1 parts:

ClientAge = c(22, 78, 74, 26, 43, 77),

Age_category1 = as.factor(c("18-40",

I think i have solved the initial question now, even though the reprex showed 'object 'Better_health1' not found'. But any more tips on copy/pasting the reprex properly are still much appreciated.

Hi Alice, for the reprex, see the message posted earlier. Basically, it was this:

Paste the output of dput(head(Better_health1)) or datapasta::dpasta(head(Better_health1)) .

1 Like

I would use cut insted of if_else or case_when-formulas:


df <- tibble::tribble(
  ~person, ~age,
  "A",   5L,
  "B",  27L,
  "C",  66L,
  "D",   9L,
  "E",   4L,
  "F",   8L,
  "G",   1L,
  "H",  58L,
  "I",  44L,
  "J",  30L,
  "K",  19L,
  "L",  18L,
  "M",  87L,
  "N",  23L,
  "O",   5L,
  "P",   30L,
  "Q",   40L,
  "R",   60L,
  "S",   80L,
  "T",   79L,
  "U",   0L

dfage_grp <- df %>% 
  mutate(age_group = cut(age, breaks = c(0, 18, 40, 80, 100), include.lowest = T, labels = c("0-18","19-40", "41-80", "81-100")))

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.