I am new to R and new to stackoverflow. I am trying to figure out data.table and looked at "R data.table replacing an index of values from another data.table" and thought I understood but can't get what I want.
I have two data frames- the first is the data I'm interested in and the second is a key containing names/ids to translate the ids in the first data frame. I would like to use the "key" data.table to convert the numbers in table$id1 and table$id2 into the "Names" in the "key" data.table. Here's what I've managed so far:
table<-data.table("Sample" = sample(40:46, 6), "Conc1" = sample(100:106,6),
"id1" = as.character(sample(1:6, 6)), "Conc2" = sample(200:206,6),
"id2" = as.character(sample(1:6, 6)))
key<-data.table("Name" = c("Sally", "John", "Roger", "Bob", "Kelsey", "Molly"),
"id1" = as.character(1:6))
setkey(table, id1)
setkey(key, id1)
table[key, `:=`(id1 = i.Name)]
I've gotten this far (substituted values for names in table$id1) but can't figure out how to also change id2 without changing the column names, resetting the keys and re-doing the same step above for id2. In the real data set, there will be multiple Sally's, John's etc. and I want the code to "translate" both columns using the same key.
Hoping the code uses data.table (for learning purposes) but if there is another package that will do this better, that would be great too. Thanks!