I have a data.frame with three columns: a token, year of birth and number of contacts. The birthyears range from 1934 to 2020 and I don't want individual years but 5-year groups like 2000-2005, 2006-2010 and so on to later visualize the contact count per age group.
I already found the cut
function like this:
# set up cut-off values
breaks <- c(0,2,4,6,8,10,12,14,16,18,20)
# specify interval/bin labels
tags <- c("[0-2)","[2-4)", "[4-6)", "[6-8)", "[8-10)", "[10-12)","[12-14)", "[14-16)","[16-18)", "[18-20)")
# bucketing values into bins
group_tags <- cut(v$MeanEducation,
breaks=breaks,
include.lowest=TRUE,
right=FALSE,
labels=tags)
However in this example I'd have to set a vector of breaks and labels manually.
Is there a solution to automize this? Like beginning the first bucket at the next lower by 5 dividable year than the minimum in my dataframe. Analogue at the top end.
Thanks in advance!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…