[datatable-help] New column with conditions for first and last observation by id group
Michael Smith
my.r.help at gmail.com
Sat Jul 26 02:15:04 CEST 2014
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 !!
>
More information about the datatable-help
mailing list