Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close


Perfect! You may be interested to know that you can change the names of your levels. For example, if you have a variable named phone with categories "N" and "Y", and you would like to change these to "No" and "Yes", respectively, you can create new labels like this:

levels(houses$phone_factor) <- c("No","Yes")

Not only does this change the labels of the levels you've defined for the factor, but it also changes every instance of "N" and "Y" in the factor to "No" and "Yes", respectively.

You should be careful when renaming levels. The following two expressions are not going to do the same thing:

levels(houses$phone_factor) <- c("No","Yes") 


levels(houses$phone_factor) <- c("Yes","No")

The first expression will rename "N" to "No" and "Y" to "Yes", which is what we want. On the other hand, the second expression will produce an undesirable result—it will replace "N" with "Yes" and "Y" with "No". This is because the assignment renames levels in the order they are defined in the factor. Our phone_factor variable has levels "N", "Y", in that order, so the assignment should match the order of the levels.

You can also change only one level from already existing levels vector:

levels(houses$phone_factor)[1] <- "NO"

This way we changed the first level of the phone_factor variable. Remember that the indexing in R starts from 1, not from 0.


The houses$type column stores the housing types for different properties. Let's replace the "detached house" label with "detached". But you only need to change the name of this one level! To do so, you'll need to determine the position of the "detached house" level by using the levels() function. Then, access the level with its index in the array that levels() returned, and assign a new name to it.