我有两个长度相等的data.frames。一个data.frame包含变量WEEK,Winner,Loser,homeScore,awayScore。
'data.frame': 256 obs. of 5 variables:
$ Week : int 313 313 313 313 313 313 313 313 313 313 ...
$ Winner : Factor w/ 32 levels "ARI","ATL","BAL",..: 1 2 4 5 7 10 11 13 17 18 ...
$ Loser : Factor w/ 32 levels "ARI","ATL","BAL",..: 26 20 6 30 3 14 21 32 19 29 ...
$ homeScore: Factor w/ 41 levels "","12","13","14",..: 7 25 12 9 12 20 23 6 21 22 ...
$ awayScore: Factor w/ 32 levels "","0","10","11",..: 10 26 13 7 9 17 7 30 13 30 ...
>
另一个有WEEK,Favorite,Underdog,Spread。
'data.frame': 256 obs. of 4 variables:
$ Week : int 313 313 313 313 313 313 313 313 313 313 ...
$ Favorite: chr "SEA" "NO" "STL" "PIT" ...
$ Underdog: Factor w/ 32 levels "ARI","ATL","BAL",..: 12 2 18 8 15 23 7 4 32 31 ...
$ Spread : chr "-5" "-3" "-3" "-6" ...
我需要将这两个data.frames组合成一个带有WEEK,Favorite,Underdog,homeScore,awayScore,Spread的data.frame。
我正在努力解决这个匹配问题,因为收藏夹可能不是其他data.frame的赢家。所以我需要经历每周匹配收藏夹与赢家或失败者并将点差放在正确的位置行。我是R的新手,所以这应该很容易做到,但它超出了我的技能。谢谢你的帮助
..好的,这是data.frames的样子
head(df1)
Week Winner Loser homeScore awayScore
313 ARI SD 18 17
313 ATL NO 37 34
313 BUF CHI 23 20
313 CAR TB 20 14
313 CIN BAL 23 16
313 DEN IND 31 24
313 DET NYG 35 14
313 HOU WAS 17 6
313 MIA NE 33 20
313 MIN STL 34 6
head(df2)
Week Favorite Underdog Spread
313 SEA GB -5
313 NO ATL -3
313 STL MIN -3
313 PIT CLE -6
313 PHI JAX -10
313 NYJ OAK -6.5
313 BAL CIN -1
313 CHI BUF -7
313 HOU WAS -3
313 KC TEN -3
答案 0 :(得分:0)
您需要cbind()。
如果你的第一个数据帧是A而第二个是B,那么首先选择每个数据的常用WEEK:
subset.A <- subset(A, Week == 313)
subset.B <- subset(B, Week == 313)
然后结合它们:
resultDF <- cbind(A$Week, B$Favorite, B$Underdog, A$homeScore, A$awayScore, B$Spread)
答案 1 :(得分:0)
我不知道我是否理解正确,但几天前我有点问同样的问题。对我来说最好的答案是:
merge(DFa, DFb, by.x='columnName_a_2Match', by.y='columnName_b_2Match')