-1

Whenever I try to replace column names using the below codes, I get an error.

colnames(all_lab_drug)<- str_replace_all(colnames(all_lab_drug),' |-','_')

Error

Error in str_replace_all(colnames(all_lab_drug), " |-", "_"): 
  could not find function "str_replace_all"
AndrewGB
  • 16,126
  • 5
  • 18
  • 49
Deepak
  • 3
  • 1

1 Answers1

2

You need to load the stringr package before running the code.

library(stringr)

colnames(all_lab_drug) <- str_replace_all(colnames(all_lab_drug), ' \\|-', '_')

Or you can call the package explicitly.

stringr::str_replace_all(colnames(all_lab_drug), ' \\|-', '_')

# [1] "drug_one"   "drug_two"   "drug_three"

Data

all_lab_drug <-
  structure(
    list(
      `drug |-one` = c(1, 1),
      `drug |-two` = c(1, 1),
      `drug |-three` = c(1, 1)
    ),
    class = "data.frame",
    row.names = c(NA,-2L)
  )
AndrewGB
  • 16,126
  • 5
  • 18
  • 49