Dear All, I have a vector x = (1,1,1,2,2,2,3,3,3,3) I am looking for a function to return a vector containing the distinct elements of x i,e y = (1,2,3) The following code gives the desired results: as.numeric(levels(as.factor(x))) Is there any other elegant way? Thanks, B --------------------------------- [[alternative HTML version deleted]]
x = (1,1,1,2,2,2,3,3,3,3) unique(x) Best, Matthias> Dear All, > > I have a vector x = (1,1,1,2,2,2,3,3,3,3) > I am looking for a function to return a vector containing > the distinct elements of x i,e y = (1,2,3) > > The following code gives the desired results: > > as.numeric(levels(as.factor(x))) > > Is there any other elegant way? > > Thanks, > > B > > > > > --------------------------------- > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read > the posting guide! http://www.R-project.org/posting-guide.html >
look at ?unique(), e.g.,
x <- c(1, 1, 1, 2, 2, 2, 3, 3, 3, 3)
unique(x)
I hope it helps.
Best,
Dimitris
----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm
----- Original Message -----
From: "Marc Bernard" <bernarduse1 at yahoo.fr>
To: <r-help at stat.math.ethz.ch>
Sent: Friday, September 16, 2005 4:38 PM
Subject: [R] Replicate
> Dear All,
>
> I have a vector x = (1,1,1,2,2,2,3,3,3,3)
> I am looking for a function to return a vector containing the
> distinct elements of x i,e y = (1,2,3)
>
> The following code gives the desired results:
>
> as.numeric(levels(as.factor(x)))
>
> Is there any other elegant way?
>
> Thanks,
>
> B
>
>
>
>
> ---------------------------------
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Hi Marc,> x = c(1,1,1,2,2,2,3,3,3,3) > unique(x)[1] 1 2 3>Being a database guy myself, it took me a while to think "unique" rather than "distinct" -- HTH, Jim Porzak Loyalty Matrix Inc. San Francisco, CA On 9/16/05, Marc Bernard <bernarduse1 at yahoo.fr> wrote:> Dear All, > > I have a vector x = (1,1,1,2,2,2,3,3,3,3) > I am looking for a function to return a vector containing the distinct elements of x i,e y = (1,2,3) > > The following code gives the desired results: > > as.numeric(levels(as.factor(x))) > > Is there any other elegant way? > > Thanks, > > B > > > > > --------------------------------- > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >