首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >组合split()和cumsum()

组合split()和cumsum()
EN

Stack Overflow用户
提问于 2011-09-14 06:06:40
回答 3查看 1.3K关注 0票数 4

我正在尝试生成特定足球运动员按赛季累积进球数的统计数据。我使用了cut函数从比赛日期中获取赛季。我有与此数据帧相对应的数据

代码语言:javascript
运行
复制
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")))

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

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

代码语言:javascript
运行
复制
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")))

将年度内的目标和本赛季的比赛次数累加

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-09-14 06:18:47

ddplytransform的另一个任务(来自plyr包):

代码语言:javascript
运行
复制
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
票数 5
EN

Stack Overflow用户

发布于 2011-09-14 06:26:34

代码语言:javascript
运行
复制
 df.raw$cumGoals <- with(df.raw,  ave(Goals, season, FUN=cumsum) )
 df.raw$seasonGame <- with(df.raw,  ave(Game, season, FUN=seq))
 df.raw

或者通过变换..。原始的变换,也就是:

代码语言:javascript
运行
复制
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
票数 6
EN

Stack Overflow用户

发布于 2011-09-14 07:48:12

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

代码语言:javascript
运行
复制
library(data.table)
df.raw.tab = data.table(df.raw)
df.raw.tab[,list(seasonGame = 1:NROW(Goals), cumGoals = cumsum(Goals)),'season']
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7409138

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档