[R] count the # of appearances...

Chuck Cleland ccleland at optonline.net
Thu Mar 1 15:43:09 CET 2007


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



More information about the R-help mailing list