[R] An extention of outer() ?

Henrique Dallazuanna wwwhsd at gmail.com
Thu Apr 7 20:06:06 CEST 2011


Try this:

aperm(outer(x, y, fn1, y), c(3, 1, 2))[,,2]


On Thu, Apr 7, 2011 at 3:21 PM, Bogaso Christofer
<bogaso.christofer at gmail.com> wrote:
> Dear all, as per my understanding, outer() function is to facilitate
> 2-dimensional function evaluation, like to evaluate f(x,y) for different
> values of x and y. However I have slightly modified version of that, where x
> is a matrix with, say, 5 rows and 2 columns and y is a vector. Let take
> following example:
>
>
>
> fn1 <- function(x1, x2, y) return (x1+x2+y)
>
>
>
> x <- matrix(1:10, nc = 2)
>
> y <- 1:5
>
>
>
> with this input, I want to construct a 5x5 matrix whose (1,1)th element will
> be : fn1(x[1,1], x[1,2], y[1]), (1,2)th element will be : fn1(x[1,1],
> x[1,2], y[2])..... (1,5)th element will be : fn1(x[1,1], x[1,2],
> y[5]).....similarly (2,1)th element will be : fn1(x[2,1], x[2,2],
> y[1])....etc
>
>
>
> If my 'x' object is a vector then I can easily use outer() function
> effectively. However for my present case, I am looking for some ***outer()
> like function*** to get the result effectively. Can somebody help me on how
> can I do that without using any for loop?
>
>
>
> Thanks and regards,
>
>
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O



More information about the R-help mailing list