'changing string names of column through a function R [duplicate]
I am trying to change the column names of a dataframe with a function, example below:
df <- data.frame(x = c(71.33, 74.98 , 80 , 85.35 , 90.03),
y = c(119.17, 107.73 , 99.72 , 75, 54.59))
df
change_name <- function(old_name, new_name, data) {
data = plyr::rename(data, c(old_name = new_name))
return(names(data))
}
change_name("x", "Var_x", df)
however it is giving me an error that I don't understand.
The following `from` values were not present in `x`: old_name
[1] "x" "y"
Can anyone help? thanks!
Solution 1:[1]
change_name <- function(old_name, new_name, data) {
data = plyr::rename(data, replace = setNames(new_name, nm=old_name))
return(names(data))
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | langtang |
