简体   繁体   中英

R Combine Two Data Frames, Remove Unique Patient IDs

I have two data sets: OriginalPTs and RetestPTs. Each has three columns, PID, Speed, and Group. Group is either "original" or "retest" depending on which data frame. All values for RetestPTs$PID are in OriginalPTs$PID, but OriginalPTs$PID contains additional entries which I do not want to use.

I would like to create a new data frame that consists of PID, Speed and Group, but only for the PIDs which are found in both original data frames. I am very new to R and coding in general, so any help would be appreciated. Thanks!

head(OriginalPTs): 
  pid    speed    group
1 ALFE 1.418733 Original
2 ALFE 1.187550 Original
3 ALFE 0.536114 Original
4 ALFE 1.350950 Original
5 ALFE 1.505700 Original
6 ALFE 1.907850 Original
head(RetestPTs): 
 pid     speed  group
1 ALFE 1.4636500 Retest
2 ALFE 0.7094775 Retest
3 ALFE 0.4197050 Retest
4 ALFE 1.2798667 Retest
5 ALFE 1.4745000 Retest
6 ALFE 1.7771000 Retest

There are multiple entries for each PID.

What I tried:

left_join(OriginalPts,RetestPTs)

Which produces a vertical join instead of a horizontal one.

I would like to see the two data frames side by side. I also have a trial number for each one if that makes it easier.

Does this work?

library(dplyr)
inner_join(OriginalPTs, RetestPTs, by = c("pid")

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM