Re: [R] Applying function to multiple input vectors

From: Adaikalavan Ramasamy <ramasamy_at_cancer.org.uk>
Date: Mon 28 Mar 2005 - 20:04:35 EST

set.seed(1)
xx <- sample( 1:10 )
xx
 [1] 3 4 5 7 2 8 9 6 10 1

yy <- sample( 1:10 )
yy
 [1] 3 2 6 10 5 7 8 4 1 9

(xx + yy)^2
 [1] 36 36 121 289 49 225 289 100 121 100

myfun <- function(x, y) ( x + y )^2
mapply( myfun, xx, yy )
 [1] 36 36 121 289 49 225 289 100 121 100

diag( outer( xx, yy, FUN=myfun) )
 [1] 36 36 121 289 49 225 289 100 121 100

Regards, Adai

On Sun, 2005-03-27 at 15:38 -0800, Ronnen Levinson wrote:
> Hi.
> Say I have a function f with two inputs, x and y
>
> f <- function(x,y) {something}
>
> that I wish to evaluate with two input vectors of length N
>
> X <- c(x1, x2, ..., xN)
> Y <- c(y1, y2, ..., yN)
>
> to obtain the length-N output vector c( f(x1,y1), f(x2,y2), ...,
> f(xN,yN) ).
> Is there a method analogous to sapply() for this operation?
> Yours truly,
> Ronnen.
> P.S. E-mailed CCs of posted replies appreciated.
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Mon Mar 28 20:22:36 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:30:56 EST