I'm trying to identify if MATLAB or R has a function that resembles the following.
Say I have an input vector v
.
v = [1, 3, 1, 2, 4, 2, 1, 3]
I want to generate a vector, w
of equivalent length to v
. Each element w[i]
should tell me the following: for the corresponding value v[i]
, how many times has this value been encountered so far in v
, i.e. in all elements of v
up to, but not including, position i
. In this example
w = [0, 0, 1, 0, 0, 1, 2, 1]
I'm really looking to see if any statistical or domain-specific languages have a function/instruction like this and what it might be called.
dplyr
like this:library(dplyr); data.frame(v) %>% group_by(v) %>% mutate(count = row_number()-1)
(the result would be a data.frame but you could easily extract the count column if you need it separate). – Acceptable