gpt4 book ai didi

r - 按列合并这些列的不同值

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

我有两个 data.tables。我想合并第二个 data.table 的行 dfB与第一 dfA ,如果 year来自 dfA对应于 year 一年前 dfB .

dfB的第一行为例将与 dfA 的第一行合并因为dfB的年份2009年是dfA的前一年,2010 年。

  library(data.table)
dfA <- fread("
A B C D E F G Z iso year matchcode
1 0 1 1 1 0 1 0 NLD 2010 NLD2010
2 1 0 0 0 1 0 1 NLD 2014 NLD2014
3 0 0 0 1 1 0 0 AUS 2010 AUS2010
4 1 0 1 0 0 1 0 AUS 2006 AUS2006
5 0 1 0 1 0 1 1 USA 2008 USA2008
6 0 0 1 0 0 0 1 USA 2010 USA2010
7 0 1 0 1 0 0 0 USA 2012 USA2012
8 1 0 1 0 0 1 0 BLG 2008 BLG2008
9 0 1 0 1 1 0 1 BEL 2008 BEL2008
10 1 0 1 0 0 1 0 BEL 2010 BEL2010
11 0 1 1 1 0 1 0 NLD 2010 NLD2010
12 1 0 0 0 1 0 1 NLD 2014 NLD2014
13 0 0 0 1 1 0 0 AUS 2010 AUS2010
14 1 0 1 0 0 1 0 AUS 2006 AUS2006
15 0 1 0 1 0 1 1 USA 2008 USA2008
16 0 0 1 0 0 0 1 USA 2010 USA2010
17 0 1 0 1 0 0 0 USA 2012 USA2012
18 1 0 1 0 0 1 0 BLG 2008 BLG2008
19 0 1 0 1 1 0 1 BEL 2008 BEL2008
20 1 0 1 0 0 1 0 BEL 2010 BEL2010",
header = TRUE)

dfB <- fread("
A B C D H I J K iso year matchcode
1 0 1 1 1 0 1 0 NLD 2009 NLD2009
2 1 0 0 0 1 0 1 NLD 2014 NLD2014
3 0 0 0 1 1 0 0 AUS 2011 AUS2011
4 1 0 1 0 0 1 0 AUS 2007 AUS2007
5 0 1 0 1 0 1 1 USA 2007 USA2007
6 0 0 1 0 0 0 1 USA 2010 USA2010
7 0 1 0 1 0 0 0 USA 2013 USA2013
8 1 0 1 0 0 1 0 BLG 2007 BLG2007
9 0 1 0 1 1 0 1 BEL 2009 BEL2009
10 1 0 1 0 0 1 0 BEL 2012 BEL2012",
header = TRUE)

我想尝试:
dfA <- merge(dfA , dfB, on =.(iso, year == year-1), all.x = TRUE, allow.cartesian=FALSE)

但这在当年创造了一场比赛,这不是我想要的。

我也相信 roll会尝试找到最接近的匹配。

我应该如何编写此合并?

期望的输出:
library(data.table)
dfA <- fread("
A B C D E F G Z H I J K year_from_B iso year matchcode
1 0 1 1 1 0 1 0 1 0 1 0 2009 NLD 2010 NLD2010
2 1 0 0 0 1 0 1 NA NA NA NA NA NLD 2014 NLD2014
3 0 0 0 1 1 0 0 NA NA NA NA NA AUS 2010 AUS2010
4 1 0 1 0 0 1 0 NA NA NA NA NA AUS 2006 AUS2006
5 0 1 0 1 0 1 1 NA NA NA NA NA USA 2008 USA2008
6 0 0 1 0 0 0 1 NA NA NA NA NA USA 2010 USA2010
7 0 1 0 1 0 0 0 NA NA NA NA NA USA 2012 USA2012
8 1 0 1 0 0 1 0 0 0 1 0 2007 BLG 2008 BLG2008
9 0 1 0 1 1 0 1 NA NA NA NA NA BEL 2008 BEL2008
10 1 0 1 0 0 1 0 1 1 0 1 2009 BEL 2010 BEL2010
11 0 1 1 1 0 1 0 1 0 1 0 2009 NLD 2010 NLD2010
12 1 0 0 0 1 0 1 NA NA NA NA NA NLD 2014 NLD2014
13 0 0 0 1 1 0 0 NA NA NA NA NA AUS 2010 AUS2010
14 1 0 1 0 0 1 0 NA NA NA NA NA AUS 2006 AUS2006
15 0 1 0 1 0 1 1 NA NA NA NA NA USA 2008 USA2008
16 0 0 1 0 0 0 1 NA NA NA NA NA USA 2010 USA2010
17 0 1 0 1 0 0 0 NA NA NA NA NA USA 2012 USA2012
18 1 0 1 0 0 1 0 0 0 1 0 2007 BLG 2008 BLG2008
19 0 1 0 1 1 0 1 NA NA NA NA NA BEL 2008 BEL2008
20 1 0 1 0 0 1 0 1 1 0 1 2009 BEL 2010 BEL2010",
header = TRUE)

最佳答案

这有点乱,但请尝试:

dfB[dfA[,c(.SD,.(year1=year-1))],
on=.(A,B,C,D,iso,year == year1)]
A B C D H I J K iso year matchcode E F G Z i.year i.matchcode
1: 1 0 1 1 1 0 1 0 NLD 2009 NLD2009 1 0 1 0 2010 NLD2010
2: 2 1 0 0 NA NA NA NA NLD 2013 <NA> 0 1 0 1 2014 NLD2014
3: 3 0 0 0 NA NA NA NA AUS 2009 <NA> 1 1 0 0 2010 AUS2010
4: 4 1 0 1 NA NA NA NA AUS 2005 <NA> 0 0 1 0 2006 AUS2006
5: 5 0 1 0 1 0 1 1 USA 2007 USA2007 1 0 1 1 2008 USA2008
6: 6 0 0 1 NA NA NA NA USA 2009 <NA> 0 0 0 1 2010 USA2010
7: 7 0 1 0 NA NA NA NA USA 2011 <NA> 1 0 0 0 2012 USA2012
8: 8 1 0 1 0 0 1 0 BLG 2007 BLG2007 0 0 1 0 2008 BLG2008
9: 9 0 1 0 NA NA NA NA BEL 2007 <NA> 1 1 0 1 2008 BEL2008
10: 10 1 0 1 NA NA NA NA BEL 2009 <NA> 0 0 1 0 2010 BEL2010
11: 11 0 1 1 NA NA NA NA NLD 2009 <NA> 1 0 1 0 2010 NLD2010
12: 12 1 0 0 NA NA NA NA NLD 2013 <NA> 0 1 0 1 2014 NLD2014
13: 13 0 0 0 NA NA NA NA AUS 2009 <NA> 1 1 0 0 2010 AUS2010
14: 14 1 0 1 NA NA NA NA AUS 2005 <NA> 0 0 1 0 2006 AUS2006
15: 15 0 1 0 NA NA NA NA USA 2007 <NA> 1 0 1 1 2008 USA2008
16: 16 0 0 1 NA NA NA NA USA 2009 <NA> 0 0 0 1 2010 USA2010
17: 17 0 1 0 NA NA NA NA USA 2011 <NA> 1 0 0 0 2012 USA2012
18: 18 1 0 1 NA NA NA NA BLG 2007 <NA> 0 0 1 0 2008 BLG2008
19: 19 0 1 0 NA NA NA NA BEL 2007 <NA> 1 1 0 1 2008 BEL2008
20: 20 1 0 1 NA NA NA NA BEL 2009 <NA> 0 0 1 0 2010 BEL2010

关于r - 按列合并这些列的不同值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59029427/

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