0

我希望从电子邮件主题行中提取患者 ID。我正在使用两个数据框:一个具有来自 SQL 数据库的输出(包含电子邮件主题行),另一个具有患者信息(医院名称和患者 ID)。

我想使用患者 ID 并从第一个数据帧中删除主题行,然后返回与所述患者关联的医院。不幸的是,我无法提供对数据的访问。

## Example Data

Data frame 1 example row:

Column 1 (from_Email): xxxxx@hospital.com 

Column 2 (Time_IN): 1/11/2000 12:00:00

Column 3 (from_Subject): Patient H2445JFLD presented into ER with .... symptoms

Data frame 2 example row:

Column 1 (Hospital Name): Hospital ABC

Column 2 (Patient ID): H2445JFLD 
4

1 回答 1

1

由于您只共享了一行数据,我不确定电子邮件主题行的模式from_Subject。如果它是一个自动电子邮件系统,那么就有一个固定的电子邮件主题行模式from_Subject。我为您提供了 3 种Patient_IDfrom_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
于 2017-02-21T01:40:33.367 回答