Hello all I would like to ask your help use mapply. I have a function called findCell that takes two arguments(x,sr) where x is a vector of size two (e.g x<-c(2,3) and sr is a matrix. I would like to call many times the findCell function (thus I need mapply) for different x inputs but always for the same sr. as x is a vector of size two (two cells) I want to pass inside inside the following 2*10 matrix. -The first input is the two elements of the first row cbin(test[1,1],test[1,2]) -the second input is cbind(test[2,1],test[2,2] -the third input is cbind(test[3,1],test[3,2] and so on This is the str: str(cbind(test[,1],test[,2])) num [1:30, 1:2] -1 -0.667 -0.333 0 0.333 ... so I tried the following:> 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 have spend time reading the ?mapply but I am not sure what is the problem here. Could you please help me understand what is missing? Best Regards Alex
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.defaultI would be grateful if you can help me understand me what this error message is about. Best Regards Alex
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 withmapply 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 loopmore 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 theresults 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 asinput> 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 errormessage 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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.