1
votes

I would like to subset data.table in a way which would provide some rows repeatedly. This works well with indices but I am not sure how to do it in a simple way with values especially when the values does not appear in one row only.

E.g.:

library(data.table)

dt<-data.table(x1=c('a','a','b','b','c','c'),x2=c(1,2,3,4,5,6))
xsel<-c('a','b','a','a','c','b')
dt[x1%in%xsel,]

will provide this output:

   x1 x2
1:  a  1
2:  a  2
3:  b  3
4:  b  4
5:  c  5
6:  c  6

I would like to get it in the original order and with repetition exactly as it is in the xsel vector. Is it possible to do it in a reasonably simple way without looping? Thanks.

1

1 Answers

2
votes

Using:

setkey(dt, x1)  # set the key
dt[J(xsel)]     # or: dt[.(xsel)]

gives:

    x1 x2
 1:  a  1
 2:  a  2
 3:  b  3
 4:  b  4
 5:  a  1
 6:  a  2
 7:  a  1
 8:  a  2
 9:  c  5
10:  c  6
11:  b  3
12:  b  4

Without setting the key, you could use:

dt[.(xsel), on = .(x1)]