[R] avoiding a loop

Leeds, Mark (IED) Mark.Leeds at morganstanley.com
Tue Oct 24 21:28:39 CEST 2006


christos : change your vector to

1.0,1.0,0.5,2.0,0.2,0.4,4.0,5.0

You should get 

1.00 1.00 0.50 2.00 0.40 0.16 4.00 5.00

But your function gives : 

1.00 1.00 0.50 2.00 0.40 0.08 4.00 5.00

I think it doesn't work when there are two in a row elements that are
less than 1.0.
Again, I do appreciate your help.




-----Original Message-----
From: Christos Hatzis [mailto:christos at nuverabio.com] 
Sent: Tuesday, October 24, 2006 3:01 PM
To: Leeds, Mark (IED); R-help at stat.math.ethz.ch
Subject: RE: [R] avoiding a loop

Try this (essentially the trick is to shift the invector to get the
y[i-i]
effect):

constructLt<-function(invector, a=1) {
    invector[invector<a] <- c(0,invector)[invector<a] *
invector[invector<a]
    invector
}

> aa <- c(1,1,0.5,2,3,0.4,4,5)
> aa
[1] 1.0 1.0 0.5 2.0 3.0 0.4 4.0 5.0
> constructLt(aa)
[1] 1.0 1.0 0.5 2.0 3.0 1.2 4.0 5.0

-Christos

-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Leeds, Mark (IED)
Sent: Tuesday, October 24, 2006 2:36 PM
To: R-help at stat.math.ethz.ch
Subject: [R] avoiding a loop

I think I asked a similar question 3 years ago to the  Splus list and I
think the answer was no or noone answered so noone should spend more
than 5 minutes on this because it could definitely be a waste of time.
 
My question is whether the function below can be rewritten without a for
loop. apply is fine if it can be done that way but i doubt it. I call it
a lot and would prefer to not loop.
 
#-----------------------------------------------------------------------
--------------------------
 
constructLt<-function(invector) {
 
outvector<-invector
 
 for ( i in 2:length(invector) ) {
 if ( invector[i] < 1 ) {
  outvector[i]<-invector[i]*outvector[i-1]
 }
}
 
return(outvector)
 
}

#-----------------------------------------------------------------------
-------------------------
--------------------------------------------------------

This is not an offer (or solicitation of an offer) to buy/sell the
securities/instruments mentioned or an official confirmation.  Morgan
Stanley may deal as principal in or own or act as market maker for
securities/instruments mentioned or may advise the issuers.  This is not
research and is not from MS Research but it may refer to a research
analyst/research report.  Unless indicated, these views are the author's
and may differ from those of Morgan Stanley research or others in the
Firm.  We do not represent this is accurate or complete and we may not
update this.
Past performance is not indicative of future returns.  For additional
information, research reports and important disclosures, contact me or
see https://secure.ms.com/servlet/cls.  You should not use e-mail to
request, authorize or effect the purchase or sale of any security or
instrument, to send transfer instructions, or to effect any other
transactions.  We cannot guarantee that any such requests received via !
 e-mail will be processed in a timely manner.  This communication is
solely for the addressee(s) and may contain confidential information.
We do not waive confidentiality by mistransmission.  Contact me if you
do not wish to receive these communications.  In the UK, this
communication is directed in the UK to those persons who are market
counterparties or intermediate customers (as defined in the UK Financial
Services Authority's rules).

	[[alternative HTML version deleted]]

______________________________________________
R-help at stat.math.ethz.ch 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.
--------------------------------------------------------

This is not an offer (or solicitation of an offer) to buy/se...{{dropped}}



More information about the R-help mailing list