[R] Applying function to multiple input vectors
Adaikalavan Ramasamy
ramasamy at cancer.org.uk
Mon Mar 28 12:04:35 CEST 2005
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 at 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
>
More information about the R-help
mailing list