I have a table in R which has many columns. I want to change the column names. I have found that:
colnames(table)[123] <- "newName"
changes the 123rd column to the name "newName". But to achieve this I need to find the number of the column with the name "oldName". I was thinking of something like this:
colnames(table)["oldName"] <- "newName"
but that didn't work. So how can one go about doing this?
It's quite easy using dplyr's rename
function:
library(dplyr)
rename(mtcars, cylinders = cyl)
This will rename the column "cyl" to be "cylinders" in the data set "mtcars".
Edit after comment:
If you want to rename multiple columns, you can do (without dplyr):
names(mtcars)[names(mtcars) %in% c("mpg", "cyl", "hp")] <- c("miles", "cylinders", "horsepower")
names(mtcars)
# [1] "miles" "cylinders" "disp" "horsepower" "drat"
# [6] "wt" "qsec" "vs" "am" "gear"
#[11] "carb"
So you just need 1 line to rename multiple columns. And of course, you could store those names in vectors.
Note to whom it may concern: while it is true that plyr has a rename
function, dplyr also has a rename
function in its current CRAN version 0.3.0.2. It is described in the introduction to dplyr and, if you're running the current version, you can just type ?dplyr::rename
. If you look closely, you can even tell which one is being used because there's a subtle difference between plyr's and dplyr's rename functions: in plyr, it would be plyr::rename(mtcars, c("oldName" = "newName"))
while in dplyr (like in my answer) it is dplyr::rename(mtcars, newName = oldName)
.
Edit after comment by OP:
I cannot reproduce the error you describe. Here's what I understood you did, but it works without any errors or unexpected behavior in my case:
df <- data.frame(DE9 = 1:2, code9 = 3:4)
df
# DE9 code9
#1 1 3
#2 2 4
dplyr::rename(df, newname = DE9)
# newname code9
#1 1 3
#2 2 4
see if this suits you
library(data.table)
cars = setnames(as.data.table(mtcars),
c("mpg", "cyl", "hp"), # old column names
c("miles", "cylinders", "horsepower")) # corresponding new column names
names(cars)
# [1] "miles" "cylinders" "disp" "horsepower" "drat" "wt"
# [7] "qsec" "vs" "am" "gear" "carb"
setDF(cars) # convert back to data.frame if needed.
colnames(table)[colnames(table)=="V1"] <- "VeeOne"
If you want to stick to base R (data table and dplyr can be tricky for beginners, and besides, grep can misfire badly if you have similar column names), just use the above.
Full replicable example:
table <- matrix(1:100, ncol=10)
colnames(table) <- paste0("V",1:10)
colnames(table)
colnames(table)[colnames(table)=="V1"] <- "VeeOne"
colnames(table)
It's not dplyr
you are after as been posted here, as, according to the reference manual, it does not include rename
. It is instead plain plyr
that includes rename
. And it can take a name character vector for renaming:
library(plyr)
rename(mtcars, c("cyl"="cylinders", "mpg"="miles per gallon", "hp"="horsies"))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.