I would like to thank you very much both.

Your replies helped me understand.
I will bookmark you replies for future reference :)

Best Regards
Alex

--- On Thu, 2/3/11, Kenn Konstabel <lebatsnok@gmail.com> wrote:

From: Kenn Konstabel <lebatsnok@gmail.com>
Subject: Re: [R] Odp: Loop to mapply. Error
To: "Alaios" <alaios@yahoo.com>
Cc: "Petr PIKAL" <petr.pikal@precheza.cz>, R-help@r-project.org
Date: Thursday, February 3, 2011, 12:48 PM

On Thu, Feb 3, 2011 at 1:16 PM, Alaios <alaios@yahoo.com> wrote:

I am sorry that I did not provide all the required data (for me is not very straight-forward to understand what I have to provide and what not)





 findCell<-function(x,sr){ # It is just for me understand how mapply works

         sr[x[1],x[2]]

}



and I want to call mapply like that.



mapply(findCell,x=cbind(borders[1:nrow(borders),1],borders[1:nrow(borders),2]),sr=sr)

The second argument (x=cbind(borders[1:nrow(borders),1],borders[1:nrow(borders),2])
is confusing: it is equivalent to:


cbind(borders[,1],borders[,2])

... which, in turn, is equivalent to:

as.matrix(borders[,1:2])

Now is that what you had in mind?

After the first argument (a function), mapply expects any number of arguments with the same length (or else it will try to recycle the shorter argument). Is it the case that  length(as.matrix(borders[,1:2]))==length(sr). If not, you need something different. (The arguments that will be the same for all calls can be included in MoreArgs).


A simple working example of mapply is:

mapply(paste, LETTERS[1:5], 1:5, MoreArgs= list(sep="."))

But of course, you don't need no mapply in this case (paste(LETTERS[1:5], 1:5, sep=".") will do it). And if not just for learning, you don't seem to need it in your case either. Just subsetting / subset-assignment should be enough.

 
Using mapply for efficiency (as you said in your first mail) is not a good idea. Usually it doesn't make it any quicker but in some cases can make it considerably slower (due to using more memory). And it can make it more difficult to understand.


(Try, for example, write a single line of code that, using both mapply and sapply at least once, would take a data frame, and apply a function [say, `cor`] to all possible pairs of variables. Obviously very unnecessary in case of `cor`, but can be instructive.)


So it's perhaps a good idea to use mapply for simplicity rather than efficiency.

Regards,
Kenn





where borders is

str(borders)

'data.frame':   30 obs. of  2 variables:

 $ Var1                : num  -1 -0.667 -0.333 0 0.333 ...

 $ Var2                : num  -1 -1 -1 -1 -1 ...



small example is

borders[1:2,1:2]

        Var1 Var2

1 -1.0000000   -1

2 -0.6666667   -1





> From: Petr PIKAL <petr.pikal@precheza.cz>

> Subject: Odp: [R] Loop to mapply. Error

> To: "Alaios" <alaios@yahoo.com>

> Cc: R-help@r-project.org

> Date: Thursday, February 3, 2011, 11:02 AM

> Hi

>

> Well, no data so a wild guess. You want select values from

> matrix sr based

> on values in borders[,1] and borders[,2].

>

> If it is the case plain selection could be far better

>

> > mat<-matrix(1:12, 4,4)

> > mat

>      [,1] [,2] [,3] [,4]

> [1,]    1    5    9 

>   1

> [2,]    2   

> 6   10    2

> [3,]    3   

> 7   11    3

> [4,]    4   

> 8   12    4

> > b1<-sample(1:4, 3)

> > b2<-sample(1:4, 3)

> > cbind(b1,b2)

>      b1 b2

> [1,]  3  2

> [2,]  4  1

> [3,]  2  3

> > mat[cbind(b1,b2)]

> [1]  7  4 10

> >

>

> If you want something else please provide some sample code

> and data which

> can be used for reproduction of your problem.

>

> From the error it seems that your function is somehow

> incompatible with

> mapply expectation.

>

> Regards

> Petr

>

>

> r-help-bounces@r-project.org

> napsal dne 03.02.2011 10:59:34:

>

> > Goodmorning List Member,

> > I would like to ask your help using mapply or

> Vectorize to make a loop

> more efficient.

> > I have a m*4 matrix called borders and I would like to

> add a 5th column.

> Thus first

> > A. I add the new 5th column

> >

> borders<-cbind(borders,matrix(data=NA,nrow=nrow(borders)))

> > B. For every cell in this new column I call a function

> and I put the

> results in

> > for (i in c(1:nrow(borders))) # How can I can improve

> that with mapply?

> >   

> borders[i,5]<-findCell(c(borders[i,1],borders[i,2]),sr)

> >

> > As you can see from B I call a function called

> findCell that takes as

> input

> > two arguments

> > -x: a vector with two cells thus the

> c(borders[i,1],borders[i,2])

> > -sr: a matrix that is always the same (this input is

> always constant)

> >

> > Then I tried to change the loop in B and use mapply.

> > mapply(findCell,x=cbind(test[,1],test[,2]),sr=sr)

> > > Error in if (!is.finite(length.out) || length.out

> < 0L)

> > > stop("length must be non-negative number") else

> if

> > > (length.out ==  :

> > >   missing value where TRUE/FALSE

> needed

> > > Calls: mapply -> .Call -> <Anonymous>

> -> seq

> > > -> seq.default

> >

> > I would be grateful if you can help me understand me

> what this error

> message is about.

> >

> > Best Regards

> > Alex

> >

> >

> >

> >

> > ______________________________________________

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

>

>









______________________________________________

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]]

