0

I am dealing with a dataframe with an "Age" column varying from 1 to 100, and a "Clothing" column indicating 6 types of cloths. I want to divide the age values into 5 categories: 25 and under, 26-35, 36-45, 46-64, 65 and above. For example, having a new age column with character strings of those 5 categories. How do I do that? Thanks a lot in advance!

  • 1
    You are getting down voted (not me) because you did not provide a minimal example: see [mcve] for next time – denis Dec 02 '19 at 21:24

1 Answers1

1

cut() is a great function to use here. Use the labels argument to cut() to control the labels:

d <- data.frame(
  age = c(5, 27, 38, 59, 88)
)

d$age_cat <- cut(
  d$age, 
  breaks = c(0, 25, 35, 45, 64, Inf), 
  labels = c("25 and under", "26-35", "36-45", "46-64", "65 and above")
  )

d
#>   age      age_cat
#> 1   5 25 and under
#> 2  27        26-35
#> 3  38        36-45
#> 4  59        46-64
#> 5  88 65 and above

Created on 2019-12-02 by the reprex package (v0.3.0)

davechilders
  • 8,693
  • 2
  • 18
  • 18