I'm learning data.table and I would like to reproduce computations I realized with dplyr.
I have a dataframe with daily observations for individuals identified by 2 IDs. And I would like to keep for each individual the last observation.
For example with those dummy datas :
set.seed(42)
datas <- data.frame(id1 = rep(1:2, 2), 
                    id2 = rep(letters[1:2], 2), 
                    date = seq(Sys.Date() - 3, Sys.Date(), by = "day"), 
                    x = rnorm(4))
  id1 id2       date          x
1   1   a 2015-08-02  1.3709584
2   2   b 2015-08-03 -0.5646982
3   1   a 2015-08-04  0.3631284
4   2   b 2015-08-05  0.6328626
This is the output I'm expecting :
library(dplyr)
datas %>% 
  group_by(id1, id2) %>% 
  filter(date == max(date))
Source: local data frame [2 x 4]
Groups: id1, id2
  id1 id2       date         x
1   1   a 2015-08-04 0.3631284
2   2   b 2015-08-05 0.6328626
And this is my attempt with data.table :
library(data.table)
datas.dt <- data.table(datas)
tmp <- datas.dt[, list(date = max(date)), by = c("id1", "id2")]
setkey(datas.dt, id1, id2, date)
setkey(tmp, id1, id2, date)
datas.dt[tmp]
id1 id2       date         x
1:   1   a 2015-08-04 0.3631284
2:   2   b 2015-08-05 0.6328626
I'm calling a join so I think I'm missing something in the basics, but I couldn't find a more direct way to achieve this.
Is there a more straight way with data.table to achieve what I would like to do ? Thanks.
