Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rescale Vector R

Tags:

r

vector

Suppose I have a vector of integers such as:

> x
[1]  1  1  1  4  4 13 13 14

I am looking for an efficient way in R to rescale the vector to integers 1 to the max of the unique number of elements. Therefore the resulting vector would be:

1 1 1 2 2 3 3 4

Seems like a simple problem but I'm having trouble finding an efficient way to do it. In practice this vector is large (around 500).

like image 274
Glen Avatar asked Dec 12 '22 20:12

Glen


2 Answers

Try match(x, sort(unique(x))):

x <- sample(1:50, 1e6, replace = TRUE)
benchmark(as.integer(factor(x)), match(x, sort(unique(x))),
          replications = 20, columns = c("test", "elapsed", "relative"))
#                        test elapsed relative
# 1     as.integer(factor(x))   18.44    10.36
# 2 match(x, sort(unique(x)))    1.78     1.00

identical(as.integer(factor(x)), match(x, sort(unique(x))))
# [1] TRUE
like image 63
Julius Vainora Avatar answered Jan 04 '23 09:01

Julius Vainora


Try the following command, assuming that v is your vector

rank( v )

see ?rank for more information.

Well, this does give another result, but is correct as ranking, which is what I thought you want to achieve. To get your preferred result, do

as.integer( factor( v ) )
like image 28
January Avatar answered Jan 04 '23 09:01

January