gpt4 book ai didi

同时向前和向后滚动 data.table

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

这与上一个关于 SO 的问题有关:roll data.table with rollends

鉴于数据...

library(data.table)

dt1 = data.table(Date=seq(from=as.Date("2013-01-03"),
to=as.Date("2013-06-27"), by="1 week"),
key="Date")[, ind:=.I]
dt2 = data.table(Date=seq(from=as.Date("2013-01-01"),
to=as.Date("2013-06-30"), by="1 day"),
key="Date")

我尝试 将每周数据点向前和向后移动一天 ...
dt1[dt2, roll=1][dt2, roll=-1]

...但只有第一个滚动连接(向前)似乎有效, roll=-1被忽略:
           Date ind
1: 2013-01-01 NA
2: 2013-01-02 NA
3: 2013-01-03 1
4: 2013-01-04 1
5: 2013-01-05 NA
---
177: 2013-06-26 NA
178: 2013-06-27 26
179: 2013-06-28 26
180: 2013-06-29 NA
181: 2013-06-30 NA

当我颠倒顺序时,效果相同:
dt1[dt2, roll=-1][dt2, roll=1]

Date ind
1: 2013-01-01 NA
2: 2013-01-02 1
3: 2013-01-03 1
4: 2013-01-04 NA
5: 2013-01-05 NA
---
177: 2013-06-26 26
178: 2013-06-27 26
179: 2013-06-28 NA
180: 2013-06-29 NA
181: 2013-06-30 NA

我想实现:
           Date ind
1: 2013-01-01 NA
2: 2013-01-02 1
3: 2013-01-03 1
4: 2013-01-04 1
5: 2013-01-05 NA
---
177: 2013-06-26 26
178: 2013-06-27 26
179: 2013-06-28 26
180: 2013-06-29 NA
181: 2013-06-30 NA

编辑:
我用的是新鲜的 数据表版本 1.8.11 ,注意 session 细节:
sessionInfo()

R version 3.0.0 (2013-04-03)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United Kingdom.1252
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C
[5] LC_TIME=English_United Kingdom.1252

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] data.table_1.8.11

loaded via a namespace (and not attached):
[1] plyr_1.8 reshape2_1.2.2 stringr_0.6.2 tools_3.0.0

谢谢

最佳答案

第一次加入后没有什么可滚动的,每一行都在dt2 dt1[dt2, roll = .] 中的相应行.所以只需分别做两个卷并将它们组合在一起,例如:

dt1[dt2, roll = 1][, ind := ifelse(is.na(ind), dt1[dt2, roll = -1]$ind, ind)]

关于同时向前和向后滚动 data.table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19252969/

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