[S] Avoiding the loop

Humbolt, Allen (HumboltA@kochind.com)
Wed, 21 Oct 1998 15:05:11 -0500


I have a dataframe called ng like the following, where I'll list the first
two and last two records.
> ng[c(1:2, 2129:2130) , ]
Date M1 M2 M3
1 05/01/1990 1.565 1.627 1.651
2 05/02/1990 1.575 1.634 1.650
2129 10/19/1998 2.143 2.420 2.531
2130 10/20/1998 2.202 2.483 2.590

I'd like to identify the 95th percentile of all possible pairwise
differences. E.g., the following does what I want.
> ngID <- rep(" ", 9)
> ngP95 <- rep(NA, 9)
> k <- 1
> for (i in 1:3) {
+ for (j in 1:3) {
+ ngID[k] <- paste("M",as.character(i)," - ","M",as.character(j),sep="")
+ ngP95[k] <- quantile(ng[ , i+1] - ng[ , j+1], 0.95)
+ k <- k+1
+ }
+ }
> data.frame(ngID, ngP95)
ngID ngP95
1 M1 - M1 0.0000000
2 M1 - M2 0.4477100
3 M1 - M3 0.8436200
4 M2 - M1 0.4000000
5 M2 - M2 0.0000000
6 M2 - M3 0.4706499
7 M3 - M1 0.7121300
8 M3 - M2 0.3309700
9 M3 - M3 0.0000000

The real problem, though, will involve about 100 variables, not just three,
so I'm worried I'll never finish using the above loops. Can someone suggest
a way to get my final result without the loops?

Allen Humbolt
Quantitative Analyst
Koch Industries, Inc.
HumboltA@kochind.com

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