Using data organised as
dtl <- replicate(10,data.table(id=sample(letters,10),val=sample(10)), simplify=F)
lapply(dtl, function(x){setkey(x,'id')})
I need to extract a list of datatables that contain the rows in dtl[[n+1]]] with id not present in dtl[[n]]. I assume it would be something like
dtl2 <- list(setdiff(dtl[[1]][['id']],dtl[[2]][['id']]),setdiff(dtl[[2]][['id']],dtl[[3]][['id']]...)
Please notice that, while the setdiff should only take the id column into account, I expect the result to contain all columns from each datatable.
I think this will do it for you:
Edit: with your new expected output, I would still use
mapplyas above, but with one of the following two changes:setdiffwithfunction(x,y)setdiff(x$id, y$id)dtlwithids <- lapply(dtl, "[", "id")Edit2:: you’ve changed your expected output again by adding a plain English description that does not match the code you had provided… I think you are now looking for this: