如何像:
dat <- data.frame(match = c(1:8), team1 = c("A","B","C","D","E","F","A","D"), team2 = c("D","E","F","C","B","A","D","A"), winningTeam = c("A","E","C","C","B","A","D","A"))
match team1 team2 winningTeam
1 1 A D A
2 2 B E E
3 3 C F C
4 4 D C C
5 5 E B B
6 6 F A A
7 7 A D D
8 8 D A A
Allteams <- c("A","B","C","D","E","F")
# A vectorized function for you to use to do as you ask:
teamX_form_lastY <- function(teams, games, dat){
sapply(teams, function(x) {
games_info <- rowSums(dat[,c("team1","team2")] == x) + (dat[,"winningTeam"] == x)
lookup <- ifelse(rev(games_info[games_info != 0])==2,1,0)
games_won <- sum(lookup[1:games])
if(length(lookup) < games) warning(paste("maximum games for team",x,"should be",length(lookup)))
games_won/games
})
}
teamX_form_lastY("A", 4, dat)
A
0.75
# Has a warning for the number of games you should be using
teamX_form_lastY("A", 5, dat)
A
NA
Warning message:
In FUN(X[[i]], ...) : maximum games for team A should be 4
# vectorized input
teamX_form_lastY(teams = c("A","B"), games = 2, dat = dat)
A B
0.5 0.5
# so you ca do all teams
teamX_form_lastY(teams = Allteams, 2, dat)
A B C D E F
0.5 0.5 1.0 0.5 0.5 0.0
嗨。感謝您回覆並回答。我今天在想,這種結構的某些東西可以發揮最佳效果。我嘗試了上述方法,它幾乎可行,但在我的場景中,我想要獲得除當前比賽之外的最後三場比賽的結果 - 我認爲上述內容將包括在內?此外,爲什麼上面不會創建一個團隊發生的前兩次NAs(因爲沒有足夠的數據來計算最後三種形式)。再次感謝! –
嗨,傑克。以上應該排除當前的遊戲 - 也就是'dat [1:(i-1)]'這個詞。 'tail'將給出data.frame(或向量等)的最後部分,直到指定的元素數量。現在你提到它,如果前三場比賽少於三場,那麼除數就不應該是三! - 以上修改。 –