Actually, Matthew DeAngelis beat me by a few seconds, so props to him.

But here's another solution that makes use of a keyed table, which might
be a bit faster if your data is large (although the previous solution
should also be fine for larger data).

DT <- data.table(obs=1:7, id=c(1,1,1,4,4,4,4), time=c(3,4,7,5,8,10,15))
DT[, value := 0]
setkey(DT, id)
DT[DT[, .I[1], by = key(DT)]$V1, value := 2]
DT[DT[, .I[.N], by = key(DT)]$V1, value := 1]
DT



On 07/25/2014 11:24 PM, Frank S. wrote:
> Thank you Michael !!
> 
_______________________________________________
datatable-help mailing list
[email protected]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help

Reply via email to