How to Work with Variable Names in R
Variables in a data frame in R always need to have a name. To access the variable names, you can again treat a data frame like a matrix and use the function colnames() like this:
> colnames(employ.data)  "employee" "salary" "startdate"
But, in fact, this is taking the long way around. In case of a data frame, the colnames() function lets the hard work be done internally by another function, the names() function. So, to get the variable names, you can just use that function directly like this:
> names(employ.data)  "employee" "salary" "startdate"
Similar to how you do it with matrices, you can use that same function to assign new names to the variables as well. For example, to rename the variable startdate to firstday, you can use the following code:
> names(employ.data) <- 'firstday' > names(employ.data)  "employee" "salary" "firstday"
One important difference between a matrix and a data frame is that data frames always have named observations. Whereas the rownames() function returns NULL if you didn’t specify the row names of a matrix, it will always give a result in the case of a data frame.
Check the outcome of the following code:
> rownames(employ.data)  "1" "2" "3"
By default, the row names — or observation names — of a data frame are simply the row numbers in character format. You can’t get rid of them, even if you try to delete them by assigning the NULL value as you can do with matrices.
You shouldn’t try to get rid of them either, because your data frame won’t be displayed correctly any more if you do.
You can, however, change the row names exactly as you do with matrices, simply by assigning the values via the rownames() function, like this:
> rownames(employ.data) <- c('Chef','BigChef','BiggerChef') > employ.data employee salary firstday Chef John Doe 21000 2010-11-01 BigChef Peter Gynn 23400 2008-03-25 BiggerChef Jolie Hope 26800 2007-03-14
Don’t be fooled, though: Row names can look like another variable, but you can’t access them the way you access the other variables.