[R] Loop to mapply. Error

Alaios alaios at yahoo.com
Thu Feb 3 10:59:34 CET 2011


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






More information about the R-help mailing list