Hello I am trying to write a function that will take in a data frame, and fix its respective column headers if there is a special character or space in the name. The function seems to work, as the results are printed, but it does not seem to save the respective changes to the original dataframe. Thoughts on how to fix this? The data I used to test it was a tbl_df, so I'm not sure if that has something to do with why it is not updating correctly. Thanks.
nameChange <- function(df) {
for(i in 1:length(colnames(df)[i])) {
if(str_detect(colnames(df[i]),"[:punct:]|[:space:]") == TRUE) {
#Could use "\\s" to find space
names(df) <- str_replace_all(names(df)," *",'')
names(df) <- str_replace_all(names(df),"-",'')
#df <- df
assign('df',df, envir=.GlobalEnv)
#return(df)
print("Worked")
}
else{
print("Function did not replace anything")
}
}
}
This is the data I am using to test the function:
#data from: http://www.tableau.com/learn/tutorials/on-demand/getting-started-data
orders_path <- file.path("/Users/petergensler/Desktop/Global Superstore.xls")
order_table <- read_excel(orders_path, sheet = "Orders")
nameChange(order_table)
Once I call colnames on order_table you should be able to see that the hyphen in Product Sub-Category is removed, and all the spaces inside of each column name are no longer there.