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
best solution I found so far is
names(ctm2) %<>% stringr::str_replace_all("\\s","_") %>% tolower
credit goes to commenters and other answers
dplyr::select_all() can be used to reformat column names. This example replaces spaces and periods with an underscore and converts everything to lower case:
iris %>%
select_all(~gsub("\\s+|\\.", "_", .)) %>%
select_all(tolower) %>%
head(2)
sepal_length sepal_width petal_length petal_width species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
Just assign to names(ctm2):
names(ctm2) <- c("itsy", "bitsy", "eeny", "meeny")
or in data-driven way:
names(ctm2) <- paste("myColumn", 1:ncol(ctm2), sep="")
Another possibility is to edit your source file...
You can also use combination of make names and gsub functions in R.
names(ctm2)<- gsub("\\.","_", make.names(names(ctm2), unique = T))
Above code will do 2 things at a time:
If you use read.csv() to import your data (which replaces all spaces " " with ".") you can replace these instead with an underscore "_" using:
names(df) <- gsub("\\.", "_", names(df))
Alternatively, you may be able to achieve the same results with the stringr package.
names(ctm2) <- names(ctm2) %>% stringr::str_replace_all("\\s","_")