我正在整合数据以将其放入新数据库中,并且在用新的外键(FK)变量替换旧列时遇到一些问题。我将所有数据合并为长格式,然后再次将其转换为新数据模型的新表结构。由于通用变量(如“总计”)的某些名称重复,并且将一些旧名称合并为新名称,因此直接合并并不总是有效,因为名称将不匹配。我可以使用一些变通办法,也许还有一些其他概念上的改进可以解决我的问题。但是,我通常对是否有一种方法可以将FK合并并替换为长数据表感到好奇,就像它适用于宽表一样。
搜索“合并”或“长”的线程总是会产生其他熔化问题,这就是为什么我要问自己。
我尝试使用'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
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,]
例如,前四行应替换为变量“ sub_id”和值“ 4”的一行
如果我理解正确(但可能是我遗漏了要点),问题是如何合并长格式的子集。也许,OP正在寻找以下方面的内容。
第一步是要了解子集在某种程度上等效于查找表的合并/联接。因此,
library(data.table) # just to highlight that data.table syntax is used here
dt_sub[vs == 0 & am == 1 & gear == 4 & carb == 4, ]
返回与]相同的结果>
lut_wide <- data.table(vs = 0, am = 1, gear = 4, carb = 4) dt_sub[lut_wide, on = names(lut_wide)]
vs am gear carb id_sub 1: 0 1 4 4 1
要以长格式进行同样的操作,还需要调整查找表的形状:
lut_long <- melt(lut_wide, measure.vars = names(lut_wide))
variable value 1: vs 0 2: am 1 3: gear 4 4: carb 4
或者,fread()
可用于从头开始以长格式创建查找表:
的那些lut_long <- fread( "variable, value vs, 0 am, 1 gear, 4 carb, 4")
现在,我们需要找到满足条件[[全部4
id
:idx <- dt_long[lut_long, on = .(variable, value)][, which(.N == nrow(lut_long)), by = id][, .(id)]
idx
id 1: 1 2: 2
最后,idx
用于子集dt_long
(通过加入):
dt_long[variable %in% merge_cols, ][idx, on = "id"]
id variable value 1: 1 vs 0 2: 1 am 1 3: 1 gear 4 4: 1 carb 4 5: 2 vs 0 6: 2 am 1 7: 2 gear 4 8: 2 carb 4
这里,仅考虑在merge_cols
中定义的变量。显然,结果包含重复项。可以通过仅填充idx
的第一行将其删除:
dt_long[variable %in% merge_cols, ][first(idx), on = "id"]
id variable value 1: 1 vs 0 2: 1 am 1 3: 1 gear 4 4: 1 carb 4