Re: [R] avoiding loops in equation

2009-10-17 Thread Julius Tesoro
thank you very much

--- On Sat, 10/17/09, Kenn Konstabel lebats...@gmail.com wrote:

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

 To: Julius Tesoro jutes...@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 jutes...@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@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.
 
 
 
 




__
R-help@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.


[R] avoiding loops in equation

2009-10-16 Thread Julius Tesoro
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@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.