[R] Odp: Loop to mapply. Error
Alaios
alaios at yahoo.com
Thu Feb 3 12:16:45 CET 2011
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
> 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