0

I am using the home_goal and away_goal to make a new variable to see the game result for the game.

This is what I have

 if(data$home_team_goal < data$away_team_goal){
  data$result <- c('H')
}else if (data$home_team_goal > data$away_team_goal){
  data$result<- c('A')
}else (data$home_team_goal ==  data$away_team_goal){
  data$result<- c('D')
}

it giving all the game a draw.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Using `if` in that way doesn't vectorise over the rows. Do you not get a warning about 'only the first element being used'? It's probably taking the results of the first row and just copying that everywhere else. – Jul Sep 29 '17 at 04:05
  • 2
    Possible duplicate of [Nested ifelse statement in R](https://stackoverflow.com/questions/18012222/nested-ifelse-statement-in-r) – Ronak Shah Sep 29 '17 at 04:07

3 Answers3

2

You could do this with a couple of ifelse statements. And I'd use the dplyr package, then you can just refer to the columns by name instead of the verbose $ syntax, and add the new column using mutate. Also, data is not a great name for a data frame, so I'm using data1.

install.packages("dplyr") # if required
library(dplyr)

data1 <- data.frame(home_team_goal = c(0, 0, 2, 3, 3, 1),
                    away_team_goal = c(1, 0, 4, 1, 4, 1))

data1 %>% 
  mutate(result = ifelse(home_team_goal == away_team_goal, "D", 
                  ifelse(home_team_goal >  away_team_goal, "H", "A")))


  home_team_goal away_team_goal result
1              0              1      A
2              0              0      D
3              2              4      A
4              3              1      H
5              3              4      A
6              1              1      D
neilfws
  • 32,751
  • 5
  • 50
  • 63
2

Here is an option with case_when

library(dplyr)
data %>% 
    mutate(result = case_when(home_team_goal == away_team_goal~"D", 
                              home_team_goal >  away_team_goal ~ "H", 
                              TRUE ~ "A"))
#    home_team_goal away_team_goal result
#1              0              1      A
#2              0              0      D
#3              2              4      A
#4              3              1      H
#5              3              4      A
#6              1              1      D
akrun
  • 874,273
  • 37
  • 540
  • 662
1

In this case you're just wanting a label for the difference between the two columns being >0, 0 or <0. It is only one comparison and there is no need for looping over multiple if/else, ifelse or case_when:

factor(with(data1, sign(home_team_goal - away_team_goal)), labels=c("A","D","H"))
#[1] A D A H A D
#Levels: A D H

If you don't want the factor output:

c("A","D","H")[factor(with(data1, sign(home_team_goal - away_team_goal)))]
#[1] "A" "D" "A" "H" "A" "D"
thelatemail
  • 91,185
  • 12
  • 128
  • 188