How to count the cumulative number of occurrences of an element in a vector?

4

Suppose I have a vector in R:

x <- c("a", "a", "b", "a", "b", "c")

I need to determine, for each position i of the vector, how many times the x[i] element has appeared so far. The result for the above vector would be

[1] 1 2 1 3 2 1

How to do this calculation efficiently and without using native code (C)? By efficient I mean something that runs in a second or less in a vector of 1 million elements (ex. x <- sample.int(1:99, size=999999, replace=T) )

    
asked by anonymous 14.12.2013 / 09:51

1 answer

2

You can use the ave function as follows:

contagem <- ave(rep(1, length(x)), x, FUN=cumsum)

First, a vector containing only numbers 1, the size of the input vector ( rep(1, length(x)) ), is generated. Within this vector groups are selected according to the value of x , and within these groups the cumulative sum ( cumsum ) is calculated. Since the vector only contains the number 1, the cumulative sum results in the sequence 1, 2, 3, ..., ie a count.

    
14.12.2013 / 09:51