'Custom function to rename all columns

I want to manipulate the names of all the columns in a dataframe with this function that I wrote:

clean_names <- function(df) {
  names(df) <- tolower(names(df))
  names(df) <- gsub('\\s', '\\_', names(df))
  names(df) <- gsub('\\(|\\)|\\/|,|\\.', '\\_', names(df))
  names(df) <- gsub('(\\_)\\_', '\\1', names(df))
  names(df) <- gsub('\\_$', '', names(df))
}

That said, when actually called, it doesn't do anything (no error just nothing). What's the problem here?

I suspect the problem is that I'm only assigning things and not returning anything. But in this case I don't want to return a value just change the column names.

The only parameter here is df and I'm calling the names() function multiple times. Shouldn't this work? Any help is appreciated!



Solution 1:[1]

From the names documentation:

For names<-, the updated object. (Note that the value of names(x) <- value is that of the assignment, value, not the return value from the left-hand side.)

Therefore you should try:

clean_names <- function(df) {
  names(df) <- tolower(names(df))
  names(df) <- gsub('\\s', '\\_', names(df))
  names(df) <- gsub('\\(|\\)|\\/|,|\\.', '\\_', names(df))
  names(df) <- gsub('(\\_)\\_', '\\1', names(df))
  names(df) <- gsub('\\_$', '', names(df))
  return(df)
}

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 benson23