Let say, we have the following
library(data.table); library(zoo)
dt <- data.table(grp = c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3), period = seq.Date(from = as.Date('2014-01-01'), to = as.Date('2014-05-01'), by = 'month'), x=c(1:15), y=c(11:25))
dt[, period:=as.yearmon(period, '%Y-%m-%d')]
return,
    grp   period  x  y
 1:   1 Jan 2014  1 11
 2:   1 Feb 2014  2 12
 3:   1 Mar 2014  3 13
 4:   1 Apr 2014  4 14
 5:   1 May 2014  5 15
 6:   2 Jan 2014  6 16
 7:   2 Feb 2014  7 17
 8:   2 Mar 2014  8 18
 9:   2 Apr 2014  9 19
10:   2 May 2014 10 20
11:   3 Jan 2014 11 21
12:   3 Feb 2014 12 22
13:   3 Mar 2014 13 23
14:   3 Apr 2014 14 24
15:   3 May 2014 15 25
I want to update columns x and y using value related to March 2014.  The return that I expect will be as follows:
    grp   period  x  y
 1:   1 Jan 2014  1 11
 2:   1 Feb 2014  2 12
 3:   1 Mar 2014  3 13
 4:   1 Apr 2014  3 13
 5:   1 May 2014  3 13
 6:   2 Jan 2014  6 16
 7:   2 Feb 2014  7 17
 8:   2 Mar 2014  8 18
 9:   2 Apr 2014  8 18
10:   2 May 2014  8 18
11:   3 Jan 2014 11 21
12:   3 Feb 2014 12 22
13:   3 Mar 2014 13 23
14:   3 Apr 2014 13 23
15:   3 May 2014 13 23
I have tried the following code, but it only uses the values from row 3.
dt[which(period > dt[3, period]),`:=`(x=dt[3, x], y = dt[3, y]), by=grp]
Could you please give suggestions?
 
     
     
    