Re: [R] Seeking help on Vectorize()

From: Dennis Murphy <djmuser_at_gmail.com>
Date: Wed, 02 Jun 2010 08:20:42 -0700

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_at_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_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.
>
>

        [[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. Received on Wed 02 Jun 2010 - 15:23:48 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 02 Jun 2010 - 15:30:29 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive