I have a spreadsheet Y that has thousands of rows
I would like to extract a few hundred specific rows identified by key X
I used intersect function to generate object Z as below, but do not know how to proceed.
Many thanks
Z<-intersect(X$PatientID, Y$Patient.ID)
CodePudding user response:
Try using %in%
:
Sample Data
Y <- data.frame(PatientID = LETTERS,
Var1 = 1:26)
key <- data.frame(PatientID = c("A", "W", "V"),
Var1 = c(1, 22:23))
Using %in%
:
want <- Y[Y$PatientID %in% key$PatientID, ]
Output:
# PatientID Var1
# 1 A 1
# 22 V 22
# 23 W 23
Note if you needed to use intersect
, you would just do this:
Z <- intersect(Y$PatientID, key$PatientID)
want <- Y[Y$PatientID %in% Z,]
And it would give you the same output
CodePudding user response:
You can merge:
base R
merge(Y,unique(X[,"PatientID",drop=F]))
dplyr:
dplyr::inner_join(Y, dplyr::distinct(X,PatientID))