I have a primary table
library(data.table); set.seed(42)
D1 <- data.table(id=rep(1:3,each=2), name=rep(c("a","b")), val=runif(6,0,1))
> D1
   id name       val
1:  1    a 0.9148060
2:  1    b 0.9370754
3:  2    a 0.2861395
4:  2    b 0.8304476
5:  3    a 0.6417455
6:  3    b 0.5190959
giving two values for each id, named a and b. The value I want to select is determined in a secondary table
D2 <- data.table(id=1:3, name=c("a","a","b"))
and I would like to subset D1 using D2. I could do this ID by ID in a loop but given the dimensions of my data I hope that there is a more efficient solution, perhaps using a join. 
 
     
    