gpt4 book ai didi

r - 通过 id 将数据匹配到最近的时间值

转载 作者:行者123 更新时间:2023-12-01 09:46:08 25 4
gpt4 key购买 nike

我已经生成了一系列每小时时间戳:

intervals <- seq(as.POSIXct("2018-01-20 00:00:00", tz = 'America/Los_Angeles'), as.POSIXct("2018-01-20 03:00:00", tz = 'America/Los_Angeles'), by="hour")

> intervals
[1] "2018-01-20 00:00:00 PST" "2018-01-20 01:00:00 PST" "2018-01-20 02:00:00 PST"
[4] "2018-01-20 03:00:00 PST"

给定一个带有杂乱且间隔不均匀的时间戳的数据集,如何将该数据集中的时间值与 id 最接近的每小时时间戳相匹配,并删除其间的其他时间戳?例如:

> test
time id amount
312 2018-01-20 00:02:14 PST 1 54.9508346
8652 2018-01-20 00:54:41 PST 2 30.5557992
13809 2018-01-20 01:19:27 PST 3 90.5459248
586 2018-01-20 00:03:35 PST 1 79.7635973
9077 2018-01-20 00:56:37 PST 2 75.5356406
21546 2018-01-20 02:25:05 PST 3 36.6017705
7275 2018-01-20 00:47:45 PST 1 12.7618139
12768 2018-01-20 01:15:30 PST 2 72.4465838
1172 2018-01-20 00:08:01 PST 3 81.0468155
24106 2018-01-20 03:04:10 PST 1 0.8615881
14464 2018-01-20 01:25:04 PST 2 49.8718743
15344 2018-01-20 01:29:30 PST 3 85.0054113
14255 2018-01-20 01:23:22 PST 1 34.5093891
21565 2018-01-20 02:25:40 PST 2 69.0175725
15602 2018-01-20 01:31:32 PST 3 61.8602426

会产生:

> output
interval id amount
1 2018-01-20 01:00:00 1 12.7618139
2 2018-01-20 1 54.9508346
3 2018-01-20 03:00:00 1 0.8615881
4 2018-01-20 01:00:00 2 75.5356400
5 2018-01-20 02:00:00 2 69.0175700
6 2018-01-20 3 81.0468200
7 2018-01-20 01:00:00 3 90.5459200
8 2018-01-20 02:00:00 3 36.6017700

我了解 data.table

中存在可能的解决方案
setDT(reference)[data, refvalue, roll = "nearest", on = "datetime"]

使用 roll = 最近,但是如何在 test 中为每个 id 继续在 intervals 中找到最接近的匹配项code> 并保留 amount 属性?

任何建议将不胜感激!这是示例数据:

 dput(test)
structure(list(time = c("2018-01-20 00:02:14 PST", "2018-01-20 00:54:41 PST",
"2018-01-20 01:19:27 PST", "2018-01-20 00:03:35 PST", "2018-01-20 00:56:37 PST",
"2018-01-20 02:25:05 PST", "2018-01-20 00:47:45 PST", "2018-01-20 01:15:30 PST",
"2018-01-20 00:08:01 PST", "2018-01-20 03:04:10 PST", "2018-01-20 01:25:04 PST",
"2018-01-20 01:29:30 PST", "2018-01-20 01:23:22 PST", "2018-01-20 02:25:40 PST",
"2018-01-20 01:31:32 PST"), id = c(1, 2, 3, 1, 2, 3, 1, 2, 3,
1, 2, 3, 1, 2, 3), amount = c(54.9508346011862, 30.5557992309332,
90.5459248460829, 79.763597343117, 75.5356406327337, 36.6017704829574,
12.7618139144033, 72.4465838400647, 81.0468154959381, 0.861588073894382,
49.8718742514029, 85.0054113194346, 34.5093891490251, 69.0175724914297,
61.8602426256984)), .Names = c("time", "id", "amount"), row.names = c(312L,
8652L, 13809L, 586L, 9077L, 21546L, 7275L, 12768L, 1172L, 24106L,
14464L, 15344L, 14255L, 21565L, 15602L), class = "data.frame")

最佳答案

另一种选择是在 j 内加入 data.table:

# convert 'test' to a 'data.table' first with 'setDT'
# and convert the 'time'-column tot a datetime format
setDT(test)[, time := as.POSIXct(time)][]

# preform the join
test[, .SD[.(time = intervals), on = .(time), roll = 'nearest'], by = id]

给出:

    id                time     amount
1: 1 2018-01-20 00:00:00 54.9508346
2: 1 2018-01-20 01:00:00 12.7618139
3: 1 2018-01-20 02:00:00 34.5093891
4: 1 2018-01-20 03:00:00 0.8615881
5: 2 2018-01-20 00:00:00 30.5557992
6: 2 2018-01-20 01:00:00 75.5356406
7: 2 2018-01-20 02:00:00 69.0175725
8: 2 2018-01-20 03:00:00 69.0175725
9: 3 2018-01-20 00:00:00 81.0468155
10: 3 2018-01-20 01:00:00 90.5459248
11: 3 2018-01-20 02:00:00 36.6017705
12: 3 2018-01-20 03:00:00 36.6017705

在上述方法中,一些 amount 值由 id 分配给多个 time。如果您不希望这样并且只想保留最接近 time 的那些,您可以按如下方式改进方法:

test[, r := rowid(id)
][, .SD[.(time = intervals)
, on = .(time)
, roll = 'nearest'
, .(time, amount, r, time_diff = abs(x.time - i.time))
][, .SD[which.min(time_diff)], by = r]
, by = id][, c('r','time_diff') := NULL][]

给出:

    id                time     amount
1: 1 2018-01-20 00:00:00 54.9508346
2: 1 2018-01-20 01:00:00 12.7618139
3: 1 2018-01-20 02:00:00 34.5093891
4: 1 2018-01-20 03:00:00 0.8615881
5: 2 2018-01-20 00:00:00 30.5557992
6: 2 2018-01-20 01:00:00 75.5356406
7: 2 2018-01-20 02:00:00 69.0175725
8: 3 2018-01-20 00:00:00 81.0468155
9: 3 2018-01-20 01:00:00 90.5459248
10: 3 2018-01-20 02:00:00 36.6017705

关于r - 通过 id 将数据匹配到最近的时间值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48457575/

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