0

data <- data.frame(id = rep(1:3, each = 3), test = "Protein:creatinine ratio [Urine]", result = rep('4', 9))

I want to replace "Protein:creatinine ratio [Urine]" with "PCR":

data$test = str_replace_all(data$test, "Protein:creatinine ratio [Urine]", "PCR")

but it doesn't work.

Why is the string not being recognised for replacement?

Mark Davies
  • 787
  • 5
  • 18
  • `[]` has special meaning in regex. You can use `str_replace_all(data$test, fixed("Protein:creatinine ratio [Urine]"), "PCR")` but if this is an exact match you can use `replace` `replace(data$test, data$test == "Protein:creatinine ratio [Urine]", "PCR")` – Ronak Shah Mar 01 '21 at 11:21
  • Got it. That's helpful, thanks – Mark Davies Mar 01 '21 at 13:39

0 Answers0