\begin{Schunk}
\begin{Sinput}
 newname <- c("Respondent ID", "Respondent Age", "city - residence", "state - residence")
 ## Change space to underscore
 newname <- gsub(" ", "_", newname, fixed = TRUE)
 ## Replace minus with underscore
 newname <- gsub("-", "_", newname, fixed = TRUE)
 ## Replace multiple underscores with one underscore
 newname <- gsub("(_)\\1+", "_", newname)
 ## Lower case
 newname <- tolower(newname)
 newname
\end{Sinput}
\begin{Soutput}
[1] "respondent_id"   "respondent_age"  "city_residence"  "state_residence"
\end{Soutput}
\begin{Sinput}
 ##colnames(dat) <- newname
\end{Sinput}
\end{Schunk}
