# [R] substituting elements in vector according tosample(unique(vector))

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Wed Aug 29 09:20:35 CEST 2007

```try the following:

v <- c(1, 2, 1, 2, 3, 3, 1)
x <- c(3, 2, 1)

fv <- factor(v, levels = x)
as.vector(unclass(fv))

I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm

----- Original Message -----
From: "Arthur Wuster" <awuster at mrc-lmb.cam.ac.uk>
To: <r-help at stat.math.ethz.ch>
Sent: Tuesday, August 28, 2007 4:12 PM
Subject: [R] substituting elements in vector according
tosample(unique(vector))

>
> Hello!
>
> Assuming I have a vector, such as
>
> v <- c(1,2,1,2,3,3,1)
>
> This vector has three unique elements: 1, 2, and 3.
>
>> unique(v)
> [1] 1 2 3
>
> If I shuffle this vector of unique elements, I get something like
> this:
>
>> sample(unique(v))
> [1] 3 2 1
>
> In the vector v I started with, I would now like to replace each
> element in
> unique(v) with the corresponding element (i.e. the element with the
> same
> index) in sample(unique(v)).
>
> In this case, the result should be something like c(3,2,3,2,1,1,3).
>
> In particular, I would like to do this without a slow for loop.
> I have tried things like sub(), but they don't seem to be
> appropriate.
> Can anyone provide an elegant solution?
>
> Thank you,
>
> Arthur Wuster
> Theoretical and Computational Biology
> MRC Laboratory of Molecular Biology
> Cambridge, UK
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help