为什么str_替换R中的字符串?
我确实有一个数据框,需要在其中编辑疾病名称。每种疾病都有几行与其相关。出于某种原因,当我使用str\u replace\u all时,在两种情况下都不会发生置换为什么str_替换R中的字符串?,r,string,str-replace,R,String,Str Replace,我确实有一个数据框,需要在其中编辑疾病名称。每种疾病都有几行与其相关。出于某种原因,当我使用str\u replace\u all时,在两种情况下都不会发生置换(“周围神经病变(不包括颅神经和腕管综合征)”,“静脉血栓栓塞性疾病(不包括PE)”。输出中没有警告或错误消息,因此我无法找出问题所在。有人有什么想法吗 codelists <- data.frame(Disease = sample(c("Peripheral neuropathies (excluding crania
(“周围神经病变(不包括颅神经和腕管综合征)”,“静脉血栓栓塞性疾病(不包括PE)”
。输出中没有警告或错误消息,因此我无法找出问题所在。有人有什么想法吗
codelists <- data.frame(Disease = sample(c("Peripheral neuropathies (excluding cranial nerve and carpal tunnel syndromes)", "Primary Malignancy_Brain, Other CNS and Intracranial", "Venous thromboembolic disease (Excl PE)"), 15, replace = T), Codes = 1:15)
## Sort the dataframe according to Disease
codelists <- codelists[order(codelists$Disease), ]
library(stringr)
codelists$Disease2 <- str_replace_all(codelists$Disease, c("Peripheral neuropathies (excluding cranial nerve and carpal tunnel syndromes)" = "Non-diabetic peripheral neuropathies (excluding cranial nerves and carpal tunnel syndrome)", "Primary Malignancy_Brain, Other CNS and Intracranial" = "Primary malignancy brain, other CNS and intracranial", "Venous thromboembolic disease (Excl PE)" = "Venous thromboembolism"))
code列表在regex
chaarcters类*
,(
有特殊含义。str\u replace\u all
默认使用regex替换。由于您希望匹配(不包括颅神经和腕管综合征)
等词,请准确使用固定的
library(stringr)
codelists$Disease2 <- str_replace_all(codelists$Disease, fixed(c("Peripheral neuropathies (excluding cranial nerve and carpal tunnel syndromes)" = "Non-diabetic peripheral neuropathies (excluding cranial nerves and carpal tunnel syndrome)", "Primary Malignancy_Brain, Other CNS and Intracranial" = "Primary malignancy brain, other CNS and intracranial", "Venous thromboembolic disease (Excl PE)" = "Venous thromboembolism")))
库(stringr)
代码列表$Disease2