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

View source: R/runningCount.R

runningCountR Documentation

running count of occurences for each unique value in a vector

Description

running count of occurences for each unique value in a vector

Usage

runningCount(x)

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


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!")

MotusWTS/motusServer documentation built on Aug. 8, 2024, 10:23 p.m.