I am consolidating data to put it into a new data base and I am having some issues with replacing the old columns with the new Foreign Key (FK) variables. I am consolidating all my data into a long format and then cast it again to create the new table structure of the new data model. Due to some name duplicates for generic variables like 'total' and merging some old names into new ones, direct merging does not always work because the names would not match. There are some work arounds that I can use and maybe there are some other conceptual improvements to fix my issues. However, I am generally courious if there is a way to merge and replace a FK into a long data table, like it works for a wide table.
Searching for threads with "merging" or "long" always yields other melting problems, thats why I am asking myself.
I tried creating a generic example using 'mtcars'
library(data.table)
dt<-data.table(mtcars)
#Create Id to melt the data
dt[,id:=1:.N]
dt_long<-melt(dt, id.vars="id")
#Create a unique sub table for 4 variables of mtcars with a new sub_id
merge_cols<-c("vs","am","gear","carb")
dt_sub<-dt[,..merge_cols]
dt_sub<-unique(dt_sub)
dt_sub[,id_sub:=1:.N]
# Easy to merge with orginal table and delete old columns
dt<-merge(dt,dt_sub, by=merge_cols )
set(dt, , merge_cols, NULL)
#Any way to do this with the long data frame?
setorder(dt_long, id)
dt_long[variable %in% merge_cols,]
dt_sub[vs==0 & am==1 & gear ==4 & carb==4,]
For instance the first 4 rows should be replaced by one row with variable "sub_id" and value "4". Just if I were to melt the merged 'dt' from above using the wide original dt. So my final result should look like I melted the wide table after it has been merged with the sub table, but for each four rows of the 'merge_cols' we have one row with 'sub_id'
dt_final<-melt(dt, id.vars="id" )
dt_final[variable=="id_sub"]