0

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.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
ianux22
  • 405
  • 4
  • 16
  • 6
    `c` is a function name. Better not to assign function names to object names – akrun Dec 07 '18 at 17:15
  • 7
    Looks like you're just trying to do a join, but without an (in-question) example input and desired output it's hard to say exactly what you need. – IceCreamToucan Dec 07 '18 at 17:30
  • 3
    What's the for loop? How do you create the vector? – iod Dec 07 '18 at 17:33
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Include a `dput()` in the question -- don't make us go elsewhere to download data. That only makes things more difficult to help you. – MrFlick Dec 07 '18 at 17:33
  • I just Edited my question in order to be more clear – ianux22 Dec 07 '18 at 18:40

1 Answers1

0

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
ianux22
  • 405
  • 4
  • 16