Is it possible to solve this issue with base and tidyverse (preferable) tools?
library(tidyverse)
# I have
tibble(a = 1, b = 2, c = 3)
#> # A tibble: 1 x 3
#> a b c
#> <dbl> <dbl> <dbl>
#> 1 1 2 3
# here we apply something like
tbl %>% desired_rename_function(last(names(.), 2), c("second", "third"))
# I want
tibble(a = 1, second = 2, third = 3)
#> # A tibble: 1 x 3
#> a second third
#> <dbl> <dbl> <dbl>
#> 1 1 2 3
rename_at() has .vars argument. I believe it could solve the problem if the argument for new names accepted a vector, not a list of functions. rename() is not flexible as far as I can see, it forces you to specify the exact pairs of column names
library(tidyverse)
tbl <- tibble(a = 1, b = 2, c = 3, d = 4, e = 5, f = 6)
desired_rename_function <- function(x, y, z) {
x %>%
rename_at(
vars(
names(.) %>%
tail(y)),
funs(paste(z))
)
}
desired_rename_function(tbl, y = 2, z = c("hello", "yes"))
#> # A tibble: 1 x 6
#> a b c d hello yes
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1 2 3 4 5 6
tbl %>% desired_rename_function(., y = 2, z = c("hello", "yes"))
#> # A tibble: 1 x 6
#> a b c d hello yes
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1 2 3 4 5 6
library(tidyverse)
n <- 2
tibble(a = 1, b = 2, c = 3) %>%
rename_at(vars(tail(names(.), n)), funs(c("second", "third")))
#> # A tibble: 1 x 3
#> a second third
#> <dbl> <dbl> <dbl>
#> 1 1 2 3