2017-09-28 66 views
0

我想創建不具備條件的病人誰,在多個測試日期查找列表。我查找data.frame超過32892意見長,但這裏是一個小例子查找和刪除變量,基於以下因素

PatientName <- c("Alice", "Tom", "Dave", "Michele", "Wendy", "Alice", "Tom", "Wendy", "Alice", "Wendy") 
SubjectiveCondition <- c("Dizziness", "Normal", "OK", "High Heart Rate", "Normal", "Good", "High Heart Rate", "Dizziness", "Normal", "High Heart Rate") 
Date <- c("01/09/2017", "01/09/2017", "01/09/2017", "01/09/2017", "01/09/2017", "02/09/2017", "02/09/2017", "02/09/2017", "03/09/2017", "03/09/2017") 

lookupdf <- data.frame(PatientName, SubjectiveCondition, Date) 

然後我有一個包含每個病人在每個日期生理樣本進行研究實驗data.frame。一個例子是:

PatientName <- c("Alice", "Tom", "Dave", "Michele", "Wendy", "Alice", "Tom", "Wendy", "Alice", "Wendy") 
Sample <- c(120, 110, 120, 150, 210, 300, 290, 110, 150, 260) 
Date <- c("01/09/2017", "01/09/2017", "01/09/2017", "01/09/2017", "01/09/2017", "02/09/2017", "02/09/2017", "02/09/2017", "03/09/2017", "03/09/2017") 
experimentaldf <- data.frame(PatientName, Sample, Date) 

我希望在每個日期查找每一個病人,如果他們有「頭暈」或「高心率」,然後從修改experimentaldfexperimentaldf

我預期的輸出中排除將會是:

PatientName <- c("Tom", "Dave", "Wendy", "Alice", "Alice") 
Sample <- c(110, 120, 210, 300, 150) 
Date <- c("01/09/2017", "01/09/2017", "01/09/2017", "02/09/2017", "03/09/2017") 
revisedexperimentaldf <- data.frame(PatientName, Sample, Date) 

這是可以做到的嗎?

回答

1

假設每個病人都有一個唯一的ID(所以沒有患者使用相同的名稱),你可以留下的「PatientName」和「日期」加入experimentaldf上lookupdf。

join_set = merge(lookupdf,experimentaldf,by=c("PatientName", "Date"),all.x = TRUE) 

後,您可以使用子集()來選擇你需要根據你的條件組。

total_exp = subset(merged_set, SubjectiveCondition != "Dizziness" & SubjectiveCondition !="High Heart Rate")