R基于多个条件对值的外观进行顺序计数

R基于多个条件对值的外观进行顺序计数,r,dataframe,R,Dataframe,我有一个连续国际象棋游戏的数据帧: n_game winner winner_id white_id. black_id 1 black id_2 id_1 id_2 2 white id_3 id_3 id_4 3 draw draw id_4 id_1 4 black id_2 id_7 id_2 5 draw draw i

我有一个连续国际象棋游戏的数据帧:

n_game winner  winner_id white_id. black_id  
1      black   id_2      id_1      id_2
2      white   id_3      id_3      id_4
3      draw    draw      id_4      id_1
4      black   id_2      id_7      id_2
5      draw    draw      id_9      id_10
6      white   id_6      id_6      id_45
我想添加几个列,按顺序计算两个玩家的赢、输和平局数量,并用颜色标记,这样我就可以得到每个玩家到目前为止(比赛前)的表现。所以第一排我想要

n_game winner  winner_id white_id. black_id  n_wins_white  n_wins_white_as_black  n_draws_white ...
1      black   id_2      id_1      id_2       0               0                       0
在下一场以IDU 2为玩家的比赛中,我想要的是

n_game winner  winner_id white_id. black_id  n_wins_white  n_wins_white_as_black  n_draws_white ...
4      black   id_2      id_7      id_2        1                0                     0

我正试图以一种“整洁”的方式来做这件事,最好是使用dplyr。我尝试过按winner_id和winner color与黑白id进行分组,并使用seq_-along()获得顺序计数,但例如,每次白色或黑色id出现在winner列中时,我都会得到计数,而不是特定条件。我也尝试过ifelse,但这似乎打破了顺序计数。任何帮助都会很好。

lag
可以让你在每一轮之前数到。您需要将白赢和黑赢计算为单独的操作。您需要将当前组与以前的所有行进行比较,因此必须将整个表传递到
mutate
,并找到
cur\u组的相关行。然后,这将为您提供所有行,因此您只需要使用
cur\u group\u rows
将与此组对应的行子集。如果我们把它抽象成一个单独的函数,看起来会更整洁一些

library(dplyr)
get_history <- function(colour = "white", isdraw = FALSE) {
    colour_win <- colour
    if (isdraw) { colour_win <- "draw" }
    lag(cumsum(
        tab$winner == colour_win & 
        tab[[paste0(colour, "_id")]] == cur_group()[[1L]]), default = 0)[seq_len(nrow(tab)) %in% cur_group_rows()]
}
tab %>%  
    group_by(white_id) %>% 
    mutate(
        n_wins_white_as_white = get_history(),
        n_draws_white_as_white = get_history(isdraw = TRUE),
        n_wins_white_as_black = get_history("black")) %>% 
    group_by(black_id) %>% 
    mutate(
        n_wins_black_as_black = get_history("black"),
        n_draws_black_as_black = get_history("black", isdraw = TRUE),
        n_wins_black_as_white = get_history())
# # A tibble: 8 x 11
# # Groups:   black_id [5]
#   n_game winner winner_id white_id black_id n_wins_white_as_whi… n_draws_white_as_wh… n_wins_white_as_bla… n_wins_black_as_bl… n_draws_black_as_b… n_wins_black_as_wh…
#    <int> <chr>  <chr>     <chr>    <chr>                   <dbl>                <dbl>                <dbl>               <dbl>               <dbl>               <dbl>
# 1      1 black  id_2      id_1     id_2                        0                    0                    0                   0                   0                   0
# 2      2 white  id_3      id_3     id_4                        0                    0                    0                   0                   0                   0
# 3      3 draw   draw      id_4     id_1                        0                    0                    0                   0                   0                   0
# 4      4 black  id_2      id_7     id_2                        0                    0                    0                   1                   0                   0
# 5      5 white  id_3      id_3     id_2                        1                    0                    0                   2                   0                   0
# 6      6 white  id_2      id_2     id_4                        0                    0                    2                   0                   0                   0
# 7      7 black  id_3      id_1     id_3                        0                    0                    0                   0                   0                   2
# 8      8 draw   draw      id_4     id_7                        0                    1                    0                   0                   0                   0
库(dplyr)
获取历史记录%
分组人(黑色id)%>%
变异(
n_赢得_black_as_black=获得历史(“黑色”),
n_将_black绘制为_black=get_history(“black”,isdraw=TRUE),
n\u赢得\u黑\u白=获取\u历史()
##A tibble:8 x 11
##团体:黑人身份[5]
#n_游戏赢家n_id white_id black_id n_赢得白色_as_whi…n_赢得白色_as_bla…n_赢得黑色_bl…n_赢得黑色_as_b…n_赢得黑色_as_whi…
#                                                                                                                
#1 1黑色id_2 id_1 id_2 0 0 0 0 0
#2 2白色id_3 id_3 id_4 0 0 0 0 0
#3 3绘图id_4 id_1 0 0 0 0
#4 4黑色id_2 id_7 id_2 0 0 0 1 0 0 0
#5 5白色id_3 id_3 id_2 1 0 2 0 0
#6 6白色id_2 id_2 id_4 0 0 2 0 0 0 0 0
#7 7黑色id_3 id_1 id_3 0 0 2
#8 8绘图id_4 id_7 0 1 0 0 0 0 0

太好了,我现在明白了。谢谢