re: [S] Products of pairs

Scott.Chasalow@USERS.PV.WAU.NL
Mon, 30 Mar 1998 17:38:13 -0200


On Friday, March 27, 1998 at 11:31:22 am CET,
<suobs@verinet.com> wrote:
>Is there a simple way to obtain the products of all possible pairs of
>values that are in a data frame column?
>
>Steve Bousquin

Steve,

Yes, there is a more efficient way; one which also avoids the dreaded
"expressions nested beyond limit" error:

> z <- 1:10
> which <- combn2(n = length(z))
> z[which[,1]] * z[which[,2]]
[1] 2 3 4 5 6 7 8 9 10 6 8 10 12 14 16 18 20 12 15 18 21 24 27 30
20
[26] 24 28 32 36 40 30 35 40 45 50 42 48 54 60 56 63 70 72 80 90

This uses a general strategy that, as often heard on this list, often is
preferable in S-PLUS to the use of loops or recursion: first expand the
vector by subscripting and then form all the products "at once" using
vectorized computation.

Function combn2 is in my combinat module.
A Windows zip archive of this module is available at:

http://www.spg.wau.nl/pv/pub/chasalow/S/win/combinat .

A gzipped tar archive for UNIX is available at

http://www.spg.wau.nl/pv/pub/chasalow/S/unix/combinat .

Cheers,
Scott

*==================================*
Scott.Chasalow@users.pv.wau.nl

Wageningen Agricultural University
Laboratory of Plant Breeding
P.O. Box 386
6700 AJ Wageningen
THE NETHERLANDS
*==================================*

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