'Data frame columns do not change once placing for-loop inside of function body - Rstudio

I have a dataframe with many variables and I'd like to change their names using a function. The column names have the following form

"Department_Home_Converted_Sum"                  
"Department_Womenswear_Converted_Sum"               
"Department_Menswear_Converted_Sum"               
"Department_Shoes_Converted_Sum"               
"Department_Kidswear_Converted_Sum" 

So I want to remove "Department_" and "_Converted_Sum". I managed to to it just by looping through the column names like this which works fine.

for (i in seq(1,length(colnames(mydf)))){
    colnames(mydf)[i] = str_sub(colnames(mydf)[i], 11, str_length(colnames(mydf)[i])-14)
}

But what bothers me is that once I place this inside of a function and then use the function on mydf, the dataframe has no changed column names.

rename_columns <- function(df){
   for (i in seq(1,length(colnames(df)))){
      colnames(df)[i] = str_sub(colnames(df)[i], 11, str_length(colnames(df)[i])-14)
   }
}

rename_columns(mydf)

I also tried to add a return(df) but it did not help. Does any one know why the function can't mutate the column names?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source