[R] R Processing dataframe by group - equivalent to SAS by group processing with a first. and retain statments

Naresh Gurbuxani n@re@h_gurbux@n| @end|ng |rom hotm@||@com
Thu Nov 28 04:35:03 CET 2024


In addition to many good solutions already provided, this solution uses data.table package.

library(data.table)
mydf <- data.frame(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)))
setDT(mydf)
mydf[, `:=`(firstdate = with(.SD, min(date))), by = .(id)]
setDF(mydf)

On Nov 27, 2024, at 11:30 AM, Sorkin, John <jsorkin using som.umaryland.edu> wrote:

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))


	[[alternative HTML version deleted]]



More information about the R-help mailing list