根据因素查找并删除变量

时间:2017-09-28 08:49:18

标签: r

我希望创建一个查找列表,列出没有病情的患者,在多个日期进行测试。我的查找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)

然后我有一个研究实验数据框架,其中包含每个患者在每个日期的生理样本。一个例子是:

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)

我希望在每个日期查找每位患者,如果他们有“头晕”或“高心率”,请将其排除在experimentaldf

之外

我预计的修订版experimentaldf的输出将是:

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 个答案:

答案 0 :(得分:1)

假设每个患者都有一个唯一的id(所以没有相同名字的患者),你可以通过&#34; PatientName&#34;在lookupdf上留下joindf。和&#34;日期&#34;。

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

之后,您可以使用subset()根据您的条件选择所需的组。

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