0
sample <- function(df, old_name, new_name){
  data <- df %>% 
    rename(new_name = old_name)
  
  return(data)
}
  
[1] temp <- sample(data, "old", "new")  
[2] temp <- sample(data, old, new)

I want to create a customized function in R, and I want to use an input argument to rename the columns in this function. But the rename() function from dplyr only allows format like rename(oldname, newname), which means both inputs cannot be character. I cannot execute the function correctly either by writing it like [1] or [2]. What is the data types in the rename() and other dplyr functions? And how could I rename the columns by the input arguments?

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
Estherzh
  • 1
  • 1

0 Answers0