3

I have the following index vector:

TestVec = rep(c(6,8,9,11,18), each = 10)

This reads c(6, 6, ..., 6, 8, 8, ..., 8, 9, 9, ..., 9, ...).

I would like to convert this vector into c(1, 1, ..., 1, 2, 2, ..., 2, 3, 3, ..., 3, ...)


Try

I have improvised a quick-and-dirty method, as follows:

sapply(TestVec, function(x) {which(x == unique(TestVec))})

This works fine, but this takes a lot of time in a large dataset.

Is there any efficient way to improve?

Universal Link
  • 307
  • 1
  • 14
moreblue
  • 322
  • 1
  • 4
  • 16

4 Answers4

1
match(TestVec, unique(TestVec))
Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248
1

Another option:

as.numeric(as.factor(TestVec))
# [1] 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5
Julius Vainora
  • 47,421
  • 9
  • 90
  • 102
1

Requiring data.table:

rleid(TestVec)
tmfmnk
  • 38,881
  • 4
  • 47
  • 67
1

Here is another one,

c(1, cumsum(diff(TestVec) != 0)) + 1
Sotos
  • 51,121
  • 6
  • 32
  • 66