I like plyr's renaming function rename
. I have recently started using dplyr, and was wondering if there is an easy way to rename variables using a function from dplyr, that is as easy to use as to plyr's rename
?
dplyr version 0.3 added a new rename()
function that works just like plyr::rename()
, but with the old and new names switched:
df <- rename(df, new_name = old_name)
dplyr::rename(iris, petal_length = Petal.Length)
. –
Neurocoele iris <- dplyr::rename(iris, petal_length = Petal.Length)
and rename() does not handle variable names with spaces, for example, dplyr::rename(iris, petal_length = "petal length")
produces an error. –
Lavish colnames(df) <- c("bla","blu")
. –
Dominate setNames()
if you're replacing the column names wholesale: df %>% mutate(foo = 1 +2) %>% setNames(c("blah", "blu", "bar"))
–
Lakshmi The next version of dplyr will support an improved version of select that also incorporates renaming:
> mtcars2 <- select( mtcars, disp2 = disp )
> head( mtcars2 )
disp2
Mazda RX4 160
Mazda RX4 Wag 160
Datsun 710 108
Hornet 4 Drive 258
Hornet Sportabout 360
Valiant 225
> changes( mtcars, mtcars2 )
Changed variables:
old new
disp 0x105500400
disp2 0x105500400
Changed attributes:
old new
names 0x106d2cf50 0x106d28a98
changes
is exported (or it should be) –
Mowbray plyr
's rename function uses "old"="new"
whereas dplyr
uses new=old
which does keep it consistent with the rest of the dplyr functions. Personally, I don't think of it as a problem--you get used to new things quickly especially when it means a significant speedup in your data processing. –
Cestoid mtcars2 <- select( mtcars, disp2 = disp )
returns a dataframe containing only the renamed variable, where it should return all variables, just with disp
named disp2
. Can anyone else confirm? –
Cestoid select
. Not sure we have something that says select all variables and by the way rename this column. –
Alkoran select
actually behaves? I would put in a vote for an easy dplyr
way to keep all variables and just rename one or two. :) For now I'll keep loading plyr
and using rename
. –
Cestoid select()
is an incredibly useful function but not really a replacement for plyr::rename()
. I may be missing something but it seems like the answer to the original question is no. –
Locomotion mtcars %>% select(matches(".*"),disp2=disp)
. I would love a more parsimonious solution but this works and preserves all columns (though not their order). disp
does not get duplicated. –
Antigen You can actually use plyr
's rename
function as part of dplyr
chains. I think every function that a) takes a data.frame
as the first argument and b) returns a data.frame
works for chaining. Here is an example:
library('plyr')
library('dplyr')
DF = data.frame(var=1:5)
DF %>%
# `rename` from `plyr`
rename(c('var'='x')) %>%
# `mutate` from `dplyr` (note order in which libraries are loaded)
mutate(x.sq=x^2)
# x x.sq
# 1 1 1
# 2 2 4
# 3 3 9
# 4 4 16
# 5 5 25
UPDATE: The current version of dplyr
supports renaming directly as part of the select
function (see Romain Francois post above). The general statement about using non-dplyr functions as part of dplyr
chains is still valid though and rename
is an interesting example.
It is not listed as a function in dplyr (yet): http://cran.rstudio.org/web/packages/dplyr/dplyr.pdf
The function below works (almost) the same if you don't want to load both plyr and dplyr
rename <- function(dat, oldnames, newnames) {
datnames <- colnames(dat)
datnames[which(datnames %in% oldnames)] <- newnames
colnames(dat) <- datnames
dat
}
dat <- rename(mtcars,c("mpg","cyl"), c("mympg","mycyl"))
head(dat)
mympg mycyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
Edit: The comment by Romain produces the following (note that the changes function requires dplyr .1.1)
> dplyr:::changes(mtcars, dat)
Changed variables:
old new
disp 0x108b4b0e0 0x108b4e370
hp 0x108b4b210 0x108b4e4a0
drat 0x108b4b340 0x108b4e5d0
wt 0x108b4b470 0x108b4e700
qsec 0x108b4b5a0 0x108b4e830
vs 0x108b4b6d0 0x108b4e960
am 0x108b4b800 0x108b4ea90
gear 0x108b4b930 0x108b4ebc0
carb 0x108b4ba60 0x108b4ecf0
mpg 0x1033ee7c0
cyl 0x10331d3d0
mympg 0x108b4e110
mycyl 0x108b4e240
Changed attributes:
old new
names 0x10c100558 0x10c2ea3f0
row.names 0x108b4bb90 0x108b4ee20
class 0x103bd8988 0x103bd8f58
mtcars
etc ... but quite dramatic if you deal with substantial data. check dplyr:::changes(mtcars, dat)
–
Alkoran While not exactly renaming, dplyr::select_all()
can be used to reformat column names. This example replaces spaces and periods with an underscore and converts everything to lower case:
iris %>%
select_all(~gsub("\\s+|\\.", "_", .)) %>%
select_all(tolower) %>%
head(2)
sepal_length sepal_width petal_length petal_width species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
I tried to use dplyr::rename and I get an error:
occ_5d <- dplyr::rename(occ_5d, rowname='code_5d')
Error: Unknown column `code_5d`
Call `rlang::last_error()` to see a backtrace
I instead used the base R function which turns out to be quite simple and effective:
names(occ_5d)[1] = "code_5d"
dplyr >= 1.0.0
In addition to dplyr::rename
in newer versions of dplyr
is rename_with()
rename_with() renames columns using a function.
You can apply a function over a tidy-select set of columns using the .cols
argument:
iris %>%
dplyr::rename_with(.fn = ~ gsub("^S", "s", .), .cols = where(is.numeric))
sepal.Length sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
© 2022 - 2024 — McMap. All rights reserved.
rename(TheDataFrame,OldVarName=NewVarName)
but I getError: Unknown variables: NewVarName.
and I don't understand why. – Galateah