I have the following data:
data <- data.frame(id_pers=c(1, 2, 3, 4, 5),
Birthyear=c(2018, 2009, 2008, 2000, 1998,2005),
family=c(Elliot, Elliot, Elliot, Gerrard, Gerrard,Gerrard)
I want to find the maximal difference (in birthyear) in each family, that is the same for all the family-members in the following.
It should look like:
datanew <- data.frame(id_pers=c(1, 2, 3, 4, 5, 6),
Birthyear=c(2018, 2009, 2008, 2000, 1998, 2005),
family=c(Elliot, Elliot, Elliot, Gerrard, Gerrard, Gerrard),
maxdifference=c(10,10,10,7,7,7)
Obligatory base-r one-liner
data$maxdifference = ave(data$Birthyear, data$family, FUN = \(years) max(years) - min(years))