I have a dataset where each observation has an integer "code" variable, which I would like to convert to a character "class" variable. Here is a simple example to illustrate what I am trying to do.
code.list <- data.frame(code = 1:10,
class = c("Class 1", "Class 2", "Class 3", "Class 4", "Class 5",
"Class 6", "Class 7", "Class 8", "Class 9", "Class 10"))
set.seed(1)
data <- data.frame(code = rbinom(100000, 10, 0.5))
> head(code.list, 4)
code class
1 1 Class 1
2 2 Class 2
3 3 Class 3
4 4 Class 4
> head(data, 4)
code
1 4
2 4
3 5
4 7
I want to add a "class" variable to data
, such that the class for each observation matches its corresponding "code" variable in code.list
. Is there a way to do this without using a for loop and iterating over every observation?