R dplyr:: rename and select using string variable
Asked Answered
D

3

13

I am trying to select a subset of variables in my dataframe, and rename the variables in the new dataframe. I have a large number of variables that I would need to rename. I am using

dplyr::select
dplyr::select_

Since I have number of variables to rename, I am thinking if I should use a string variable to rename, but not sure if it could be possible? Using a string helps me to manage the newname oldname mapping. Here is an example

dplyr::select
library(dplyr)
library(nycflights13) 
set.seed(123)
data <- sample_n(flights, 3)

select(data,yr=year,mon=month,deptime=dep_time)

The question how could I pass the arguments for this in a string, that is the newvariable=oldvariable arguments and then use

dplyr::select_

col_vector <- c("year", "month", "dep_time")
select_(data, .dots = col_vector)

The string I have in mind are:

rename_vector <- c("yr=year","mon=month","deptime=dep_time")

Any suggestions would be very helpful.

Dilley answered 9/4, 2016 at 18:16 Comment(5)
Why not change column names with colnames(data) <- col_vector?Cinerama
Is the real question how to auto generate col_vector without manually constructing it? Then, you have to provide some logic of how you want to rename these columns.Cinerama
@Cinerama in my case I dont have to change all column names, only a subset of it. Also by having the "new name = old name" not only makes it readable, but also ensures there is a direct 1-1 match. Think of 50 variable names.Dilley
You can selectively rename columns using names(mtcars)[c(1, 2, 3)] <- c('z', 'y', 'z') or using names as names(mtcars)[names(mtcars) %in% c('x', 'y', 'z')] <- c('mpg', 'cyl', 'disp').Cinerama
In case you just want to rename some variable but not all of them, this answer could be useful: https://mcmap.net/q/904092/-renaming-column-names-with-variableSurfacetosurface
S
9

Instead of using a vector, you can pass a list to .dots in dplyr::select_, where the names are the new column names and the old names are characters.

> rename_list <- list(sepal_length = "Sepal.Length", sepal_width = "Sepal.Width")
> iris %>% tbl_df %>% select_(.dots = rename_list)

Source: local data frame [150 x 2]

   sepal_length sepal_width
          (dbl)       (dbl)
1           5.1         3.5
2           4.9         3.0
3           4.7         3.2
4           4.6         3.1
5           5.0         3.6
6           5.4         3.9
7           4.6         3.4
8           5.0         3.4
9           4.4         2.9
10          4.9         3.1
..          ...         ...
Sal answered 9/4, 2016 at 19:21 Comment(0)
B
16

dplyr

Another option using dplyr in conjunction with setNames to pass the vector with the new column names:

iris %>%
  select(Sepal.Length, Sepal.Width) %>% 
  setNames(c("sepal_length","sepal_width")) 

Base package

setNames(iris[, c("Sepal.Length", "Sepal.Width")], 
         c("sepal_length", "sepal_width"))

data.table

library(data.table)
setnames(iris, old = c("Sepal.Length", "Sepal.Width"), new = c("sepal_length","sepal_width"))
Bonfire answered 10/4, 2017 at 20:41 Comment(0)
S
9

Instead of using a vector, you can pass a list to .dots in dplyr::select_, where the names are the new column names and the old names are characters.

> rename_list <- list(sepal_length = "Sepal.Length", sepal_width = "Sepal.Width")
> iris %>% tbl_df %>% select_(.dots = rename_list)

Source: local data frame [150 x 2]

   sepal_length sepal_width
          (dbl)       (dbl)
1           5.1         3.5
2           4.9         3.0
3           4.7         3.2
4           4.6         3.1
5           5.0         3.6
6           5.4         3.9
7           4.6         3.4
8           5.0         3.4
9           4.4         2.9
10          4.9         3.1
..          ...         ...
Sal answered 9/4, 2016 at 19:21 Comment(0)
V
4

Using rename_with you can do something like:

old = c("Sepal.Length", "Sepal.Width")
new = c("sepal_length", "sepal_width")
iris %>% 
  dplyr::select(all_of(old)) %>%
  dplyr::rename_with(~ new, all_of(old)) 
Vedi answered 17/7, 2021 at 12:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.