Hi there, is there a possibility to count the number of appearances of an element in a vector ? i mean of any given element.. deliver all elements which are exactly xtimes in this vector ? thx in advance !!
Hello Bunny/Lautioscrew, sum(your vector == your chosen element) should do what you want... HTH, Ranjan On Thu, 1 Mar 2007 15:20:19 +0100 "bunny , lautloscrew.com" <bunny at lautloscrew.com> wrote:> Hi there, > > is there a possibility to count the number of appearances of an > element in a vector ? > i mean of any given element.. deliver all elements which are exactly > xtimes in this vector ? > > thx in advance !! > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code. >
?table
Best,
Dimitris
----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
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: "bunny , lautloscrew.com" <bunny at lautloscrew.com>
To: <r-help at stat.math.ethz.ch>
Sent: Thursday, March 01, 2007 3:20 PM
Subject: [R] count the # of appearances...
> Hi there,
>
> is there a possibility to count the number of appearances of an
> element in a vector ?
> i mean of any given element.. deliver all elements which are exactly
> xtimes in this vector ?
>
> thx in advance !!
>
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.
>
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Dear Bunny, ? table might be what you wish. Best wishes Wolfgang Huber EBI bunny , lautloscrew.com wrote:> Hi there, > > is there a possibility to count the number of appearances of an > element in a vector ? > i mean of any given element.. deliver all elements which are exactly > xtimes in this vector ? > > thx in advance !!
Take a look at "table".
Hope this helps,
Matt
-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of bunny ,
lautloscrew.com
Sent: Thursday, March 01, 2007 9:20 AM
To: r-help at stat.math.ethz.ch
Subject: [R] count the # of appearances... [Broadcast]
Hi there,
is there a possibility to count the number of appearances of an
element in a vector ?
i mean of any given element.. deliver all elements which are exactly
xtimes in this vector ?
thx in advance !!
______________________________________________
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
and provide commented, minimal, self-contained, reproducible code.
------------------------------------------------------------------------------
Notice: This e-mail message, together with any attachments,...{{dropped}}
On Thu, 2007-03-01 at 15:20 +0100, bunny , lautloscrew.com wrote:> Hi there, > > is there a possibility to count the number of appearances of an > element in a vector ? > i mean of any given element.. deliver all elements which are exactly > xtimes in this vector ? > > thx in advance !!Vec <- sample(20, replace = TRUE)> Vec[1] 16 4 16 1 13 13 7 14 20 18 18 18 6 1 8 5 12 8 5 2 # See ?table> table(Vec)Vec 1 2 4 5 6 7 8 12 13 14 16 18 20 2 1 1 2 1 1 2 1 2 1 2 3 1 # Get the elements that appear 3 times Vec.tab <- table(Vec)> Vec.tab[Vec.tab == 3]18 3 HTH, Marc Schwartz
bunny , lautloscrew.com wrote:> Hi there, > > is there a possibility to count the number of appearances of an > element in a vector ? > i mean of any given element.. deliver all elements which are exactly > xtimes in this vector ? > > thx in advance !!> myvec <- c(0, rep(1:6, each = 2), 7, 7, 7)> table(myvec)myvec 0 1 2 3 4 5 6 7 1 2 2 2 2 2 2 3 # Extract elements appearing twice> myvec[myvec %in% names(which(table(myvec) == 2))][1] 1 1 2 2 3 3 4 4 5 5 6 6> ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code.-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
On 01-Mar-07 bunny , lautloscrew.com wrote:> Hi there, > > is there a possibility to count the number of appearances of an > element in a vector ? > i mean of any given element.. deliver all elements which are exactly > xtimes in this vector ? > > thx in advance !!If it is a specific element value which you designate beforehand, then Ranjan Maitra's method sum(your vector == your chosen element) will of course give you the number of times this occurs. However, your query suggests this may not be what you want. First, if you do not designate a specific element, then table() can give you counts of repetitions of all the distinct elements. For example: ## Generate the vector y with repetitions of elements of x:> x<-round(runif(10),digits=3) > y<-sample(x,25,replace=TRUE)## Count the numbers of repetitions> x<-round(runif(10),digits=3) > y<-sample(x,25,replace=TRUE) > table(y)## y ## 0.122 0.372 0.431 0.486 0.523 0.858 0.886 0.948 ## 5 4 5 3 3 2 2 1 Second: You ask for a method to "deliver all elements which are exactly xtimes in this vector". So suppose "xtimes" is a given number of times, and you want to know all elements which each occur xtimes times in the vector y. You could base a method for this on the ouput of table() as above (look at "?table" for the background). counts <- as.data.frame(table(y)) counts ## y Freq ## 1 0.122 5 ## 2 0.372 4 ## 3 0.431 5 ## 4 0.486 3 ## 5 0.523 3 ## 6 0.858 2 ## 7 0.886 2 ## 8 0.948 1 so, if your "xtimes" is say 3, then counts$y[counts$Freq==3] ## [1] 0.486 0.523 ## Levels: 0.122 0.372 0.431 0.486 0.523 0.858 0.886 0.948 showing that elements "0.486" and "0.523" occurred 3 times each in the vector y. Hoping this helps, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <ted.harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 01-Mar-07 Time: 15:11:09 ------------------------------ XFMail ------------------------------