3

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?

Kirk Fogg
  • 521
  • 5
  • 14

1 Answers1

3

If I understood your problem, then I guess you really want to lookup/merge the table, if so then you can use data.table to solve this problem:

library(data.table)
data <- data.table(data)
code.list <- data.table(code.list)
data[code.list, class := i.class, on="code"]

Thanks Frank, I have been guided that there is better way of doing things in data.table , I have updated the same.

data[code.list, class := i.class,on="code"]

    > data
        code   class
     #1:    5 Class 5
     #2:    5 Class 5
     #3:    6 Class 6
     #4:    6 Class 6
     #5:    5 Class 5
    ---             
 #99996:    5 Class 5
 #99997:    6 Class 6
 #99998:    8 Class 8
 #99999:    6 Class 6
#100000:    5 Class 5
> 
PKumar
  • 10,971
  • 6
  • 37
  • 52
  • 2
    Fyi, correct data.table style would be to add a col to data, rather than making a new table: `data[code.list, class := i.class]`. Also, you can use `on=` instead of setting keys. Also, you can use `setDT(d)` instead of `d <- data.table(d)`. – Frank Apr 18 '17 at 17:28
  • 1
    @Frank Thanks for the gyan, updated the same – PKumar Apr 18 '17 at 17:34
  • 2
    Thanks for the answers, saves a ton of time! – Kirk Fogg Apr 18 '17 at 17:40