Dear falks, here I have written following function : fn <- Vectorize(function(x = 1:3, y = 3:6) { x <- matrix(x, nrow=1) y <- matrix(y, ncol=1) dat <- apply(x, 2, function(xx) { apply(y, 1, function(yy) { return(xx + yy) } ) }) return(dat)}, SIMPLIFY = TRUE) If I run this function, I got some warning message, even format of the returned object is not correct, for example :> fn(x = 1:3, y = 3:7)[1] 4 6 8 7 9 Warning message: In mapply(FUN = function (x = 1:3, y = 3:6) : longer argument not a multiple of length of shorter However if I run individual line of codes like :> x <- 1:3; y = 3:7 > x <- matrix(x, nrow=1) > y <- matrix(y, ncol=1) > dat <- apply(x, 2, function(xx) {+ apply(y, 1, function(yy) { + return(xx + yy) } ) })> dat[,1] [,2] [,3] [1,] 4 5 6 [2,] 5 6 7 [3,] 6 7 8 [4,] 7 8 9 [5,] 8 9 10 I get exactly what I want. Where I am making fault? Thanks, [[alternative HTML version deleted]]
Your arguments are not coming through. fn <- function(x = 1:3, y = 3:6) { x <- matrix(x, nrow=1) y <- matrix(y, ncol=1) dat <- apply(x, 2, function(xx) { apply(y, 1, function(yy) { return(xx + yy) } ) }) Vectorize(dat, SIMPLIFY = TRUE) return(dat)}> fn(1:3,3:7)[,1] [,2] [,3] [1,] 4 5 6 [2,] 5 6 7 [3,] 6 7 8 [4,] 7 8 9 [5,] 8 9 10 Cheers Joris On Wed, Jun 2, 2010 at 11:25 AM, Megh Dal <megh700004@yahoo.com> wrote:> Dear falks, here I have written following function : > > fn <- Vectorize(function(x = 1:3, y = 3:6) { > x <- matrix(x, nrow=1) > y <- matrix(y, ncol=1) > dat <- apply(x, 2, function(xx) { > apply(y, 1, function(yy) { > return(xx + yy) } ) }) > return(dat)}, SIMPLIFY = TRUE) > > If I run this function, I got some warning message, even format of the > returned object is not correct, for example : > > > fn(x = 1:3, y = 3:7) > [1] 4 6 8 7 9 > Warning message: > In mapply(FUN = function (x = 1:3, y = 3:6) : > longer argument not a multiple of length of shorter > > However if I run individual line of codes like : > > > x <- 1:3; y = 3:7 > > x <- matrix(x, nrow=1) > > y <- matrix(y, ncol=1) > > dat <- apply(x, 2, function(xx) { > + apply(y, 1, function(yy) { > + return(xx + yy) } ) }) > > dat > [,1] [,2] [,3] > [1,] 4 5 6 > [2,] 5 6 7 > [3,] 6 7 8 > [4,] 7 8 9 > [5,] 8 9 10 > > > I get exactly what I want. Where I am making fault? > > Thanks, > > > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@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. > >-- Joris Meys Statistical Consultant Ghent University Faculty of Bioscience Engineering Department of Applied mathematics, biometrics and process control Coupure Links 653 B-9000 Gent tel : +32 9 264 59 87 Joris.Meys@Ugent.be ------------------------------- Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php [[alternative HTML version deleted]]
Hi: This is shorter: t(outer(x, y, '+')) [,1] [,2] [,3] [1,] 4 5 6 [2,] 5 6 7 [3,] 6 7 8 [4,] 7 8 9 [5,] 8 9 10 HTH, Dennis On Wed, Jun 2, 2010 at 2:25 AM, Megh Dal <megh700004@yahoo.com> wrote:> Dear falks, here I have written following function : > > fn <- Vectorize(function(x = 1:3, y = 3:6) { > x <- matrix(x, nrow=1) > y <- matrix(y, ncol=1) > dat <- apply(x, 2, function(xx) { > apply(y, 1, function(yy) { > return(xx + yy) } ) }) > return(dat)}, SIMPLIFY = TRUE) > > If I run this function, I got some warning message, even format of the > returned object is not correct, for example : > > > fn(x = 1:3, y = 3:7) > [1] 4 6 8 7 9 > Warning message: > In mapply(FUN = function (x = 1:3, y = 3:6) : > longer argument not a multiple of length of shorter > > However if I run individual line of codes like : > > > x <- 1:3; y = 3:7 > > x <- matrix(x, nrow=1) > > y <- matrix(y, ncol=1) > > dat <- apply(x, 2, function(xx) { > + apply(y, 1, function(yy) { > + return(xx + yy) } ) }) > > dat > [,1] [,2] [,3] > [1,] 4 5 6 > [2,] 5 6 7 > [3,] 6 7 8 > [4,] 7 8 9 > [5,] 8 9 10 > > > I get exactly what I want. Where I am making fault? > > Thanks, > > > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@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. > >[[alternative HTML version deleted]]
Megh Dal wrote:> Dear falks, here I have written following function : > > fn <- Vectorize(function(x = 1:3, y = 3:6) { > x <- matrix(x, nrow=1) > y <- matrix(y, ncol=1) > dat <- apply(x, 2, function(xx) { > apply(y, 1, function(yy) { > return(xx + yy) } ) }) > return(dat)}, SIMPLIFY = TRUE) > > If I run this function, I got some warning message, even format of the returned object is not correct, for example : > > >> fn(x = 1:3, y = 3:7) >> > [1] 4 6 8 7 9 > Warning message: > In mapply(FUN = function (x = 1:3, y = 3:6) : > longer argument not a multiple of length of shorter > > However if I run individual line of codes like : > > >> x <- 1:3; y = 3:7 >> x <- matrix(x, nrow=1) >> y <- matrix(y, ncol=1) >> dat <- apply(x, 2, function(xx) { >> > + apply(y, 1, function(yy) { > + return(xx + yy) } ) }) > >> dat >> > [,1] [,2] [,3] > [1,] 4 5 6 > [2,] 5 6 7 > [3,] 6 7 8 > [4,] 7 8 9 > [5,] 8 9 10 > > > I get exactly what I want. Where I am making fault?I think you don't understand what Vectorize is trying to do. It is basically a way to run a loop, calling your function with each matching pair of x and y values, i.e. x=1, y=3 in the first call, then x=2 y=4 in the second, etc. The warning comes because you have three x values and five y values, so it will repeat the 1st two x values -- but it warns you that's likely not what you intended. Duncan Murdoch