Please consider this example code:
d1 <- c(1,2,2,3,4,3)
d2 <- c(10,11,12,13,14,15)
dt <- data.frame(d1,d2)
sample.index <- c(2,3)
dt[dt$d1 %in% sample.index, ]
This returns
d1 d2
2 2 11
3 2 12
4 3 13
6 3 15
which is OK. However, if we have
sample.index <- c(2,2,3)
then the code still returns the same result. Instead I want the rows matching 2 to be returned twice because 2 appears twice in sample.index - how can I achieve this ?