我还没有看到这个问题的类似问题(它非常具体)。
我有三个人可以选择回答有关患者的问题。这三个人中只有两个人实际上用于任何特定的患者(在我的真实数据中,总是选择两个人,但在 10 个人中)。
如果最初的两个人不同意,则使用第三人(3rdOpinion)并且该意见优先于其他人。
因此,最终结果=第3意见结果,除非最初的两个意见相同(即3rdOpinion为NA),这种情况下最终结果只是最初的两个人给出的意见(即第一个非NA值对于那个病人的那个问题)
因此例如患者 1 问题 1,Ben 和 Chris 不同意,因此使用 3rdOpinion 作为最终结果。
对于问题 2,患者 2,Adam 和 Chris 都说“是”,所以最终结果是“是”,没有使用第 3 种意见。
如何编码我的数据以给出最后两列 Question1_final 和 Question2_final?
#Code to reproduce the data with the desired last two columns:
Patient <- c("1","2","3")
Question1_Adam <- c(NA,"Yes","No")
Question2_Adam <- c(NA,"Yes","No")
Question1_Ben <- c("Yes",NA,"Unlikely")
Question2_Ben <- c("No",NA,"No")
Question1_Chris <- c("Probably","Probably",NA)
Question2_Chris <- c("Unlikely","Yes",NA)
Question1_3rdOpinion <- c("Probably","Yes","No")
Question2_3rdOpinion <- c("No",NA,NA)
Question1_final <- c("Probably","Yes","No")
Question2_final <- c("No","Yes","Unlikely")
df <- data.frame(Patient, Question1_Adam, Question2_Adam, Question1_Ben, Question2_Ben, Question1_Chris, Question2_Chris, Question1_3rdOpinion, Question2_3rdOpinion, Question1_final, Question2_final)
我想我需要这样的东西,但不知道如何编码最后一部分:
df <- transform(df, Q1_final = ifelse(!is.na(Question1_3rdOpinion), Question1_3rdOpinion, *here I would somehow grep the first non-NA question 1 value*))