I have my dataset such that
df <- data.frame(ID = c("m1","m2","m3","m4","m5","m6","m2","m3","m5","m6","m1","m4","m5"),
Year = c(1,1,1,1,1,1,2,2,2,2,3,3,3))
and want to perform a check whether the ID appears in the previous year. Now I have a code that seems to work
df$Check <- apply(df, 1, function(x) x["ID"] %in% df[df$Year == (as.numeric(x["Year"]) - 1), "ID"])
but given that my dataset is 3million rows long this function takes far too long to run. Is there a better alternative to this??