s-news
[Top] [All Lists]

Re: [S] frequency of data points

To: s-news@wubios.wustl.edu, KINLEY_ROBERT@lilly.com
Subject: Re: [S] frequency of data points
From: Prof Brian Ripley <ripley@stats.ox.ac.uk>
Date: Mon, 26 Apr 1999 17:49:24 +0100 (BST)
Cc: kin_cheung@alkermes.com
Reply-to: Prof Brian Ripley <ripley@stats.ox.ac.uk>
Sender: owner-s-news@wubios.wustl.edu
Or just

x <- c(9,1,2,4,5,9,1,2,6,2,7)
y <- c(9,1,2,3,5,9,1,2,4,2,8)
f <- factor(paste(x, y, sep=","))
cbind(x, y, z=as.vector(table(f)[f]))


> Date: Mon, 26 Apr 1999 16:16:51 +0100
> From: Robert Kinley <KINLEY_ROBERT@lilly.com>
> 
> 
> Try this -

[ ... ]
> :>
> :> f<-paste(x,y);
> :>
> :> f<-factor(f,levels=unique(f));
> :>
> :> nocc<-rle(sort(f))
> :> ans.col<-nocc$lengths[f]
> :> ans.col
>  [1] 2 2 3 1 1 2 2 3 1 3 1
> :> cbind(xy,ans.col)
>       x y ans.col
>  [1,] 9 9       2
>  [2,] 1 1       2
>  [3,] 2 2       3
>  [4,] 4 3       1
>  [5,] 5 5       1
>  [6,] 9 9       2
>  [7,] 1 1       2
>  [8,] 2 2       3
>  [9,] 6 4       1
> [10,] 2 2       3
> [11,] 7 8       1
 
> 
> 
> Kin Cheung <kin_cheung@alkermes.com> on 26/04/99 15:11:50

>      I need to create vector in a data frame indicating the
> number of frequency data points. For example,
> 
> x_c(9,1,2,4,5,9,1,2,6,2,7)
> y_c(9,1,2,3,5,9,1,2,4,2,8)
> xy_cbind(x,y)
> 
> > xy
>       x y
>  [1,] 9 9
>  [2,] 1 1
>  [3,] 2 2
>  [4,] 4 3
>  [5,] 5 5
>  [6,] 9 9
>  [7,] 1 1
>  [8,] 2 2
>  [9,] 6 4
> [10,] 2 2
> [11,] 7 8
> 
> 
> The output that I need would be
> 
> > xy
>       x y z
>  [1,] 9 9 2
>  [2,] 1 1 2
>  [3,] 2 2 3
>  [4,] 4 3 1
>  [5,] 5 5 1
>  [6,] 9 9 2
>  [7,] 1 1 2
>  [8,] 2 2 3
>  [9,] 6 4 1
> [10,] 2 2 3
> [11,] 7 8 1
> 
> The new vector Z is the number of occurrence of the data points X and Y.
> 
> Any help from anyone would be great appreciated.
> 

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

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

<Prev in Thread] Current Thread [Next in Thread>