简体   繁体   English

用R中的向量替换data.frame的某些值

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

I'm here again with a new R problem. 我再次遇到新的R问题。

Basically I am inside a for loop, and I've troubles for the very last lines of the code. 基本上,我在for循环中,并且在代码的最后几行遇到麻烦。

I've 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 . 是的,数据集是关于足球的,您可以从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 <- c(HomeTeam, AwayTeam, Val1, Val2, Val3, Val4)

As i said before, I am inside a for loop. 如我之前所说,我处于for循环中。

I'd like that at each cycle, the computer finds the row in wich 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. 我想在每个周期中,计算机找到HomeTeam和AwayTeam中与C的前两个值匹配的行,然后将C的其余值(indxes1,indexs2,indexs3,indexs4)放在最后4列中。

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. 显然,在整个数据帧中,向量c中只有HomeTeam和AwayTeam的“组合”。 At Each iteration of the cycle I change HomeTeam and AwayTeam, and the values. 在循环的每个迭代中,我更改HomeTeam和AwayTeam以及其值。

I would like to do a sort of Join but I've really no Idea of what to do. 我想进行某种加入,但是我真的不知道该怎么做。 What causes me problem is the "check" of the HomeTeam and of the Away team. 导致我出现问题的是HomeTeam和客队的“检查”。 Thanks in advice for the replys! 感谢您的建议!

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 然后我提取了Mydb中的行,其中有我要分析的匹配项

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM