RE: [R] getting the unique values and counts from a vector

From: Ravi Varadhan <rvaradha_at_jhsph.edu>
Date: Sat 21 May 2005 - 01:01:27 EST


Hi,
"table" should do it.

> x<-c(2 ,1 ,2, 1, 4 ,2 ,1, 4 ,1 ,1)
> table(x)

x
1 2 4
5 3 2



Ravi Varadhan, Ph.D.
Assistant Professor, The Center on Aging and Health Division of Geriatric Medicine and Gerontology Johns Hopkins University
Ph: (410) 502-2619
Fax: (410) 614-9625
Email: rvaradhan@jhmi.edu

> -----Original Message-----
> From: r-help-bounces@stat.math.ethz.ch [mailto:r-help-
> bounces@stat.math.ethz.ch] On Behalf Of Ravi.Vishnu@outokumpu.com
> Sent: Friday, May 20, 2005 10:48 AM
> To: r-help@stat.math.ethz.ch
> Subject: [R] getting the unique values and counts from a vector
>
> Hi all,
> >From a vector, I want to get the unique values and the counts of these
> unique values in the vector. For example,
> x<-c(2 ,1 ,2, 1, 4 ,2 ,1, 4 ,1 ,1)
> xu<-unique(x)
> xn<-numeric(length(xu))
> for (i in 1:length(xu)) {xn[i]<-length(which(x==xu[i]))}
> There must be a very much simpler method of doing this. Can somebody
> direct me to the functions that I must read in order to do operations of
> these sort.
> Thanks,
> Ravi Vishnu
>
>
> This message is meant for the addressee only and may contain
> confidential and legally privileged information. Any unauthorised
> review, use, copying, storage, disclosure or distribution of this e-
> mail and any attachments is strictly prohibited. If you are not the
> named recipient or have otherwise received this communication in
> error, please destroy this message from your system and kindly notify
> the sender by e-mail. Thank you for your co-operation.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@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


R-help@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 Received on Sat May 21 01:32:28 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:58 EST