
R is an amazing language and there are endless things you can do. I’m also throwing in the towel on the deprecated/superseded rename_at / rename_if / rename_all functions, since they have been replaced by select and rename_with. I didn’t have any idea how many ways there would be to rename columns when I started this, but it’s becoming evident that there are likely hundreds of ways if we count every nuance. x))Ī note: I’m going to stop interchanging names and colnames as I did previously. Rename_with(~str_replace("new_column", "old_column". Select("new_column" = ends_with("column"))

Select("new_column" = starts_with("old")) Str_replace_all("old_column", "new_column")) Using mutate to create a new column and then removing the old_column without pipes ( %>%):.Using mutate to create a new column and then removing the old_column:.Renaming in a select call without a %>%:.

You can learn more about the tidyverse here SetNames(df, replace(names(df), names(df) = 'old_column', 'new_column'))ĭf <- transform(df, new_column = old_column, old_column = NULL) Names(df) <- gsub("old_column", "new_column", colnames(df))Ĭolnames(df) <- gsub("old_column", "new_column", names(df))Įval(parse(text = 'names(df) <- "new_column"'))Įval(parse(text = 'colnames(df) <- "new_column"')) Names(df) <- gsub("old_column", "new_column", names(df)) Names(df) <- sub("old_column", "new_column", colnames(df))Ĭolnames(df) <- sub("old_column", "new_column", names(df))Ĭolnames(df) <- gsub("old_column", "new_column", colnames(df)) Names(df) <- sub("old_column", "new_column", names(df))

R rename column code#
It was hard for me to follow, and I cringe at the idea that I sent some of this old code to colleagues. I would reference a column by index and then by name. When I first starting using R, my code was a mash-up of base R, dplyr, and data.table. Not that my previous posts were intellectual thinkpieces, but I thought that I had to write about something novel or innovative to provide any level of value. Don't be afraid to share 🙏🏾- Angie Jones February 14, 2021 I ran into a basic configuration issue and couldn't find a solution online.Īfter I figured it out, I wrote a simple post with the exact error message and the solution.Įverything doesn't have to be a think piece.

I haven’t posted for a while, and came across a tweet from Angie Jones that I really related to.
