r - Rename function for data frame columns -


i trying make function gets input data frame , 2 names (old , new) , replaces column old name of data frame new name. tried

df <- data.frame(a = 1:3, b = 8:10)   my.rename <- function(df, old.name, new.name){  names(df)[names(df) == old.name] <- new.name  } my.rename(df, "a", "something") df 

and

my.rename <- function(df, old.name, new.name){  eval(substitute(names(df)[names(df) == old.name] <- new.name), df) } my.rename(df, "a", "something") df 

but name doesn't seem affected.

you need return dataframe modifed in function:

df <- data.frame(a = 1:3, b = 8:10)   my.rename <- function(df, old.name, new.name){  names(df)[names(df) == old.name] <- new.name   df } df2 <- my.rename(df, "a", "something") df2 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -