我有两个数据表“通勤者”和“距离”。距离是一个带有行索引和列索引的巨大距离矩阵。通勤者有“家”和“目的地”栏。
通勤者(1646044 行):
家 | 目的地 |
---|---|
2 | 2 |
1 | 2 |
3 | 3 |
1 | 4 |
1 | 4 |
距离(1187行):
1 | 2 | 3 | 4 | |
---|---|---|---|---|
1 | 238.23 | 453 | 263 | 1 |
2 | 21.2 | 1 | 238.23 | 238.23 |
3 | 577.98 | 238.23 | 4362 | 2443.22 |
4 | 234.12 | 987.98 | 89.93 | 12.21 |
我想为 df 通勤者添加一个新列“距离”。因此,如果“home”中的值与“distance”的行索引匹配,并且“destination”与“distance”的列索引匹配,我想将矩阵中的相应值添加到这个新列“distance”中“通勤者”。问题是“家”有时会多次出现相同的值(这就是为什么通勤者的行数比距离的行数多的原因)。 R 不断给我错误“cpmmuters”中的行与“distance”中的行不匹配。但是,我想保留重复项(如下表所示:“1 | 4”出现两次)。
所需输出:
家 | 目的地 | 距离 |
---|---|---|
2 | 2 | 1 |
1 | 2 | 453 |
3 | 3 | 4362 |
1 | 4 | 1 |
1 | 4 | 1 |
我尝试过的:
commuters$distance <- distance[cbind(commuters$home, commuters$destination)]
我收到此错误: i 是无效类型(矩阵)。也许将来 2 列矩阵可以返回 DT 元素列表(本着 FAQ 2.14 中 A[B] 的精神)。如果您愿意,请向 data.table 问题跟踪器报告,或将您的评论添加到 FR #657。
我该如何解决这个问题?
我会将其作为熔化和合并来完成。
设置
dist<-data.table(home = 0:4,
`1` = c(1, 238.23, 21.2, 577.98, 234.12),
`2` = c(2, 453, 1, 238.23, 987.98),
`3` = c(3, 263, 238.23,4362, 89.93),
`4` = c(4, 1, 238.23, 2443.22, 12.21))
comm <- data.table(home=c(2,1,3,1,1), destination=c(2,2,3,4,4))
merge(
comm,
melt(dist, id.vars='home',
value.name = 'distance',
variable.factor = FALSE,
variable.name='destination')[,
destination:=as.numeric(destination)],
by=c('destination', 'home')
)
最后
merge(
comm,
melt(dist, id.vars='home',
value.name = 'distance',
variable.factor = FALSE,
variable.name='destination')[,
destination:=as.numeric(destination)],
by=c('destination', 'home')
)
destination home distance
1: 2 1 453
2: 2 2 1
3: 3 3 4362
4: 4 1 1
5: 4 1 1