s-news
[Top] [All Lists]

Re: More elegant usage of outer?

To: "'Steve Su'" <s.su@qut.edu.au>, s-news@lists.biostat.wustl.edu
Subject: Re: More elegant usage of outer?
From: "Liaw, Andy" <andy_liaw@merck.com>
Date: Thu, 23 Sep 2004 23:44:03 -0400
[I posted this function to R-help a while ago, but should work the same in
S-PLUS.]

gouter <- function(x, FUN, ...) {
  xgrid <- as.list(do.call("expand.grid", x))
  names(xgrid) <- NULL
  array(do.call(deparse(substitute(FUN)), c(xgrid, list(...))),
                dim=sapply(x, length), dimnames=x)
}


## Examples:
> list1<-1:3
> list2<-4:7
> list3<-9:12
> gouter(list(list1, list2, list3), paste)
, , 9

  4       5       6       7      
1 "1 4 9" "1 5 9" "1 6 9" "1 7 9"
2 "2 4 9" "2 5 9" "2 6 9" "2 7 9"
3 "3 4 9" "3 5 9" "3 6 9" "3 7 9"

, , 10

  4        5        6        7       
1 "1 4 10" "1 5 10" "1 6 10" "1 7 10"
2 "2 4 10" "2 5 10" "2 6 10" "2 7 10"
3 "3 4 10" "3 5 10" "3 6 10" "3 7 10"

, , 11

  4        5        6        7       
1 "1 4 11" "1 5 11" "1 6 11" "1 7 11"
2 "2 4 11" "2 5 11" "2 6 11" "2 7 11"
3 "3 4 11" "3 5 11" "3 6 11" "3 7 11"

, , 12

  4        5        6        7       
1 "1 4 12" "1 5 12" "1 6 12" "1 7 12"
2 "2 4 12" "2 5 12" "2 6 12" "2 7 12"
3 "3 4 12" "3 5 12" "3 6 12" "3 7 12"

Andy

> From: Steve Su
> 
> Dear All,
> 
> I am trying to write a nice and elegant function in Splus 6.0 
> on Windows XP.
> Basically what I am trying to do is this:
> 
> list1<-1:3
> list2<-4:7
> list3<-9:12
> list4<-0:2
> 
> # If I only have list1 and list2
> outer(list1,list2,"paste")
> # If I have list1, list2 and list3
> outer(outer(list1,list2,"paste"),list3,"paste")
> # If I have list1, list2, list3 and list4
> outer(outer(outer(list1,list2,"paste"),list3,"paste"),list4,"paste")
> # And so forth.....
> 
> I am thinking there must be a more elegant solution instead 
> of using outer
> recursively? It does not matter if the final output is a 
> matrix or array as
> long as I can get all the combinations right. I seem to be 
> rather blind at
> the moment to see a better solution than the above!
> 
> Thank you for your attention.
> 
> Steve.
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> **************************************************************
> **************
> **********
> 
>  Steve Su (s.su@qut.edu.au)
>  Postdoctoral fellow
> 
>  School of Accountancy
>  Queensland University of Technology
> 
>  Postal Address: Steve Su, School of Accountancy, QUT, PO Box 2434,
> Brisbane,
>  Queensland, Australia, 4001.
> 
>  Phone:  +61 7 3864 4357
>  Fax:    +61 7 3864 1812
>  Mobile: 0421  840  586
>      .
>    _--_|\
>   /      QUT
>   \_.--._/
>         v
> 
> **************************************************************
> **************
> **********
> 
> --------------------------------------------------------------------
> This message was distributed by s-news@lists.biostat.wustl.edu.  To
> unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
> the BODY of the message:  unsubscribe s-news
> 
> 


------------------------------------------------------------------------------
Notice:  This e-mail message, together with any attachments, contains 
information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station, New 
Jersey, USA 08889), and/or its affiliates (which may be known outside the 
United States as Merck Frosst, Merck Sharp & Dohme or MSD and in Japan, as 
Banyu) that may be confidential, proprietary copyrighted and/or legally 
privileged. It is intended solely for the use of the individual or entity named 
on this message.  If you are not the intended recipient, and have received this 
message in error, please notify us immediately by reply e-mail and then delete 
it from your system.
------------------------------------------------------------------------------

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