Cumulative count in R

末鹿安然 提交于 2019-11-26 09:54:47

问题


Is there a way of counting the number of times an object appears in a column cumulatively in R?

e.g. say I have the column:

id  
1  
2  
3  
2  
2  
1  
2  
3

This would become:

id   count  
1     1  
2     1  
3     1  
2     2  
2     3  
1     2  
2     4  
3     2  

etc...

Thanks


回答1:


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)).




回答2:


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



回答3:


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 



回答4:


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)
}


来源:https://stackoverflow.com/questions/10029235/cumulative-count-in-r

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