gpt4 book ai didi

r - 如何将两个 data.frame 与不精确匹配的标识符进行匹配(一个标识符必须在另一个标识符的范围内)

转载 作者:行者123 更新时间:2023-12-01 21:42:22 26 4
gpt4 key购买 nike

我有以下匹配问题:我有两个 data.frame,一个每个月都有一次观察(每个公司 ID),另一个每个季度有一个观察(每个公司 ID;请注意,季度表示财政季度;因此 1Q = Jan、Feb、Mar 不一定正确,而且财政季度不一定是 3 个月长)。

对于每个月和公司,我想获得该季度的正确值。因此,几个月的值与一个季度的值相同。作为示例,请参阅下面的代码:

monthlyData <- data.frame(ID = rep(c("A", "B"), each = 5),
Month = rep(1:5, times = 2),
MonValue = 1:10)
monthlyData
ID Month MonValue
1 A 1 1
2 A 2 2
3 A 3 3
4 A 4 4
5 A 5 5
6 B 1 6
7 B 2 7
8 B 3 8
9 B 4 9
10 B 5 10

#Quarterly data, i.e. the value of every quarter has to be matched to several months in d1
#However, I want to match fiscal quarters, which means that one quarter is not necessarily 3 month long
qtrData <- data.frame(ID = rep(c("A", "B"), each = 2),
startMonth = c(1, 4, 1, 3),
endMonth = c(3, 5, 2, 5),
QTRValue = 1:4)
qtrData
ID startMonth endMonth QTRValue
1 A 1 3 1
2 A 4 5 2
3 B 1 2 3
4 B 3 5 4

#Desired output
ID Month MonValue QTRValue
1 A 1 1 1
2 A 2 2 1
3 A 3 3 1
4 A 4 4 2
5 A 5 5 2
6 B 1 6 3
7 B 2 7 3
8 B 3 8 4
9 B 4 9 4
10 B 5 10 4

注意:这个问题几个月前发布在 R-help 上,但当时我没有得到任何答案,我自己找到了解决方案(参见 R-help )。然而,现在我在 stackoverflow 上发布了一个问题,其中我有一个关于 data.table 的问题,其中也提到了这个问题,Andrie 让我再次发布这个问题,因为他显然有一个很好的解决方案(参见 Question on SO )

更新:请参阅 Matthew Dowle 的评论:真实数据看起来如何?

这个数据是比较真实的。我添加了几行,但唯一更改的主要部分是 qtrData 中的 endMonth 列。更准确地说,startMonth 不一定是上一季度加一个月的 endMonth。因此,使用 roll 选项,我认为您需要另一行代码(如果没有,您将返回 20 行,但使用 Andrie 的解决方案(这是所需的解决方案),您将返回 17 行) 。如果我没有错过这里的任何内容,那么就不再有性能差异了。

monthlyData_new <- data.table(ID = rep(c("A", "B"), each = 10),
Month = rep(1:10, times = 2),
MonValue = 1:20)

qtrData_new <- data.table(ID = rep(c("A", "B"), each = 3),
startMonth = c(1, 4, 7, 1, 3, 8),
endMonth = c(3, 5, 10, 2, 5, 10),
QTRValue = 1:6)

setkey(qtrData_new, ID)
setkey(monthlyData_new, ID)

qtrData1 <- qtrData_new
setkey(qtrData1, ID, startMonth)
monthlyData1 <- monthlyData_new
setkey(monthlyData1, ID, Month)

withTable1 <- function(){
xx <- qtrData1[monthlyData1, roll=TRUE]
xx <- xx[startMonth <= endMonth]

}

withTable2 <- function(){
yy <- monthlyData_new[qtrData_new][Month >= startMonth & Month <= endMonth]

}

benchmark(withTable1, withTable2, replications=1e6)
test replications elapsed relative user.self sys.self user.child sys.child
1 withTable1 1000000 4.244 1.028599 4.232 0.008 0 0
2 withTable2 1000000 4.126 1.000000 4.096 0.028 0 0

最佳答案

试试这个:

mD = data.table(monthlyData, key="ID,Month")
qD = data.table(qtrData,key="ID,startMonth")
qD[mD,roll=TRUE]
ID startMonth endMonth QTRValue MonValue
[1,] A 1 3 1 1
[2,] A 2 3 1 2
[3,] A 3 3 1 3
[4,] A 4 5 2 4
[5,] A 5 5 2 5
[6,] B 1 2 3 6
[7,] B 2 2 3 7
[8,] B 3 5 4 8
[9,] B 4 5 4 9
[10,] B 5 5 4 10

那应该快得多。

编辑:回答有问题的后续编辑。一种方法是使用 NA 存储缺失月份的位置。我发现查看一个时间序列列(不规则,有间隙和 NA)比查看两个创建一系列范围更容易。

> mD <- data.table(ID = rep(c("A", "B"), each = 10),
+ Month = rep(1:10, times = 2),
+ MonValue = 1:20, key="ID,Month")
>
> qD <- data.table(ID = rep(c("A", "B"), each = 4),
+ Month = c(1,4,6,7, 1,3,6,8),
+ QtrValue = c(1,2,NA,3, 4,5,NA,6),
+ key="ID,Month")
>
> mD
ID Month MonValue
[1,] A 1 1
[2,] A 2 2
[3,] A 3 3
[4,] A 4 4
[5,] A 5 5
[6,] A 6 6
[7,] A 7 7
[8,] A 8 8
[9,] A 9 9
[10,] A 10 10
[11,] B 1 11
[12,] B 2 12
[13,] B 3 13
[14,] B 4 14
[15,] B 5 15
[16,] B 6 16
[17,] B 7 17
[18,] B 8 18
[19,] B 9 19
[20,] B 10 20
> qD
ID Month QtrValue
[1,] A 1 1
[2,] A 4 2
[3,] A 6 NA # missing for 1 month (6)
[4,] A 7 3
[5,] B 1 4
[6,] B 3 5
[7,] B 6 NA # missing for 2 months (6 and 7)
[8,] B 8 6
> qD[mD,roll=TRUE]
ID Month QtrValue MonValue
[1,] A 1 1 1
[2,] A 2 1 2
[3,] A 3 1 3
[4,] A 4 2 4
[5,] A 5 2 5
[6,] A 6 NA 6
[7,] A 7 3 7
[8,] A 8 3 8
[9,] A 9 3 9
[10,] A 10 3 10
[11,] B 1 4 11
[12,] B 2 4 12
[13,] B 3 5 13
[14,] B 4 5 14
[15,] B 5 5 15
[16,] B 6 NA 16
[17,] B 7 NA 17
[18,] B 8 6 18
[19,] B 9 6 19
[20,] B 10 6 20
> qD[mD,roll=TRUE][!is.na(QtrValue)]
ID Month QtrValue MonValue
[1,] A 1 1 1
[2,] A 2 1 2
[3,] A 3 1 3
[4,] A 4 2 4
[5,] A 5 2 5
[6,] A 7 3 7
[7,] A 8 3 8
[8,] A 9 3 9
[9,] A 10 3 10
[10,] B 1 4 11
[11,] B 2 4 12
[12,] B 3 5 13
[13,] B 4 5 14
[14,] B 5 5 15
[15,] B 8 6 18
[16,] B 9 6 19
[17,] B 10 6 20

关于r - 如何将两个 data.frame 与不精确匹配的标识符进行匹配(一个标识符必须在另一个标识符的范围内),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8011333/

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