gpt4 book ai didi

r - 使用不等式表达式的内连接

转载 作者:行者123 更新时间:2023-12-04 08:33:37 26 4
gpt4 key购买 nike

背景

(问题不是必需的,但阅读可能有用)

Rolling join on data.table with duplicate keys

Odd behaviour when joining with multiple conditions

数据

library(data.table)   ## using version 1.9.6
## arrival timetable
dt_arrive <- structure(list(txn_id = c(1L, 1L, 1L, 1L, 1L), place = c("place_a",
"place_a", "place_a", "place_a", "place_a"), arrival_minutes = c(515,
534, 547, 561, 581), journey_id = 1:5), .Names = c("txn_id",
"place", "arrival_minutes", "journey_id"), class = c("data.table",
"data.frame"), row.names = c(NA, -5L), sorted = c("txn_id",
"place"))

## departure timetable
dt_depart <- structure(list(txn_id = c(1L, 1L, 1L, 1L), place = c("place_a",
"place_a", "place_a", "place_a"), arrival_minutes = c(489, 507,
519, 543), journey_id = 10:13), .Names = c("txn_id", "place",
"arrival_minutes", "journey_id"), sorted = c("txn_id", "place"
), class = c("data.table", "data.frame"), row.names = c(NA, -4L
))

> dt_arrive
txn_id place arrival_minutes journey_id
1: 1 place_a 515 1
2: 1 place_a 534 2
3: 1 place_a 547 3
4: 1 place_a 561 4
5: 1 place_a 581 5

> dt_depart
txn_id place arrival_minutes journey_id
1: 1 place_a 489 10
2: 1 place_a 507 11
3: 1 place_a 519 12
4: 1 place_a 543 13

问题

我想加入 的到达和离开只有那些 dt_depart$journey_id发生的 dt_arrive$journey_idarrival_minutes 方面(即 txn_idplace 上的内连接)

例如,我想要的输出是:
   txn_id   place journey_in_id journey_out_id journey_place_arrive journey_place_depart
1 place_a 1 12 515 519
1 place_a 1 13 515 543
1 place_a 2 13 534 543

尝试

使用我构建的两个链接问题中的方法
setkey(dt_arrive, txn_id, place)
setkey(dt_depart, txn_id, place)

dt_join <- dt_arrive[dt_depart,
{
idx = (i.arrival_minutes > arrival_minutes)
.(journey_in_id = journey_id[idx],
journey_out_id = i.journey_id,
journey_place_arrive = arrival_minutes[idx],
journey_place_depart = i.arrival_minutes
)
},
by=.EACHI]

但这给出了来自 dt_depart 的所有信息,所以包括 NA结果中的 s - 这表明“正确连接”:
   txn_id   place journey_in_id journey_out_id journey_place_arrive journey_place_depart
1: 1 place_a NA 10 NA 489
2: 1 place_a NA 11 NA 507
3: 1 place_a 1 12 515 519
4: 1 place_a 1 13 515 543
5: 1 place_a 2 13 534 543

我试过使用 nomatch=0强制它“内部连接”,但这没有用。

我可以用 complete.cases删除 NA行,但我想知道是否有办法在查询本身中做到这一点?

最佳答案

这是不聪明的方法:采用交叉/笛卡尔连接,然后过滤。

merge(dt_arrive, dt_depart, allow.cartesian=TRUE)[arrival_minutes.y > arrival_minutes.x]

# txn_id place arrival_minutes.x journey_id.x arrival_minutes.y journey_id.y
# 1: 1 place_a 515 1 519 12
# 2: 1 place_a 515 1 543 13
# 3: 1 place_a 534 2 543 13

通过采用笛卡尔连接,我们很可能会吃掉很多内存。

关于r - 使用不等式表达式的内连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33072658/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com