@andresrcs Thanks for the explanation! I find it helpful.
I have another question: Suppose there are variables Name and name2, and corresponding new variables are nName and nname2. How would your solution change in this case? Put another way, instead of "new_", new variables's names start with "n".
library(tidyverse)
# Toy data
df <- tibble(
id = 7:8,
country = c("UK", "US"),
Name = c("abc", "def"),
nName = c("ABC", "DEF"),
name2 = c("xyz", "ijk"),
nname2 = c("xyz", "ijk"),
p123 = 1:2,
np123 = 100:101,
p900 = 2:3,
np900 = 4:5
)
# What to do?
df %>%
select(id, country, starts_with("n")) %>%
rename_with(.fn = ~str_remove(.x, "^n"),
.cols = starts_with("n"))
#> Error: Names must be unique.
#> x These names are duplicated:
#> * "Name" at locations 3 and 4.