为什么 str_replace_all 没有替换 R 中的字符串?

Why does str_replace_all is not replacing strings in R?

我确实有一个数据框,我需要在其中编辑疾病名称。每种疾病都有几行与之相关。出于某种原因,当我使用 str_replace_all 时,对于两个条件 ("Peripheral neuropathies (excluding cranial nerve and carpal tunnel syndromes)", "Venous thromboembolic disease (Excl 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"))

谢谢。

regex中,*(等字符具有特殊含义。 str_replace_all 默认使用正则表达式替换。因为你想匹配像 "(excluding cranial nerve and carpal tunnel syndromes)" 这样的词,所以使用 fixed.

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")))