0

I have this table (all football games from greek league, where one team won from behind - 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

and I want to create a new column, let's call it tempWinner which has the name of the winner. I am using the following formula, which uses excel's rational and unfortunately fails to give me the correct result. I have searched how to just "copy" a cell using a condition, but I was not able to find anything relevant.

anatropes$tempWinner <- ifelse (anatropes$FTR == "H", anatropes$HomeTeam , anatropes$AwayTeam)

Any idea? What I want to do eventually is count how many times each team has won from behind (either being home or away team). edit: str(anatropes) returns:

'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 ...

There is no error in my method, I just get the following data frame:

> 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
Fierce82
  • 408
  • 5
  • 17

1 Answers1

0

as @Sotos suggested - use as.character() since HomeTeam and AwayTeam are both factors. what you are getting is the id of the level instead of the string value.

Zahiro Mor
  • 1,708
  • 1
  • 16
  • 30