# outer() function Classic List Threaded 5 messages Open this post in threaded view
|

## outer() function

 Greetings R-help community, I am relatively new to R, which may be why I am having trouble understanding this problem.  I am trying to use outer() to generate a graphable surface of a function.  If there is a better way to do this, I would appreciate the insight.  Otherwise, could someone suggest a method to get the outer() function to work here? Below is my simplified R program.  Further down is the output. Thanks in advance, Kyle ###################         data <- c(0, 1, 2, 3)         x <- c(0,2,4)         y <- c(0,1,2)         f <- function(x, y) sum(data*x)+y         f(0,0); f(2,0); f(4,0);         f(0,1); f(2,1); f(4,1);         f(0,2); f(2,2); f(4,2);         outer(x, y, f)         f <- function(x, y) x-x+y-y+force(sum(data^x))         outer(x, y, f) ################################## > data <- c(0, 1, 2, 3) > x <- c(0,2,4) > y <- c(0,1,2) > > f <- function(x, y) sum(data*x)+y > f(0,0); f(2,0); f(4,0);  0  12  24 > f(0,1); f(2,1); f(4,1);  1  13  25 > f(0,2); f(2,2); f(4,2);  2  14  26 > outer(x, y, f)      [,1] [,2] [,3] [1,]   20   21   22 [2,]   20   21   22 [3,]   20   21   22 Warning message: longer object length         is not a multiple of shorter object length in: data * x ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Open this post in threaded view
|

## Re: outer() function

 On Thu, 23 Mar 2006, Kyle LaMalfa wrote: > Greetings R-help community, > > I am relatively new to R, which may be why I am having trouble > understanding this problem.  I am trying to use outer() to generate a > graphable surface of a function.  If there is a better way to do this, > I would appreciate the insight.  Otherwise, could someone suggest a > method to get the outer() function to work here? > It's a FAQ (7.17).   -thomas > Below is my simplified R program.  Further down is the output. > > Thanks in advance, > Kyle > > ################### > > data <- c(0, 1, 2, 3) > x <- c(0,2,4) > y <- c(0,1,2) > > f <- function(x, y) sum(data*x)+y > f(0,0); f(2,0); f(4,0); > f(0,1); f(2,1); f(4,1); > f(0,2); f(2,2); f(4,2); > outer(x, y, f) > > f <- function(x, y) x-x+y-y+force(sum(data^x)) > outer(x, y, f) > > ################################## > >> data <- c(0, 1, 2, 3) >> x <- c(0,2,4) >> y <- c(0,1,2) >> >> f <- function(x, y) sum(data*x)+y >> f(0,0); f(2,0); f(4,0); >  0 >  12 >  24 >> f(0,1); f(2,1); f(4,1); >  1 >  13 >  25 >> f(0,2); f(2,2); f(4,2); >  2 >  14 >  26 >> outer(x, y, f) >     [,1] [,2] [,3] > [1,]   20   21   22 > [2,]   20   21   22 > [3,]   20   21   22 > Warning message: > longer object length >        is not a multiple of shorter object length in: data * x > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html> Thomas Lumley Assoc. Professor, Biostatistics [hidden email] University of Washington, Seattle ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Open this post in threaded view
|

## Re: outer() function

 In reply to this post by Kyle LaMalfa This is a FAQ: http://cran.r-project.org/doc/manuals/R-FAQ.html#Why-does-outer_0028_0029-behave-strangely-with-my-function_003fOn 3/23/06, Kyle LaMalfa <[hidden email]> wrote: > Greetings R-help community, > > I am relatively new to R, which may be why I am having trouble > understanding this problem.  I am trying to use outer() to generate a > graphable surface of a function.  If there is a better way to do this, > I would appreciate the insight.  Otherwise, could someone suggest a > method to get the outer() function to work here? > > Below is my simplified R program.  Further down is the output. > > Thanks in advance, > Kyle > > ################### > >        data <- c(0, 1, 2, 3) >        x <- c(0,2,4) >        y <- c(0,1,2) > >        f <- function(x, y) sum(data*x)+y >        f(0,0); f(2,0); f(4,0); >        f(0,1); f(2,1); f(4,1); >        f(0,2); f(2,2); f(4,2); >        outer(x, y, f) > >        f <- function(x, y) x-x+y-y+force(sum(data^x)) >        outer(x, y, f) > > ################################## > > > data <- c(0, 1, 2, 3) > > x <- c(0,2,4) > > y <- c(0,1,2) > > > > f <- function(x, y) sum(data*x)+y > > f(0,0); f(2,0); f(4,0); >  0 >  12 >  24 > > f(0,1); f(2,1); f(4,1); >  1 >  13 >  25 > > f(0,2); f(2,2); f(4,2); >  2 >  14 >  26 > > outer(x, y, f) >     [,1] [,2] [,3] > [1,]   20   21   22 > [2,]   20   21   22 > [3,]   20   21   22 > Warning message: > longer object length >        is not a multiple of shorter object length in: data * x > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html> ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide! http://www.R-project.org/posting-guide.html