Create the frequency count from a vector in R

Viewed 2098

Suppose there is a vector with numerical values with possible duplicated values

x <- c(1, 2, 3, 4, 5, 1, 2, 2, 3)

I want to create another vector of counts as follows.

  1. It has the same length as x.
  2. For each unique value in x, the first appearance is 1, the second appearance is 2, and so on.

The new vector I want is

1, 1, 1, 1, 1, 2, 2, 3, 2

I need a fast way of doing this since x can be really long.

1 Answers
Related