runningCount: running count of occurences for each unique value in a vector

Description Usage Arguments Details Value Note Author(s) Examples

View source: R/runningCount.R

Description

running count of occurences for each unique value in a vector

Usage

1

Arguments

x:

integer vector

Details

For an integer vector, returns a vector of equal length giving the number of times the i'th element occurs in the first i slots.

Value

integer vector rv; rv[i] = sum(x[1:i] == x[i])

Note

: despite the O(log(length(x))) call to order(), this works faster up to at least length(x) == 1E8 than a similar version that first converts to a factor and then orders using method="radix"

Author(s)

John Brzustowski jbrzusto@REMOVE_THIS_PART_fastmail.fm

Examples

1
2
3
runID = ceiling(10 * runif(100))
posInRun = runningCount(runID)
for (i in seq(along=runID)) if (posInRun[i] != sum(runID[1:i] == runID[i])) stop("runningCount failed!")

jbrzusto/motusServer documentation built on May 19, 2019, 8:19 a.m.