[R] Odp: Loop to mapply. Error

Petr PIKAL petr.pikal at precheza.cz
Thu Feb 3 12:02:19 CET 2011


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



More information about the R-help mailing list