Message-ID: <39B6DDB9048D0F4DAD42CB26AAFF0AFA076DEE@usctmx1106.merck.com>
Date: 2005-04-16T14:15:38Z
From: Liaw, Andy
Subject: function corresponding to map of perl
> From: Fernando Saldanha
>
> I defined map as follows:
>
> map <- function(x, y, fun) {
> mymat <- matrix( c(x,y), c(length(x), 2) )
> tmat <- t(mymat)
> oldmat <- tmat
> result <- apply(tmat, 2, function(x) {fun(x[1], x[2])})
> }
>
> It seems to work (see below). Of course you can turn it into
> a one-liner.
>
> > a<-c(1,2,3)
> > b<-c(4,5,6)
> > mysum <- function(x, y) {x + y}
> > map <- function(x, y, fun) {
> + mymat <- matrix( c(x,y), c(length(x), 2) )
> + tmat <- t(mymat)
> + oldmat <- tmat
> + result <- apply(tmat, 2, function(x) {fun(x[1], x[2])})
> + }
> > (test <- map(a, b, mysum))
> [1] 5 7 9
Maybe you're re-inventing mapply()?
> mapply(mysum, a, b)
[1] 5 7 9
Andy
>