Modifying column names
Sometimes you want to rename your colums. You could do this by creating copies of the columns with new names, but you can also directly get and set the column names of a data frame, using the function colnames()
.
The dplyr library has a rename()
function, which can also be used. Remember the cheatsheet.
This exercise is part of the course
Helsinki Open Data Science
Exercise instructions
- Print out the column names of
learning2014
- Change the name of the second column to 'age'
- Change the name of 'Points' to 'points'
- Print out the column names again to see the changes
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# learning2014 is available
# print out the column names of the data
colnames(learning2014)
# change the name of the second column
colnames(learning2014)[2] <- "age"
# change the name of "Points" to "points"
# print out the new column names of the data