【问题标题】:Combining split() and cumsum()结合 split() 和 cumsum()
【发布时间】:2011-11-16 13:43:32
【问题描述】:

我正在尝试生成特定足球运动员按赛季累积进球的统计数据。我已经使用 cut 函数从比赛日期中获取赛季。我有对应于这个数据框的数据

df.raw <-
    data.frame(Game = 1:20,
            Goals=c(1,0,0,2,1,0,3,2,0,0,0,1,0,4,1,2,0,0,0,3),     
               season = gl(4,5,labels = c("2001", "2002","2003", "2004")))

在现实生活中,每个赛季的比赛场数可能不是恒定的

我希望得到如下所示的数据

df.seasoned <-
    data.frame(Game = 1:20,seasonGame= rep(1:5),
            Goals=c(1,0,0,2,1,0,3,2,0,0,0,1,0,4,1,2,0,0,0,3),
            cumGoals = c(1,1,1,3,4,0,3,5,5,5,0,1,1,5,6,2,2,2,2,5),     
               season = gl(4,5,labels = c("2001", "2002","2003", "2004")))

一年内累计进球数和赛季场次

【问题讨论】:

    标签: r plyr data.table


    【解决方案1】:

    这是一个使用data.table 的解决方案,速度非常快。

    library(data.table)
    df.raw.tab = data.table(df.raw)
    df.raw.tab[,list(seasonGame = 1:NROW(Goals), cumGoals = cumsum(Goals)),'season']
    

    【讨论】:

    • 感谢您的回答。如果我有大数据框,我可能会比较时间
    【解决方案2】:
     df.raw$cumGoals <- with(df.raw,  ave(Goals, season, FUN=cumsum) )
     df.raw$seasonGame <- with(df.raw,  ave(Game, season, FUN=seq))
     df.raw
    

    或者用transform ...原来的transform,即:

    df.seas <- transform(df.raw, seasonGame = ave(Game, season, FUN=seq),
                              cumGoals = ave(Goals, season, FUN=cumsum) )
    df.seas
       Game Goals season seasonGame cumGoals
    1     1     1   2001          1        1
    2     2     0   2001          2        1
    3     3     0   2001          3        1
    4     4     2   2001          4        3
    5     5     1   2001          5        4
    6     6     0   2002          1        0
    7     7     3   2002          2        3
    8     8     2   2002          3        5
    9     9     0   2002          4        5
    10   10     0   2002          5        5
    snipped
    

    【讨论】:

    • 谢谢。我实际上发现这更容易关注 +1
    【解决方案3】:

    ddplytransform 的另一份工作(来自 plyr 包):

    ddply(df.raw,.(season),transform,seasonGame = 1:NROW(piece),
                                     cumGoals = cumsum(Goals))
       Game Goals season seasonGame cumGoals
    1     1     1   2001          1        1
    2     2     0   2001          2        1
    3     3     0   2001          3        1
    4     4     2   2001          4        3
    5     5     1   2001          5        4
    6     6     0   2002          1        0
    7     7     3   2002          2        3
    8     8     2   2002          3        5
    9     9     0   2002          4        5
    10   10     0   2002          5        5
    11   11     0   2003          1        0
    12   12     1   2003          2        1
    13   13     0   2003          3        1
    14   14     4   2003          4        5
    15   15     1   2003          5        6
    16   16     2   2004          1        2
    17   17     0   2004          2        2
    18   18     0   2004          3        2
    19   19     0   2004          4        2
    20   20     3   2004          5        5
    

    【讨论】:

    • 仅供参考 transform 是基础 R 的一部分,summarizeplyr 的一部分
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-03
    • 1970-01-01
    • 2013-03-23
    • 1970-01-01
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多