Personally, I'd do this in the tidyverse with dplyr and its row_number() function.
olddata %>% group_by(ID) %>% mutate(first = as.integer(row_number() == 1)) --Chris Ryan Sorkin, John wrote: > ID <- c(rep(1,10),rep(2,6),rep(3,2)) > date <- c(rep(1,2),rep(2,2),rep(3,2),rep(4,2),rep(5,2), > rep(5,3),rep(6,3),rep(10,2)) > olddata <- data.frame(ID=ID,date=date) ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide https://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.