J. Paul
J. Paul

Reputation: 391

Using daply with multi-variable function

I am still new to R and want to use a *ply function to extract information from a dataframe. A sample input dataframe looks like this:

# Construct the dataframe
season <- c("12","12","12","12","12")
hometeam <- c("Team A","MyTeam","MyTeam","Team D","Team E")
awayteam <- c("MyTeam","Team B","Team C","MyTeam","MyTeam")
score <- c("1 - 1","7 - 1","0 - 0","0 - 2","0 - 1")
stats <- data.frame(season,hometeam,awayteam,score)
print(stats)


  season hometeam awayteam score
1  11/12   Team A   MyTeam 1 - 1
2  11/12   MyTeam   Team B 7 - 1
3  11/12   MyTeam   Team C 0 - 0
4  11/12   Team D   MyTeam 0 - 2
5  11/12   Team E   MyTeam 0 - 1

What I want to do is extract both the opponent of 'MyTeam' as well as the winner. The score is always given as the home team's score vs. the away team's score. I have a way of extracting who the opponent is like this:

# Get the opponent to MyTeam; can add this to dataframe later
opponent <- ifelse(stats$hometeam == "MyTeam", stats$awayteam, stats$hometeam)

But I am stuck trying to get the winner of every match. I tried doing this with daply() and a named function like so:

# Separate out scores for home and away team to determine winner
stats <- separate(stats, score, c('homescore','awayscore'), sep=' - ', remove=TRUE)

# Function for use in ply to get the winner of a match
determineWinner <- function(homescore, awayscore, hometeam) {
  homewon <- FALSE
  if ( homescore < awayscore) {
    homewon <- FALSE
  } else if ( homescore > awayscore ) { 
    homewon <- TRUE
  } else {
    return("tie")
  }
  if ( hometeam == "MyTeam" ) { 
    ifelse(homewon, return("won"), return("lost"))
  } else {
    ifelse(homewon, return("lost"), return("won"))
  }
}#end of function

winner <- daply(stats, .(homescore,awayscore,hometeam), determineWinner(stats$homescore, stats$awayscore, stats$hometeam) )

But, this clearly does not work. Am I applying the daply() method incorrectly? I think am still unsure how the *ply functions really behave. It seems like a *ply function is the way to go here, but if there are other solutions out there, I am all ears. Any help is greatly appreciated!

Upvotes: 0

Views: 78

Answers (1)

aichao
aichao

Reputation: 7435

Your logic can be implemented using nested ifelse:

winner <- ifelse(stats$homescore > stats$awayscore,
             ifelse(stats$hometeam == "MyTeam","won","lost"),
             ifelse(stats$homescore < stats$awayscore,
                    ifelse(stats$hometeam == "MyTeam","lost","won"),
                    "tie"))
##[1] "tie" "won" "tie" "won" "won"

Upvotes: 3

Related Questions