I have two data tables in R: DT1
having about 30K obervations of 5 variables: userID
, userName
, productID
, productName
, usersRate
, DT2
having only 500 observations of 2 variables: productID
, similarProductID
. I want to find all rows from DT1
which productID
is same as similarProductID
from DT2. I've tried DT1[which(DT1$productID==DT2$similarProductID)]
and DT1[which(DT1$productID==intersect(DT1$productID,DT2$similarProductID))]
but it didn't worked out, I receive too few observations. Any ideas how could I do this?
Fastest way is with a join:
#mock data
DT1<-data.table(userID=1:30000,userName=sample(LETTERS,30000,T),productID=30001:60000,productName=sample(LETTERS,30000,T),userRate=runif(30000))
DT2<-data.table(productID=1:500,similarProductId=sample(30001:60000,500))
#set keys
setkey(DT1,productID)
setkey(DT2,similarProductId)
#join
DT1[DT2]
productID userID userName productName userRate productID.1
1: 30014 14 L R 0.87649196 473
2: 30025 25 E A 0.02237395 326
3: 30027 27 H Z 0.43986360 198
4: 30065 65 V K 0.33047666 240
5: 30123 123 R X 0.38637559 210
---
496: 59575 29575 U A 0.41036652 214
497: 59665 29665 C E 0.67345907 45
498: 59724 29724 F Y 0.18853101 81
499: 59764 29764 D X 0.50271854 386
500: 59790 29790 Z A 0.02222698 397
I doubt that this way would be faster than Troy's join answer, but you can use the merge function in R.
DT1<-data.frame(userID=1:30000,userName=sample(LETTERS,30000,T),productID=30001:60000,productName=sample(LETTERS,30000,T),userRate=runif(30000))
DT2<-data.frame(productID=sample(30001:60000,500), similarproductID=sample(30001:60000,500))
colnames(DT2)<-c("BadproductID","productID") #Do this to match the colname in DT1
DTMerged<-merge(DT1,DT2, by="productID") #Should give you all your matches without NA's
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.