Here's a scaled down sample of my problem. I have a data.table with a column of multiple IDs in vector form. These IDs all correspond to words in another data.table.
ID.table <- data.table(IDs = list(c(4, 5, 6), c(2, 3, 4)))
word.table <- data.table(ID = c(1, 2, 3, 4, 5, 6), word = c("This", "is", "a", "test", "sentence", "."))
which yields
IDs
1: 4,5,6
2: 2,3,4
and
ID word
1: 1 This
2: 2 is
3: 3 a
4: 4 test
5: 5 sentence
6: 6 .
I need to convert all the IDs in ID.table to the corresponding words in word.table, like in the following.
IDs
1: test,sentence,.
2: is,a,test
I know I can do this using a for loop and looping through every vector in ID.table, but my actual table has thousands of rows, which means it runs very slowly.
row <- 1
for(ID.row in ID.table[, IDs]){
word.row <- word.table[ID %in% ID.row]$word
ID.table[row] <- word.row
row <- row + 1
}
Is there a more efficient way to do this?
EDIT: I made a mistake by listing sequential IDs starting from 1 in word.table. ID.table and word.table would look something more like this.
IDs
1: 608,609,610
2: 606,607,608
and
ID word
1: 605 This
2: 606 is
3: 607 a
4: 608 test
5: 609 sentence
6: 610 .
where each row of ID.table will be a vector of sequential numbers not starting from 1, and the ID column of word.table will have not-always sequential ID numbers not starting from 1.