I have the following tibble
tib <- tibble(AAA111 = rnorm(3),
AAA222 = rnorm(3),
BBB5 = rnorm(3),
BBB3456=rnorm(3))
I want to change the component of column names such that "AAA" becomes "M" and "BBB" becomes "W". I can do this outside purrr or a loop by the following
tib %>%
rename_with(~str_replace(.x, "AAA", "M"), .cols = starts_with("AAA")) %>%
rename_with(~str_replace(.x, "BBB", "W"), .cols = starts_with("BBB"))
But how do I achieve this in a map function or a loop?
Note I received some information from the following post. Purrr map with rename_with