创建一个仅显示获胜团队的新列 [英] Create a new column that only shows the winning team

查看:45
本文介绍了创建一个仅显示获胜团队的新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个仅显示获胜团队的新列.

I am trying to create a new column that only shows the winning team.

以下是一些示例数据:

results <- data.frame(
  home_team = c("Scotland", "England", "Scotland", "England", "Scotland", "Scotland",
                "England", "Wales", "Scotland", "Scotland", "England"),
  away_team = c("England", "Scotland", "England", "Scotland", "England", "Wales",
                "Scotland", "Scotland", "England", "Wales", "Wales"),
  home_score = c(0, 4, 2, 2, 3, 4, 1, 0, 7, 9, 2),
  away_score = c(0, 2, 1, 2, 0, 0, 3, 2, 2, 0, 1),
  stringsAsFactors = FALSE
)

这是我目前的代码:

results <- intl.football.results
first6home <- head(results$home_team)
first6away <- head(results$away_team)
homescore <- (results$home_score)
awayscore <- (results$away_score)

data.frame('winning_team' = 0, results)

for (i in 1:length(results)){
  if(homescore[i] > awayscore[i]){
    homewins <- print("home wins")
  }else if(homescore[i] == awayscore[i]){
    draw <- print("draw")
  }else{
    awaywins <- print("away team wins")
  }
}

我想我需要以某种方式纠正 home_team 的主场胜利".我能想到的最好方法是找到homewins"的行号,然后选择 home_team 所在的行.但是如果 data.frame 有 30,000+ 行,我该怎么做?抱歉,这听起来很基本,但我正在努力!

I am thinking that I need to somehow rectify the "homewins" to the home_team. The best way I can think of this is by finding the row number of the "homewins" then selecting the rows that the home_team is in. But how do I do this if the data.frame has 30,000+ rows? Sorry this may sound basic but I'm trying!

谢谢大家的回复,我一定会练习的.最后一件事,如果我想打印获胜国家栏而不是主场、客场或平局"怎么办?

Thank you everyone for the responses, I will definitely practice them. One last thing, what if I wanted to print out the winning country column and not "home, away, or draw"?

推荐答案

dplyr 中的 case_when 函数可能是解决这个问题的好方法.它似乎与您在上面尝试执行的操作非常接近,因此希望它非常直观.

The case_when function in dplyr might be a good way to solve this. It seems pretty close to what you're trying to do above, so hopefully its quite intuitive.

文档和更多示例:https://dplyr.tidyverse.org/reference/case_when.html

我从相应的行中传递获胜团队的名称作为要在 case_when 中采取的操作,但是您可以传递一个字符串,例如主场胜利",就像我在平局中所做的那样,如果这是你想要的结果.

I'm passing the name of the the winning team from the corresponding row as the action to take in the case_when, but you can pass in a character string, e.g. 'Home Win', as I've done for the drawn games, if thats the outcome you want.

library(tidyverse)


d <- tibble(
        home_team = c('Scotland', 'England', 'Scotland', 'England', 
                'Scotland', 'Scotland', 'England', 'Wales'), 
        away_team = c('England', 'Scotland', 'England', 'Scotland', 
                'England', 'Wales', 'Scotland', 'Scotland'), 
        home_score = c(0, 4, 2, 2, 3, 4, 1, 0), 
        away_score = c(0, 2, 1, 2, 0, 0, 3, 2))

d %>% 
        mutate(winner = case_when(
                home_score > away_score ~ home_team, 
                away_score > home_score ~ away_team, 
                away_score == home_score ~ 'Drawn Game'))

这篇关于创建一个仅显示获胜团队的新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆