Skip to main content

Section 7.3 Cleaning our data

Not only does R love it when our column headers do not have spaces, it is also best practice for making our analyzes reproducible.
We could use the rename() command from the dplyr package like in Section Subsectionย 2.5.7, but there is more than one column, so we can streamline this process. Using the clean_names() function from the janitor package, we can easily remove the spaces in our column names
library(janitor)
colnames(examData) # Before we clean the data

examData <- clean_names(examData)

colnames(examData) # After we clean the data
[1] "Student Name"                     "Studying Hours"                  
[3] "Exam Score"                       "Anxiety Score"                   
[5] "First Generation College Student"
[1] "student_name"                     "studying_hours"                  
[3] "exam_score"                       "anxiety_score"                   
[5] "first_generation_college_student"
Looking at the before and after, we see that the clean_names() function replaced every space within our column headers and replaced them with underscores. Notice how it also turned all of the letters to lowercase (also best practice for reproducibility). Now we can begin our journey.