I got a table in which I would like to replace values of a single column using a lookup-table.
Using lapply this works fine. However, with a huge dataset it runs hours.
Is there a faster alternative? Thanks!
MWE:
require(data.table)
# Dummy data.
dt_lookup <- data.table(cls_old=c(1:5), cls_new=c(5:1)) # Lookup-Table, the real data has different and non-continous entries.
dt <- data.table(cls=c(5:1), data=c(1,2,3,4,5)) # Table in which data shall be replaced depending on the lookup-table.
# Function to get the new column entry for every row based on the lookup-table.
get_new_label <- function(cls) {
return(dt_lookup[cls_old==cls]$cls_new)
}
# Actual replacement of values.
dt <- dt[,cls:=lapply(cls, get_new_label)]