1

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.

bumblebee
  • 1,116
  • 8
  • 20

2 Answers2

0

We can use a join and it would be faster as we are already having data.table objects

D1[D2, on = .(id, name)]
#  id name       val
#1:  1    a 0.9148060
#2:  2    a 0.2861395
#3:  3    b 0.5190959

Or with inner_join from dplyr

library(dplyr)
inner_join(D1, D2)

Or using match in base R

D2$val <- D1$val[match(paste(D2$id, D2$name), paste(D1$id, D1$name))]
akrun
  • 874,273
  • 37
  • 540
  • 662
0

A base R solution is via using merge(), i.e.,

> merge(D2,D1)
   id name       val
1:  1    a 0.9148060
2:  2    a 0.2861395
3:  3    b 0.5190959
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81