I have this data.table:

sample:

```
id cond date
1  A1   2012-11-19
1  A1   2013-05-09
1  A2   2014-09-05
2  B1   2015-03-05
2  B1   2015-07-06
3  A1   2015-02-05
4  B1   2012-09-26
4  B1   2015-02-05
5  B1   2012-09-26

```

I want to calculate overdue days from today's date within each group of 'id' and 'cond', so I am trying to get the difference of days between the last date in each group and sys.date. Desired output is ;

```
id cond date        overdue
1  A1   2012-11-19  NA
1  A1   2013-05-09  832 
1  A2   2014-09-05  348
2  B1   2015-03-05  NA 
2  B1   2015-07-06  44
3  A1   2015-02-05  195
4  B1   2012-09-26  NA 
4  B1   2015-02-05  195
5  B1   2012-09-26  1057

```

I tried to achieve this by following code: 

```
sample <- sample[ , overdue := Sys.Date() - date[.N], by = c('id','cond')]

```

But I am getting following output, where it the value are recycling:

```
id cond date        overdue
1  A1   2012-11-19  832
1  A1   2013-05-09  832 
1  A2   2014-09-05  348
2  B1   2015-03-05  44 
2  B1   2015-07-06  44
3  A1   2015-02-05  195
4  B1   2012-09-26  195 
4  B1   2015-02-05  195
5  B1   2012-09-26  1057

```

I am not sure, how can I restrict my code to just do calculations for the last row and not recycle. I am sure there would be ways to do this, help is appreciated.