In the above table, some variables have corresponding "new" variables. For example, name and new_name. How can I drop the old ones, and keep the new ones without the new_ prefix?
Here is my desired output:
id
country
name
p123
p900
7
UK
ABC
100
4
8
US
DEF
101
5
library(tidyverse)
# Toy data
df <- tibble(
id = 7:8,
country = c("UK", "US"),
name = c("abc", "def"),
new_name = c("ABC", "DEF"),
p123 = 1:2,
new_p123 = 100:101,
p900 = 2:3,
new_p900 = 4:5
)
@andresrcs Many thanks for the solution! One question: Why are you using .cols = starts_with("new")? Even without it, the code produces the wanted result.
To only apply the renaming function to the columns where is needed, the rest of the columns do not match the extrac pattern but the function is still applied to all of them if you do not restrict the selection. For a small amount of columns doesn't make much of a difference though.
@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.