Re: [S] FUNCTION replicated()

Jens Oehlschlaegel (oehl@Psyres-Stuttgart.DE)
Fri, 29 May 1998 15:36:42 +0200 (MET DST)


John, if you want to handle NA's like duplicated() and rle():

> x
[1] "cat" "dog" "mouse" "cat" "NA" "NA"
> duplicated(x)
[1] F F F T F T

> rle(x)
$lengths:
[1] 1 1 1 1 2

$values:
[1] "cat" "dog" "mouse" "cat" "NA"

you could change:

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, exclude = NULL)
# ^^^^^^^^^^^^^^^^
return(tabulate(as.integer(x1))[x1])
}

Best regards

Jens Oehlschlaegel

On Fri, 29 May 1998, John Thaden wrote:

> 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
>

--
Jens Oehlschlaegel-Akiyoshi
Psychologist/Statistician
Project TR-EAT + COST Action B6
                                                 F.rankfurt
oehl@psyres-stuttgart.de                         A.ttention
+49 711 6781-408 (phone)                         I.nventory
+49 711 6876902  (fax)                           R .-----.
                                                  / ----- \
Center for Psychotherapy Research                | | 0 0 | |
Christian-Belser-Strasse 79a                     | |  ?  | |
D-70597 Stuttgart Germany                         \ ----- /
-------------------------------------------------- '-----' -
(general disclaimer)                             it's better

----------------------------------------------------------------------- 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