Creating a new column with breaks

First, here's a nice way to setup your data for the reprex:

df <- data.frame(
  ID = c(1, 2, 3, 4, 5 ,6, 7, 8, 9, 10), # ID of patient
  Age = c(22, 55, 90, 7, 14, 100, 72, 85, 91, 43),# Age of patient
  Gender = c(1, 2, 2, 2, 1, 1, 2, 1, 1, 2)# Gender, 1 = Male, 2 = Female
)

I think cut could still work for you (I think you want slightly different breaks...).


library(dplyr)
df %>% 
  mutate(
    age_cut = cut(Age, breaks = c(0,30,35,40,Inf))
  )
#>    ID Age Gender  age_cut
#> 1   1  22      1   (0,30]
#> 2   2  55      2 (40,Inf]
#> 3   3  90      2 (40,Inf]
#> 4   4   7      2   (0,30]
#> 5   5  14      1   (0,30]
#> 6   6 100      1 (40,Inf]
#> 7   7  72      2 (40,Inf]
#> 8   8  85      1 (40,Inf]
#> 9   9  91      1 (40,Inf]
#> 10 10  43      2 (40,Inf]

Created on 2018-05-21 by the reprex package (v0.2.0).