By Andrie de Vries, Joris Meys

One important difference between a matrix and a data frame in R 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] “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 variables.