Re: [R] counts of elements in vector

2010-03-29 Thread yehengxin
Try using aggregate( ) -- View this message in context: http://n4.nabble.com/counts-of-elements-in-vector-tp1704036p1710369.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailma

Re: [R] counts of elements in vector

2010-03-29 Thread David Winsemius
On Mar 29, 2010, at 6:52 PM, Ali Tofigh wrote: Assume you have a vector of characters x: x [1] "a" "b" "a" "d" "d" "c" I use a function that counts the number of times each string occurs in x: sapply(unique(x), function(s) {sum(x == s)}) a b d c 2 1 2 1 Is there a more efficient way

[R] counts of elements in vector

2010-03-29 Thread Ali Tofigh
Assume you have a vector of characters x: > x [1] "a" "b" "a" "d" "d" "c" I use a function that counts the number of times each string occurs in x: > sapply(unique(x), function(s) {sum(x == s)}) a b d c 2 1 2 1 Is there a more efficient way of doing this? Cheers, /Ali