Cumulative count in R

核能气质少年 提交于 2019-11-27 04:58:57

The ave function computes a function by group.

> id <- c(1,2,3,2,2,1,2,3)
> data.frame(id,count=ave(id==id, id, FUN=cumsum))
  id count
1  1     1
2  2     1
3  3     1
4  2     2
5  2     3
6  1     2
7  2     4
8  3     2

I use id==id to create a vector of all TRUE values, which get converted to numeric when passed to cumsum. You could replace id==id with rep(1,length(id)).

Here is a way to get the counts:

id <- c(1,2,3,2,2,1,2,3)

sapply(1:length(id),function(i)sum(id[i]==id[1:i]))

Which gives you:

[1] 1 1 1 2 3 2 4 2

The dataframe I had was too large and the accepted answer kept crashing. This worked for me:

library(plyr)
df$ones <- 1
df <- ddply(df, .(id), transform, cumulative_count = cumsum(ones))
df$ones <- NULL 

Function to get the cumulative count of any array, including a non-numeric array:

cumcount <- function(x){
  cumcount <- numeric(length(x))
  names(cumcount) <- x

  for(i in 1:length(x)){
    cumcount[i] <- sum(x[1:i]==x[i])
  }

  return(cumcount)
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!