我有这张桌子(所有足球比赛来自希腊联赛,其中一支球队从后面获胜 - ht)
Date HomeTeam AwayTeam FTHG FTAG FTR HTHG HTAG HTR
8 24/08/15 Panetolikos Panathinaikos 1 2 A 1 0 H
16 31/08/15 Platanias Atromitos 1 2 A 1 0 H
40 28/09/15 Veria AEK 1 2 A 1 0 H
42 03/10/15 Panthrakikos Levadeiakos 1 3 A 1 0 H
68 01/11/15 Asteras Tripolis PAOK 2 1 H 0 1 A
97 05/12/15 Asteras Tripolis Iraklis 1 2 A 1 0 H
120 21/12/15 AEK Levadeiakos 1 2 A 1 0 H
138 17/01/16 Asteras Tripolis Kallonis 3 1 H 0 1 A
196 06/03/16 Panthrakikos PAOK 2 1 H 0 1 A
203 13/03/16 Atromitos Asteras Tripolis 2 1 H 0 1 A
233 17/04/16 Asteras Tripolis Veria 2 1 H 0 1 A
我想创建一个新列,让我们称之为tempWinner,其名称为获胜者。我使用以下公式,使用excel的理性,但遗憾的是没有给我正确的结果。我已经搜索了如何只是"复制"使用条件的单元格,但我找不到任何相关的单词。
anatropes$tempWinner <- ifelse (anatropes$FTR == "H", anatropes$HomeTeam , anatropes$AwayTeam)
有什么想法吗?我最终想做的是计算每支球队从后面赢得多少次(无论是主场还是客场球队)。
修改
str(anatropes)
返回:
'data.frame': 11 obs. of 9 variables:
$ Date : Factor w/ 85 levels "","01/11/15",..: 67 84 75 7 2 12 57 41 14 32 ...
$ HomeTeam: Factor w/ 17 levels "","AEK","Asteras Tripolis",..: 11 15 16 13 3 3 2 3 13 4 ...
$ AwayTeam: Factor w/ 17 levels "","AEK","Asteras Tripolis",..: 10 4 2 8 14 6 8 7 14 3 ...
$ FTHG : int 1 1 1 1 2 1 1 3 2 2 ...
$ FTAG : int 2 2 2 3 1 2 2 1 1 1 ...
$ FTR : Factor w/ 4 levels "","A","D","H": 2 2 2 2 4 2 2 4 4 4 ...
$ HTHG : int 1 1 1 1 0 1 1 0 0 0 ...
$ HTAG : int 0 0 0 0 1 0 0 1 1 1 ...
$ HTR : Factor w/ 4 levels "","A","D","H": 4 4 4 4 2 4 4 2 2 2 ...
我的方法没有错误,我只是得到以下数据框:
> anatropes
Date HomeTeam AwayTeam FTHG FTAG FTR HTHG HTAG HTR
8 24/08/15 Panetolikos Panathinaikos 1 2 A 1 0 H
16 31/08/15 Platanias Atromitos 1 2 A 1 0 H
40 28/09/15 Veria AEK 1 2 A 1 0 H
42 03/10/15 Panthrakikos Levadeiakos 1 3 A 1 0 H
68 01/11/15 Asteras Tripolis PAOK 2 1 H 0 1 A
97 05/12/15 Asteras Tripolis Iraklis 1 2 A 1 0 H
120 21/12/15 AEK Levadeiakos 1 2 A 1 0 H
138 17/01/16 Asteras Tripolis Kallonis 3 1 H 0 1 A
196 06/03/16 Panthrakikos PAOK 2 1 H 0 1 A
203 13/03/16 Atromitos Asteras Tripolis 2 1 H 0 1 A
233 17/04/16 Asteras Tripolis Veria 2 1 H 0 1 A
tempWinner
8 10
16 4
40 2
42 8
68 3
97 6
120 8
138 3
196 13
203 4
233 3
答案 0 :(得分:0)
- 使用as.character()
,因为HomeTeam和AwayTeam都是因素。你得到的是级别的id而不是字符串值。