简体   繁体   中英

How do I rename a column in a R dataframe using a vector to indicate the OLD column name?

I want to rename a column in a R dataframe, and the existing column name value I want to rename is in a vector. I don't know what it will be as it's computed from user input.

I've tried this, but it no worky....

  > head(iris)
  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
6          5.4         3.9          1.7         0.4  setosa
> myOldNameVector<-c("Petal.Width")
> head(rename(iris, myNewName = myOldNameVector))
Error: Unknown variables: myOldNameVector.
> 

Anyone know what the correct incantation is? Thanks!

dplyr 0.6.0 will change a few things ; I think it will soon be:

library(dplyr)
packageVersion("dplyr")
# [1] ‘0.5.0.9002’
myOldNameVector<-c("Petal.Width")
head(rename(iris, myNewName = !!as.name(myOldNameVector)))
#   Sepal.Length Sepal.Width Petal.Length myNewName 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
# 6          5.4         3.9          1.7       0.4  setosa

You can use rename_

library(dplyr)

iris %>% 
  rename_("New_Petal_Width" = "Petal.Width")

or

myOldNameVector <- c("Petal.Width")
iris %>% 
    rename_("New_Petal_Width" = myOldNameVector)

Or use the colnames variable:

#simulate user input
 userInput <- "Petal.Length"

 #Load the iris dataframe
 df <- iris

 #find the column that matches user input and rename it
 colnames(df)[colnames(df) == userInput] <- "spam"

 #Show the results
 head(df)
  Sepal.Length Sepal.Width spam 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
6          5.4         3.9  1.7         0.4  setosa

How about using the names() function instead?

myOldNameVector<-c("Petal.Width", "Sepal.Width")
myNewNameVector <- c("Name1", "Name2")

names(iris)[names(iris) %in% myOldNameVector] <- myNewNameVector

head(iris)
#  Sepal.Length Name1 Petal.Length Name2 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
#6          5.4   3.9          1.7   0.4  setosa

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM