R data.table merge 产生不一致的结果(或者我遗漏了一些东西)
R data.table merge produces inconsistent results (or I am missing something)
a <- statuses[ID %between% c(59098, 59102), -c('Date')]
b <- restrictions[ID %between% c(59098, 59102), -c('Class', 'Date')]
c <- merge(a, b, by=c('Period', 'ID', 'MedID'), all.x=TRUE, allow.cartesian = TRUE)
d <- merge(statuses[ID %between% c(59098, 59102), -c('Date')],
restrictions[ID %between% c(59098, 59102), -c('Class', 'Date')],
by=c('Period', 'ID', 'MedID'), all.X=TRUE, allow.cartesian = TRUE)
a 有 4 行,b 有 5 行。c 有 7 行(正确)但 d 只有 5 行。
鉴于 c 和 d 基本上进行相同的合并,它们不应该具有相同的行数吗?
all.X=TRUE 是错误的,所以它并没有从那一边拿走全部
改成
d <- merge(statuses[ID %between% c(59098, 59102), -c('Date')],
restrictions[ID %between% c(59098, 59102), -c('Class', 'Date')],
by=c('Period', 'ID', 'MedID'), all.x=TRUE, allow.cartesian = TRUE
解释为什么 d 与 B 有相同的行。
a <- statuses[ID %between% c(59098, 59102), -c('Date')]
b <- restrictions[ID %between% c(59098, 59102), -c('Class', 'Date')]
c <- merge(a, b, by=c('Period', 'ID', 'MedID'), all.x=TRUE, allow.cartesian = TRUE)
d <- merge(statuses[ID %between% c(59098, 59102), -c('Date')],
restrictions[ID %between% c(59098, 59102), -c('Class', 'Date')],
by=c('Period', 'ID', 'MedID'), all.X=TRUE, allow.cartesian = TRUE)
a 有 4 行,b 有 5 行。c 有 7 行(正确)但 d 只有 5 行。
鉴于 c 和 d 基本上进行相同的合并,它们不应该具有相同的行数吗?
all.X=TRUE 是错误的,所以它并没有从那一边拿走全部 改成
d <- merge(statuses[ID %between% c(59098, 59102), -c('Date')],
restrictions[ID %between% c(59098, 59102), -c('Class', 'Date')],
by=c('Period', 'ID', 'MedID'), all.x=TRUE, allow.cartesian = TRUE
解释为什么 d 与 B 有相同的行。