[R] Loop removal?

Peter Malewski y0004379 at tu-bs.de
Mon Jul 31 16:04:00 CEST 2000

```On Mon, 31 Jul 2000 rijn at swi.psy.uva.nl wrote:

>   bin.index <- function(data,bins=2) {
>     # Code from Peter Malewski, added the sort command.
>     sort(rep(rep(1:bins,rep(length(data)/bins,bins)),length.out=length(data)))
>   }

with your outside-sort you have avoided a hidden bug in my example:

xx <- function(data,g=2)
rep(rep(1:g,rep(length(data)/g,g)),length.out=length(data))
> x<- 1:7 ;cbind(x,xx(x ,2))
x
[1,] 1 1
[2,] 2 1
[3,] 3 1
[4,] 4 2
[5,] 5 2
[6,] 6 2
[7,] 7 1   ## !!!

is strange, but...

> xx <- function(data,g=2)sort(rep(rep(1:g,rep(length(data)/g,
g)),length.out=length(data)))
> x<- 1:7 ;cbind( sort(x) , xx(x ,2))
x
[1,] 1 1
[2,] 2 1
[3,] 3 1
[4,] 4 1     ##<
[5,] 5 2
[6,] 6 2
[7,] 7 2

this 1 is added to the first group. The best way, to decide to which group
the rest belongs should be (if the diffs between the elements are
unequal) the distance of the point to the next group should be considered?

example: to which groups belong the "2" ?

data  ....	1.9	2	3	...
g     1		1  	1 	2  	2

data	...	1	2	2.1	...
g    	1	1	2	2	2

P.
ps.: what happens when there is more than 1 resting element?
> 8 %% 3
[1] 2
> x<- 1:8 ;cbind(x,xx(x ,3))
x
[1,] 1 1
[2,] 2 1
[3,] 3 1
[4,] 4 1
[5,] 5 2
[6,] 6 2
[7,] 7 3
[8,] 8 3

???

** To YOU I'm an atheist; to God, I'm the Loyal Opposition. Woody Allen **
P.Malewski					Tel.: 0531 500965
Maschplatz 8					Email: P.Malewski at tu-bs.de
************************38114 Braunschweig********************************

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```