由于您只共享了一行数据,我不确定电子邮件主题行的模式from_Subject
。如果它是一个自动电子邮件系统,那么就有一个固定的电子邮件主题行模式from_Subject
。我为您提供了 3 种Patient_ID
从from_Subject
.
library(dplyr)
df1 <- data_frame(from_Email = "xxxxx@hospital.com",
Time_IN = "1/11/2000 12:00:00",
from_Subject = "Patient H2445JFLD presented into ER with .... symptoms")
df2 <- data_frame(Hospital_Name = "Hospital ABC",
Patient_ID = "H2445JFLD")
# Extract 2nd word from the subject line
df1 <- df1 %>% mutate(Patient_ID = stringr::word(from_Subject, 2))
# Extract the word after "Patient" from the subject line
df1 <- df1 %>% mutate(Patient_ID = str_extract(df1$from_Subject, '(?<=Patient\\s)\\w+'))
# Extract a word of length 9 that has characters A-Z and 0-9 from the subject line
df1 <- df1 %>% mutate(Patient_ID = str_extract(df1$from_Subject, '\\b[A-Z0-9]{9}\\b'))
一旦你提取了Patient_ID
,那么它就是你需要做的一个简单的左连接。
left_join(df1, df2, on="Patient_ID")
#Joining, by = "Patient_ID"
# A tibble: 1 × 5
# from_Email Time_IN from_Subject Patient_ID Hospital_Name
# <chr> <chr> <chr> <chr> <chr>
#1 xxxxx@hospital.com 1/11/2000 12:00:00 Patient H2445JFLD presented into ER with .... symptoms H2445JFLD Hospital ABC