Say I have a dataframe that looks like the following:
dat <- data.frame("firstName" = c("John", "John", "Mary", "Bob", "Mary", "Bob"), "age"= c(21, 24, 35, 30, 20, 27))
I want to create a third variable dat$id that assign the same number if an observation's age is within +/- 5 years of another observation and has the same firstName. So the dataframe will look like this:
dat <- data.frame("firstName" = c("John", "John", "Mary", "Bob", "Mary", "Bob"), "age"= c(21, 24, 35, 30, 20, 27), "id"= c(1,1,2,3,4,3))
I have a very large dataset of names and ages and would like to find a more automated way of assigning id's. I considered creating age bins for every 5 years from age 20, but this will not match observations who are in different bins, but still within 5 years of age.
