Ravi.Vishnu@outokumpu.com
2005-May-20 14:48 UTC
[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 theseunique 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]]
Ravi Varadhan
2005-May-20 15:01 UTC
[R] getting the unique values and counts from a vector
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 at jhmi.edu --------------------------------------------------------------------------> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch [mailto:r-help- > bounces at stat.math.ethz.ch] On Behalf Of Ravi.Vishnu at outokumpu.com > Sent: Friday, May 20, 2005 10:48 AM > To: r-help at 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 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
Dimitris Rizopoulos
2005-May-20 15:14 UTC
[R] getting the unique values and counts from a vector
you could use table(), i.e., x <- c(2, 1, 2, 1, 4, 2, 1, 4, 1, 1) ####### tab <- table(x) xu <- as.numeric(names(tab)) xn <- as.vector(tab) xu; xn 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.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: <Ravi.Vishnu at outokumpu.com> To: <r-help at stat.math.ethz.ch> Sent: Friday, May 20, 2005 4:48 PM 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 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 >
Wladimir Eremeev
2005-May-20 15:57 UTC
[R] getting the unique values and counts from a vector
Dear Ravi,>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)try> hist(x,plot=FALSE,breask=unique(x))$counts[1] 5 3 0 0 0 2 -- Best regards Wladimir Eremeev mailto:wl at eimb.ru =========================================================================Research Scientist, PhD Leninsky Prospect 33, Space Monitoring & Ecoinformation Systems Sector, Moscow, Russia, 119071, Institute of Ecology, Phone: (095) 135-9972; Russian Academy of Sciences Fax: (095) 135-9972
Wladimir Eremeev
2005-May-20 16:01 UTC
[R] getting the unique values and counts from a vector
Dear Ravi, try> hist(x,plot=FALSE,breask=unique(x))$counts[1] 5 3 0 0 0 2 Sorry, there is a typo above. However, it works because of it. Correct is> hist(x,plot=FALSE)$counts[1] 5 3 0 0 0 2 another variant> hist(x,plot=FALSE,breaks=c(unique(x)-1,max(unique(x))))$counts[1] 5 3 2 -- Best regards Wladimir Eremeev mailto:wl at eimb.ru =========================================================================Research Scientist, PhD Leninsky Prospect 33, Space Monitoring & Ecoinformation Systems Sector, Moscow, Russia, 119071, Institute of Ecology, Phone: (095) 135-9972; Russian Academy of Sciences Fax: (095) 135-9972
?table On Fri, 20 May 2005, Wladimir Eremeev wrote:> Dear Ravi, > >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) > > try > > hist(x,plot=FALSE,breask=unique(x))$counts > [1] 5 3 0 0 0 2 > > > -- > Best regards > Wladimir Eremeev mailto:wl at eimb.ru > > =========================================================================> Research Scientist, PhD Leninsky Prospect 33, > Space Monitoring & Ecoinformation Systems Sector, Moscow, Russia, 119071, > Institute of Ecology, Phone: (095) 135-9972; > Russian Academy of Sciences Fax: (095) 135-9972 > > ______________________________________________ > 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 >