It's useful to keep your code concise and avoid replication.
Unless I am misunderstanding something (wouldn't be the first time) cut can give you the same result, though with slightly different category text.
for example for the first few categories;
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, 33, 85, 91, 43),# Age of patient
Gender = c(1, 2, 2, 2, 1, 1, 2, 1, 1, 2)# Gender, 1 = Male, 2 = Female
)
library(dplyr)
df %>%
mutate(
age_cut = cut(Age, breaks = c(0,29,34,39,Inf))
)
#> ID Age Gender age_cut
#> 1 1 22 1 (0,29]
#> 2 2 55 2 (39,Inf]
#> 3 3 90 2 (39,Inf]
#> 4 4 7 2 (0,29]
#> 5 5 14 1 (0,29]
#> 6 6 100 1 (39,Inf]
#> 7 7 33 2 (29,34]
#> 8 8 85 1 (39,Inf]
#> 9 9 91 1 (39,Inf]
#> 10 10 43 2 (39,Inf]
Created on 2018-05-21 by the reprex package (v0.2.0).
The category (29,34] is equivalent to your 30 - 34 range.
The breaks arguments is where you can set these ranges. For example with breaks = c(0,29,34,39,Inf), the bins will be set between 0, 29, 34, 39 and infinity.