R: dplyr - rename column name by position instead of name

I want to know if there is a way to rename column names by column position and not change by column name.

The following snippet shows how to change the name.

suppressPackageStartupMessages(library(dplyr)) gd_url <- "http://tiny.cc/gapminder" gtbl <- gd_url %>% read.delim %>% tbl_df gtbl <- gtbl %>% rename(life_exp = lifeExp, gdp_percap = gdpPercap) gtbl 
+7
r dplyr
source share
1 answer

If you prefer to stick to dplyr pipes in the world, then with dplyr 0.7.2 you can rename by position using the following nomenclature:

Using the original example:

 gtbl <- gtbl %>% rename("life_exp" = !!names(.[5]), "gdp_percap" = !!names(.[6])) 

Respectfully fishes out an older position. I had a similar problem and considered this question before figuring out this alternative solution.

+9
source share

All Articles