For the following code:
x <- data.frame(year = c(1730, 1860, 1941, 2011))
century_bands <- data.frame(min_year = c(1700, 1800, 1900, 2000),
max_year = c(1799, 1899, 1999, 2099),
century_name = c("18th", "19th", "20th", "21st"))
I'd like, for each value in x
, to work out the name of the century it falls into, using the information in century_bands
. I can't imagine this is difficult to achieve but I can't figure it out. Can anyone help please? Is there a way using the dplyr
package (which I use quite a lot) or perhaps some other technique?
This is just a very simple example of a real-life situation where the bands aren't in nice neat 100 year steps - so any shortcuts based on dividing the year by 100 etc. won't work unfortunately.
Thank you.