r - 用 R 中的向量替换 data.frame 的一些值
问题描述
我又来了一个新的 R 问题。
基本上我在一个 for 循环中,我在代码的最后几行遇到了麻烦。
我有一个这样的数据集:
> 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
是的,Dataset 是关于足球的,您可以在http://www.football-data.co.uk/italym.php上免费下载。
但是,我为每个匹配创建了几个索引,并将它们放在向量 c 中
c <- c(HomeTeam, AwayTeam, Val1, Val2, Val3, Val4)
正如我之前所说,我在一个 for 循环中。
我希望在每个循环中,计算机都会在 HomeTeam 和 AwayTeam 中找到与 C 的前两个值匹配的行,然后将 C 的剩余值(indxes1、indexes2、indexes3、indexes4)放在最后 4 列中.
编辑:基本上我正在寻找这样的输出:
> 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
显然,整个dataframe中只有一行向量c中HomeTeam和AwayTeam的“组合”。在循环的每次迭代中,我都会更改 HomeTeam 和 AwayTeam 以及值。
我想做一种加入,但我真的不知道该怎么做。引起我问题的是主队和客队的“检查”。感谢您的回复建议!
解决方案
我自己解决了这个问题!如果还有其他人需要帮助,我会发布它。
基本上我将我的团队名称提取为值并将它们“存储”在一个对象中
teams <- unique(DB_Of_The_Match$home_team_name)
teams[2] <- unique(DB_Of_The_Match.2$away_team_name)
然后我在 Mydb 中提取了我正在分析的匹配行
row_sub <- which(MYDB$HomeTeam==teams_2[1] & MYDB$AwayTeam==teams_2[2])
然后我用我创建的索引替换了零
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
推荐阅读
- azure - 如何在变量中使用 Azure Arm 模板输出?
- dart - Flutter 2.0:返回类型
- > 不是闭包上下文所要求的“无效”
- wso2is - Wso2 Identity Server 5.8.0:通过Scim服务创建用户中的自定义声明属性
- spring-boot - 覆盖 Grizzly 服务器的线程池属性
- javascript - 如何按嵌套在主数组Mongoose中的数组中对象键的值对数组进行排序
- javascript - 将 base64 下载为 pdf JavaScript 时出现问题
- php - WordPress:通过functions.php从分页中排除帖子类别
- python - 纸浆优化故障排除
- javascript - 在 tabler-react TabbedCard 中包含可视化
- c# - 为什么我不能创建一个大小为 int.MaxValue 的数组?