gpt4 book ai didi

r - 在 data.table 中添加带有条件的虚拟对象?

转载 作者:行者123 更新时间:2023-12-04 12:17:13 24 4
gpt4 key购买 nike

抱歉问了这么长的问题。我会尽我所能明确地阐明我的目标

我想使用 update 方法在 data.table 中添加虚拟对象,就像 this already answered in this link ,但稍微复杂一点。

为了更好地描述,我创建了数据。

DT <- data.table(UID = paste0("UID",rep(1:5,each=2)), 
date = as.IDate(c("2012-01-01","2012-01-02","2012-01-03","2012-01-04","2012-01-05","2012-01-06","2012-02-01","2012-02-02","2012-02-03","2012-02-04")),
value = c(1:10))

DT 是一个数据表,包含 UID、日期和值的信息。在原始数据中,结构相同,但时间跨度较长(2年)。

在这里,我想根据日期添加假人。

日期有几个特殊的时间跨度,我们可以用假期来表示它们。

例如,在我上面创建的假数据中。

有两个假期
  • 从“2012-01-02”到“2012-01-05”
  • 从“2012-02-02”到“2012-02-03”

  • 我想添加两种类型的假人
  • 关于假期长度的假人:首先计算不同假期的长度。在这个例子中,我们有两个不同的长度(2 和 4)。因此,我们将添加 2 个假人,指示日期是否在这些假期中。

  • 预期的结果是这样的:
    UID     Date    Val D_length_2  D_length_4UID1    1/1/2012    1   FALSE   FALSEUID2    1/2/2012    2   FALSE   TRUEUID3    1/3/2012    3   FALSE   TRUEUID4    1/4/2012    4   FALSE   TRUEUID5    1/5/2012    5   FALSE   TRUEUID1    1/6/2012    6   FALSE   FALSEUID2    2/1/2012    7   TRUE    FALSEUID3    2/2/2012    8   TRUE    FALSEUID4    2/3/2012    9   FALSE   FALSEUID5    2/4/2012    10  FALSE   FALSE
    1. Dummies about whether the day is exactly one day before the vacation, or exactly one day after the vacation.
    UID    Date      Val    Before  AfterUID1    1/1/2012    1   TRUE    FALSEUID2    1/2/2012    2   FALSE   FALSEUID3    1/3/2012    3   FALSE   FALSEUID4    1/4/2012    4   FALSE   FALSEUID5    1/5/2012    5   FALSE   FALSEUID1    1/6/2012    6   FALSE   TRUEUID2    2/1/2012    7   TRUE    FALSEUID3    2/2/2012    8   FALSE   FALSEUID4    2/3/2012    9   FALSE   FALSEUID5    2/4/2012    10  FALSE   TRUE

    So the total of desired results is like this

    UID Date    Val Before  After   D_length_2  D_length_4UID1    1/1/2012    1   TRUE    FALSE   FALSE   FALSEUID2    1/2/2012    2   FALSE   FALSE   FALSE   TRUEUID3    1/3/2012    3   FALSE   FALSE   FALSE   TRUEUID4    1/4/2012    4   FALSE   FALSE   FALSE   TRUEUID5    1/5/2012    5   FALSE   FALSE   FALSE   TRUEUID1    1/6/2012    6   FALSE   TRUE    FALSE   FALSEUID2    2/1/2012    7   TRUE    FALSE   FALSE   FALSEUID3    2/2/2012    8   FALSE   FALSE   TRUE    FALSEUID4    2/3/2012    9   FALSE   FALSE   TRUE    FALSEUID5    2/4/2012    10  FALSE   TRUE    FALSE   FALSE

    The total observations are more than 10M rows, with about 10 different vacations and 4 different length.

    For the second type of dummies, I think

    f <- function(x){ 
    ifelse(x %in% as.Date(c("2012-01-02","2012-02-02")) - 1, return(TRUE), return(FALSE))
    }

    DT[,Before:= f(date)]

    但似乎不正确。

    对于第一个,我没有想出一个好的解决方案。

    这个问题是关于data.table中的更新,任何关于如何处理它以及如何编写更新函数的想法都非常欢迎!

    最佳答案

    这是一个开始:

    library(data.table)

    DT <- data.table(UID = paste0("UID",rep(1:5,each=2)),
    date = as.IDate(c("2012-01-01","2012-01-02","2012-01-03","2012-01-04","2012-01-05","2012-01-06","2012-02-01","2012-02-02","2012-02-03","2012-02-04")),
    value = c(1:10))
    setkey(DT, date)


    vacStart <- data.table(start = as.IDate(c("2012-01-02", "2012-02-02")), key="start")
    vacEnd <- data.table(date = as.IDate(c("2012-01-05", "2012-02-03")), key="date")

    #identify vacations:
    vacStart[, Start:=.I]
    DT <- vacStart[DT, roll=TRUE]
    vacEnd[, End:=.I]
    DT <- vacEnd[DT, roll=-Inf]
    DT[,vac:=(End==Start)*Start]
    DT[is.na(vac), vac:=0L]

    #2-day vacations:
    DT[,length_2 := (.N==2) & vac!=0, by=vac]
    #days before vacation
    DT[,before := c(diff(vac)>0, FALSE) & vac==0]
    # date End Start UID value vac length_2 before
    # 1: 2012-01-01 1 NA UID1 1 0 FALSE TRUE
    # 2: 2012-01-02 1 1 UID1 2 1 FALSE FALSE
    # 3: 2012-01-03 1 1 UID2 3 1 FALSE FALSE
    # 4: 2012-01-04 1 1 UID2 4 1 FALSE FALSE
    # 5: 2012-01-05 1 1 UID3 5 1 FALSE FALSE
    # 6: 2012-01-06 2 1 UID3 6 0 FALSE FALSE
    # 7: 2012-02-01 2 1 UID4 7 0 FALSE TRUE
    # 8: 2012-02-02 2 2 UID4 8 2 TRUE FALSE
    # 9: 2012-02-03 2 2 UID5 9 2 TRUE FALSE
    # 10: 2012-02-04 NA 2 UID5 10 0 FALSE FALSE

    关于r - 在 data.table 中添加带有条件的虚拟对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21140218/

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