I have a dataframe with a few columns, one of those columns is ranks, an integer between 1 and 20. I want to create another column that contains a bin value like \"1-4\", \
We can use smart_cut
from package cutr
:
# devtools::install_github("moodymudskipper/cutr")
library(cutr)
Using @Andrie's sample data:
x$bins <- smart_cut(x$rank,
c(1,5,11,16),
labels = ~paste0(.y[1],'-',.y[2]-1),
simplify = FALSE)
# rank name info bins
# 1 1 steve red 1-4
# 2 3 joe blue 1-4
# 3 6 john green 5-10
# 4 3 liz yellow 1-4
# 5 15 jon pink 11-15
more on cutr and smart_cut