我有2個數據集;一個包含關於患者的信息,另一個是醫療碼在R中找到2組數據中的匹配字符串
patient <- data.table(ID = rep(1:5, each = 3),
codes = c("13H42", "1B1U", "Eu410", "Je450", "Fg65", "Eu411", "Eu402", "B110", "Eu410", "Eu50",
"1B1U", "Eu513", "Eu531", "Eu411", "Eu608")
)
code <- data.table(codes = c("BG689", "13H42", "BG689", "Ju34K", "Eu402", "Eu410", "Eu50", "JE541", "1B1U",
"Eu411", "Fg605", "GT6TU"),
term = c(NA))
列表中的code$term
具有價值,但在這個例子中,他們會省略。
我想要的是patient
中的指示器列,它顯示了中的代碼是否出現在patient$codes
中。
patient
ID codes mh
1: 1 13H42 TRUE
2: 1 1B1U TRUE
3: 1 Eu410 TRUE
4: 2 Je450 FALSE
5: 2 Fg65 FALSE
6: 2 Eu411 TRUE
7: 3 Eu402 TRUE
8: 3 B110 FALSE
9: 3 Eu410 TRUE
10: 4 Eu50 TRUE
11: 4 1B1U TRUE
12: 4 Eu513 FALSE
13: 5 Eu531 FALSE
14: 5 Eu411 TRUE
15: 5 Eu608 FALSE
我的解決辦法是使用grepl:
patient$mh <- mapply(grepl, pattern=code$codes, x=patient$codes)
然而這並沒有爲code
工作是不一樣的長度,我得到了警告
Warning message:
In mapply(grepl, pattern = code$codes, x = patient$codes) :
longer argument not a multiple of length of shorter
所有解決方案完全匹配?
你想要完全匹配嗎? –
@Kunal Puri是 – Lb93
您確定您的預期產出是正確的嗎?認爲你可以做'耐心$ mh < - 耐心$ code%在%code $代碼' – mtoto