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
Post a Comment