It works:
dt[, DateLag := Date - 7L ]
w = dt[dt, which = TRUE, on = c("Name", Date = "DateLag"), roll = "nearest"]
dt[ , `:=`(ScoreLag = Score[replace(w, w == .I, NA_integer_)], DateLag = NULL)]
Name Score Date ScoreAround7DaysAgo ScoreLag
1: John 9 2016-01-01 NA NA
2: John 6 2016-01-10 9 9
3: John 3 2016-01-17 6 6
4: John 5 2016-01-18 6 6
5: Tom 9 2016-01-01 NA NA
6: Tom 6 2016-01-10 9 9
7: Tom 3 2016-01-17 6 6
8: Tom 5 2016-01-18 6 6
He finds the closest date to Date-7, but discards it if it is again the same Date.
Frank source
share