How to fix spaces in column names of a data.frame (remove spaces, inject dots)?

dataframe, r

Solution

UDPDATE 2022 Aug:

df %>% rename_with(make.names)

OLD code was: (still works though) as of Jan 2021: drplyr solution that is brief and uses no extra libraries is

df %<>% dplyr::rename_all(make.names)

credit goes to commenter.

Problem

After importing a file, I always try try to remove spaces from the column names to make referral to column names easier. Is there a better way to do this other then using transform and then removing the extra column this command creates? This is what I use now: ``` names(ctm2) #tranform function does this, but requires some action ctm2<-transform(ctm2,dymmyvar=1) #remove dummy column ctm2$dymmyvar <- NULL names(ctm2) ```

Original source