rfor-loopvectorcbind

Replace some values of a data.frame with a vector in R


I'm here again with a new R problem.

Basically I am inside a for loop, and I've troubles for the very last lines of the code.

I have a dataset like this:

           > head(myDB)
    # A tibble: 6 x 11
      Div   Date  HomeTeam AwayTeam  FTHG  FTAG FTR   Index1     Index2  Index3     Index4
    
1 I1    20/0… Juventus Fiorent…     2     1 H              0          0            0           0
2 I1    20/0… Roma     Udinese      4     0 H              0          0            0           0
3 I1    21/0… Atalanta Lazio        3     4 A              0          0            0           0
4 I1    21/0… Bologna  Crotone      1     0 H              0          0            0           0
5 I1    21/0… Chievo   Inter        2     0 H              0          0            0           0
6 I1    21/0… Empoli   Sampdor…     0     1 A              0          0            0           0

Yes, Dataset is about football and you can download it for free on http://www.football-data.co.uk/italym.php.

However, I created several indexes for each match and I put them inside a vector c

c <- c(HomeTeam, AwayTeam, Val1, Val2, Val3, Val4)

As I said before, I am inside a for loop.

I'd like that at each cycle, the computer finds the row in which HomeTeam and AwayTeam match with the first two values of C, and then puts the remaining values of C (indxes1, indexes2, indexes3, indexes4) in the last 4 columns.

EDIT: Basically I'm looking for an output like this:

> head(myDB)
# A tibble: 6 x 11
  Div   Date  HomeTeam AwayTeam  FTHG  FTAG FTR   Index1     Index2  Index3     Index4

1 I1    20/0… Juventus Fiorent…     2     1 H              0          0            0           0
2 I1    20/0… Roma     Udinese      4     0 H              0          0            0           0
3 I1    21/0… Atalanta Lazio        3     4 A              0          0            0           0
4 I1    21/0… Bologna  Crotone      1     0 H              0          0            0           0
5 I1    21/0… Chievo   Inter        2     0 H              0          0            0           0
6 I1    21/0… Empoli   Sampdor…     0     1 A              Val1          Val2            Val3           Val4

Obviously, there is only a row in the whole dataframe with the "combination" of HomeTeam and AwayTeam in the vector c. At Each iteration of the cycle I change HomeTeam and AwayTeam, and the values.

I would like to do a sort of Join but I really have no idea of what to do. What causes me problem is the "check" of the HomeTeam and of the Away team.


Solution

  • I solved the questin by myself! I post it if there will be someone else needing Help.

    Basically I extracted my team name as values and "stored" them in an object

    teams <- unique(DB_Of_The_Match$home_team_name)
    teams[2] <- unique(DB_Of_The_Match.2$away_team_name)
    

    Then I extracted the row in Mydb in which there's the match I'm analyzing

    row_sub <- which(MYDB$HomeTeam==teams_2[1] & MYDB$AwayTeam==teams_2[2])
    

    Then I just replaced the zero with the indexes i created

    c <- c(Index1, Index2, Index3, Index4)
    
    MyDB[row_sub, 23:30] <- c
    
    > head(myDB)
    # A tibble: 6 x 11
      Div   Date  HomeTeam AwayTeam  FTHG  FTAG FTR       Index1     Index2  Index3     Index4
    
    1 I1    20/0… Juventus Fiorent…     2     1 H              0          0            0           0
    2 I1    20/0… Roma     Udinese      4     0 H              0          0            0           0
    3 I1    21/0… Atalanta Lazio        3     4 A              0          0            0           0
    4 I1    21/0… Bologna  Crotone      1     0 H              0          0            0           0
    5 I1    21/0… Chievo   Inter        2     0 H              0          0            0           0
    6 I1    21/0… Empoli   Sampdor…     0     1 A              Val1        Val2       Val3        Val4