I have a data.table like the following:
DT<- data.table(id=c(1,1,1,1,2,2,2,2), date = c(10,11,12,13,12,12,14,18), seq = c(1,2,3,4,1,2,3,4,1,2,3,4))
setkey(DT,id)
setorder(DT,id,seq)
> DT
id date seq
1: 1 10 1
2: 1 11 2
3: 1 12 3
4: 1 13 4
5: 2 12 1
6: 2 12 2
7: 2 14 3
8: 2 18 4
I want to create a new column, "id.date" which will indicate the value of "date" of the first row per id. So, the new data.table would be:
id date seq id.date
1: 1 10 1 10
2: 1 11 2 10
3: 1 12 3 10
4: 1 13 4 10
5: 2 12 1 12
6: 2 12 2 12
7: 2 14 3 12
8: 2 18 4 12
I know I can get the value of the first row by
DT[,.SD[1,date],by=id]$V1
but how can I assign this value to all the rows of the group?
Is it possible to do the same with dplyr
?