MFR MFR - 1 year ago 76
R Question

subset all the data from another dataset

I wish to keep all the data from

that I have their transaction ID in

This is my data:

dts1 <- data.frame (id = c(1,2,3,4,1,1), transactionID =c (10,22,47,15,13,NA))

dts2 <- data.frame ( transactionID = c(10,9,20,22))

I have transaction ID 10 that belongs to id 1 and transaction 22 that belongs to ID 2. So, I wish to keep all the data from Id 1 and id 2 and delete data about the other ids: My ideal output will be:

id transactionID
1 10
2 22
1 13
1 NA

This one doesn't keep all the information about ids

dts3 <- dts1 [dts1$transactionID %in% dts2$transactionID,]

Answer Source

You can do it in 2 steps:

first you get the id for which you have a transaction in dts2 like this:

dts1 [dts1$transactionID %in%  dts2$transactionID ,1]
[1] 1 2

Then you get the transaction tha have these ids:

dts1[dts1$id %in% dts1[dts1$transactionID %in% dts2$transactionID ,1],]
  id transactionID
1  1            10
2  2            22
5  1            13
6  1            NA
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download