Value Labels

To understand value labels in R, you need to understand the data structure factor.

You can use the factor function to create your own value labels.

# variable v1 is coded 1, 2 or 3
# we want to attach value labels 1=red, 2=blue, 3=green

mydata$v1 <- factor(mydata$v1,
levels = c(1,2,3),
labels = c("red", "blue", "green"))
# variable y is coded 1, 3 or 5
# we want to attach value labels 1=Low, 3=Medium, 5=High

mydata$v1 <- ordered(mydata$y,
levels = c(1,3, 5),
labels = c("Low", "Medium", "High"))

Use the factor() function for nominal data and the ordered() function for ordinal data. R statistical and graphic functions will then treat the data appriopriately.

Note: factor and ordered are used the same way, with the same arguments. The former creates factors and the later creates ordered factors.

To Practice

Factors are covered in the fourth chapter of this free interactive introduction to R course.