# [R] avoiding loops in equation

Julius Tesoro jutesoro at yahoo.com
Sat Oct 17 12:01:57 CEST 2009

```thank you very much

--- On Sat, 10/17/09, Kenn Konstabel <lebatsnok at gmail.com> wrote:

> From: Kenn Konstabel <lebatsnok at gmail.com>
> Subject: Re: [R] avoiding loops in equation
Thank God for R-help mailing list. Thanks..

> To: "Julius Tesoro" <jutesoro at yahoo.com>
> Date: Saturday, October 17, 2009, 1:51 PM
> a3 <- sapply(acc, function(x)
> pex(x,pga,std), simplify=FALSE)
> do.call(rbind, a3) #
>
> On Sat, Oct 17, 2009 at 5:51 AM,
> Julius Tesoro <jutesoro at yahoo.com>
> wrote:
>
> To illustrate my problem, I have a
> complex code of several matrices and a vector. To simplify I
> only used two matrices and a vector as an example:
>
>
>
>
> pex<-function(acc, pga, std){
>
>   (acc-pga)/std
>
> }
>
>
>
> acc<-seq(.1,1,.1)
>
> pga<-matrix(rnorm(9,4,.1),3,3)
>
> std<-matrix(rnorm(9,4,.5),3,3)
>
>
>
> I tried calculating the above function for each element of
> acc using:
>
>
>
>         for (x in 1:length(acc)){
>
>                 a1<-pex(acc[x],pga,std)
>
>                 if(x==1){a2<-a1
>
>                         }else
> {a2<-rbind(a2,a1)
>
>
>
>         }
>
>
>
> I want to recode the following using sapply
>
>
>
>         a2<-sapply(acc, function(x) pex(x,pga,std))
>
>
>
> but what I get is a different result.
>
>
>
> Anyone knows how to rewrite the above function without
> using loops?
>
>
>
> Thanks everyone.
>
>
>
> ______________________________________________
>
> R-help at r-project.org
> mailing list
>
> https://stat.ethz.ch/mailman/listinfo/r-help
>
>
> and provide commented, minimal, self-contained,
> reproducible code.
>
>
>
>

```