【问题标题】:Sum shares/number of rows according to date by groups in R data.table/frame根据 R data.table/frame 中的组按日期汇总份额/行数
【发布时间】:2021-12-24 03:32:46
【问题描述】:

我想计算上一年按组 B (country) 的组 A (industry) 的唯一值的出现次数的平方和(分别为行数)。

计算示例第 5 行2x A + 1x B + 1x C = 2^2+1^2+^+1^2 = 6(不包括第 1 行的 A,因为它超过一年,也不包括第 6 行的 A,因为它在另一个国家/地区) .

我设法按行计算数字,但未能将其移至聚合日期级别:

dt[, count_by_industry:= sapply(date, function(x) length(industry[between(date, x - lubridate::years(1), x)])), 
    by = c("country", "industry")]

该解决方案理想地扩展到具有约 200 万行和大约 10k 日期和组元素的真实数据(因此有 data.table 标签)。


示例数据

ID    <- c("1","2","3","4","5","6")
Date <- c("2016-01-02","2017-01-01", "2017-01-03", "2017-01-03", "2017-01-04","2017-01-03")
Industry <- c("A","A","B","C","A","A")
Country <- c("UK","UK","UK","UK","UK","US")
Desired <- c(1,4,3,3,6,1)

library(data.table)
dt <- data.frame(id=ID, date=Date, industry=Industry, country=Country, desired_output=Desired)
setDT(dt)[, date := as.Date(date)]

【问题讨论】:

    标签: r data.table


    【解决方案1】:

    从一开始就适应:

    dt[, output:= sapply(date, function(x) sum(table(industry[between(date, x - lubridate::years(1), x)]) ^ 2)), 
       by = c("country")]
    dt
       id       date industry country desired_output output
    1:  1 2016-01-02        A      UK              1      1
    2:  2 2017-01-01        A      UK              4      4
    3:  3 2017-01-03        B      UK              3      3
    4:  4 2017-01-03        C      UK              3      3
    5:  5 2017-01-04        A      UK              6      6
    6:  6 2017-01-03        A      US              1      1
    

    【讨论】:

    • 太好了,谢谢,我不知道table 也可以这样做。注意:似乎必须特别注意“一年”的确切定义(关于betweenlubridate:years 以及&lt;=, &gt; 的潜在其他代码)。
    • 我同意。没有代码很难知道“过去一年”是什么意思。感谢您将代码放入问题中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2016-07-31
    • 1970-01-01
    • 2018-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多