0

I would appreciate your help.

I want to change variables' name in R. The name of variables is "CACRAGE", "CACRAA1", etc. I want to remove "AC" in the middle, changing variables' name to "CRAGE", "CCRAA1". I've tried the following expression and it does not work. Please help me!

gsub(pattern = '^CAC([A-Z]{3,}', replacement = '^C([A-Z]{3,}', colnames(milwaukee_test), fixed = TRUE)

Thank you.

2
  • If you remove AC in CACRAA1 the result will be CRAA1 right? In that case you can match ^CAC and replace with C regex101.com/r/fy8Urk/1 Commented Aug 20, 2022 at 11:31
  • It sounds like you want to change strings, not variable names, correct? Those two are very different … Commented Aug 20, 2022 at 11:40

2 Answers 2

1

Why not just replace "CAC" with "C" if it occurs at the beginning of the name?

milwaukee_test <- data.frame(CACRAGE = 1:3, CACRAA1 = 2:4)

names(milwaukee_test) <- sub(pattern = '^CAC', "C", colnames(milwaukee_test))

milwaukee_test
#>   CRAGE CRAA1
#> 1     1     2
#> 2     2     3
#> 3     3     4

Created on 2022-08-20 with reprex v2.0.2

Sign up to request clarification or add additional context in comments.

Comments

0

You can use

gsub(pattern = '^CAC(?=[A-Z]{3})', replacement = 'C', colnames(milwaukee_test), perl = TRUE)

Note

  • You need perl = TRUE, not fixed = TRUE, in order to use regex and lookarounds in it
  • ^CAC(?=[A-Z]{3}) matches CAC at the start of string that is immediately followed with three uppercase ASCII letters.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.