[S] FUNCTION replicated()

John Thaden (jjthaden@life.uams.edu)
Fri, 29 May 1998 03:25:06 -0500


Here's a little function I find useful. It's a bit like duplicated():

> x <- c("cat", "dog", "mouse", "cat")
> replicated(x)
[1] 2 1 1 2

########################

replicated <- function(x){
# Returns a vector of integers the same length as x,
# the i-th integer being the number of times the value
# x[i] occurs in x. Object x may be a numeric,character,
# or logical vector, matrix or array, but not a list.
# NA's are not presently handled. -John Thaden 5/29/98 #
x1 <- factor(x)
return(tabulate(as.integer(x1))[x1])
}

John Thaden, Ph.D., Research Instructor
Don Reynolds Department of Geriatrics
University of Arkansas for Medical Sciences
Little Rock, Arkansas USA

-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news