Programmatically Rename Columns in Dplyr

programmatically rename columns in dplyr

To programatically setting variable you need to use special operator := and !! to evaluate your variable.

See dplyr's vignette Programming with dplyr

cnames=c('cylinder', 'mile_per_gallon')
library(dplyr, warn.conflicts = F)
mtcars %>%
as_tibble() %>%
rename(!!cnames[1] := cyl)
#> # A tibble: 32 x 11
#> mpg cylinder disp hp drat wt qsec vs am gear carb
#> * <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
#> 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
#> 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1
#> 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
#> 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2
#> 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1
#> 7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4
#> 8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2
#> 9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2
#> 10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4
#> # ... with 22 more rows

How to rename selected columns using dplyr with new column names as strings

If you want to use dplyr's rename function, it would be best to create a named vector / list and call it using the .dots argument in the standard evaluation version:

cols <- c("Sepal.Length", "Petal.Length")
to_app <- ".xxx"
cols <- setNames(cols, paste0(cols, to_app))

df %>% rename_(.dots = cols)

## A tibble: 5 × 3
# Sepal.Length.xxx Sepal.Width Petal.Length.xxx
#* <dbl> <dbl> <dbl>
#1 5.1 3.5 1.4
#2 4.9 3.0 1.4
#3 4.7 3.2 1.3
#4 4.6 3.1 1.5
#5 5.0 3.6 1.4

Note however, that this approach is subject to change with the next version 0.6.0 of dplyr (see e.g. http://blog.rstudio.org/2017/04/13/dplyr-0-6-0-coming-soon/ and http://dplyr.tidyverse.org/articles/programming.html).

How to rename column with dplyr where both the new column name and the original column name are variables

The link provided offered insight to the solution

rename_data <- function(iris, target = "Species", new_target_name = "Spec3") {
iris2 <- iris %>% rename(!!new_target_name := target)
iris2
}

head(rename_data(iris))

rename column names dynamically in dplyr chain

You can use rename_at. If you know that only one column starts with "ge", this will work:

library(dplyr)
mtcars %>%
rename_at(vars(starts_with("ge")), funs(paste0("garbage")))

If you want to rename more than one column, the function in funs() needs to return a vector of names, or do something like gsub() to add something to the existing column names.

in R dplyr package, a question about function rename_with()

To use rename_with, you need to provide a function. In this case, we can just pass a character vector of the new names to replace all column names. If you have a larger dataframe and only want change a few names, then you can select those as normal with dplyr (e.g., c(my_item, y2021, y2022) instead of everything()).

library(dplyr)

ori_df %>%
rename_with(~ c('item','2021','2022'), everything())

# item 2021 2022
#1 a 1 9
#2 b 4 8
#3 c 5 7

If you are replacing all columns, then everything() is the default for the .cols argument, so you can just write:

ori_df %>% 
rename_with(~c('item','2021','2022'))

R dplyr: rename variables using string functions

I think you're looking at the documentation for plyr::rename, not dplyr::rename. You would do something like this with dplyr::rename:

iris %>% rename_(.dots=setNames(names(.), tolower(gsub("\\.", "_", names(.)))))

Rename columns on the fly during lapply tidy

We can use := for assignment here. With tidyverse, we can use map to loop over the list elements (similar to lapply from base R), then after filtering the rows based on the unique elements passed into map, create a column with mutate on the fly with the assignment operator (:=), by passing the string and evaluate (!!). The str_c does similar action as paste from base R except that its behavior is sligtly different when there are NAs

library(purrr)
library(stringr)
map(files_names, ~
df %>%
filter(char_1 == .x) %>%
mutate(!!str_c(.x, "_num3") := number3 + 100))

Using glue in new column name in rename() in R

The following is also possible, without explicit use of glue, but with the walrus operator :=, like in @Ronak Shah's answer:

df %>% rename("Change since {date_variable}" := change)


Related Topics



Leave a reply



Submit