将 daply 与多变量函数一起使用

Using daply with multi-variable function

我还是 R 的新手,想使用 *ply 函数从数据帧中提取信息。示例输入数据框如下所示:

# 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

我想做的是提取 'MyTeam' 的对手和获胜者。得分始终以主队得分与客队得分的形式给出。我有一个提取对手是谁的方法是这样的:

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

但我一直在努力争取每场比赛的胜利者。我尝试使用 daply() 和这样的命名函数来执行此操作:

# 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) )

但是,这显然行不通。我是否错误地应用了 daply() 方法?我认为我仍然不确定 *ply 函数的实际行为。看起来 *ply 函数是解决问题的方法,但如果还有其他解决方案,我会洗耳恭听。任何帮助是极大的赞赏!

您的逻辑可以使用嵌套实现 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"