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

Time:02-12

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?

CodePudding user response:

If all you want to do is want to remove "Department_" and "_Converted_Sum" you can avoid the looping by using str_remove_all() from the stringr package.

names(df) <- stringr::str_remove_all(names(df), "Department_|_Converted_Sum")
  • Related