[R] Odp: Loop to mapply. Error

Petr PIKAL petr.pikal at precheza.cz
Thu Feb 3 13:06:25 CET 2011


Hi

Alaios <alaios at yahoo.com> napsal dne 03.02.2011 12:16:45:

> Hello Petr,
> 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)
> 
> 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

For your purpose there is no need to call mapply and function.

sr[borders[,1], borders[,2]] 

shall be enough.

However values in borders are not integer and are negative. So in that 
case your function excludes  respective column and row and in case of 
fractional value it is rounded (actually from help page truncated)


i, j, ...
indices specifying elements to extract or replace. Indices are numeric or 
character vectors or empty (missing) or NULL. Numeric values are coerced 
to integer as by as.integer (and hence truncated towards zero)

In that case I do not understand what you really want as an output of your 
findCell function based on data you provided.

Again if you want to choose values from sr based on values in borders 
variable plain selection procedure (without mapply, without function) is 
AFAIK better. If you want something else you need to state it 

See also the difference

> 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

> findCell(c(1,3),mat)
[1] 9
> mat[1,3]
[1] 9
> findCell(cbind(1:2,3:4),mat)
[1] 5
> mat[1:2,3:4]
     [,1] [,2]
[1,]    9    1
[2,]   10    2
> findCell(cbind(-(1:2),-(3:4)),mat)
     [,1] [,2] [,3]
[1,]    2   10    2
[2,]    3   11    3
[3,]    4   12    4
> mat[-(1:2),-(3:4)]
     [,1] [,2]
[1,]    3    7
[2,]    4    8

Which result is "correct" from your point of wiev?

Regards
Petr

> 
> 
> > From: Petr PIKAL <petr.pikal at precheza.cz>
> > Subject: Odp: [R] Loop to mapply. Error
> > To: "Alaios" <alaios at yahoo.com>
> > Cc: R-help at 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 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