# Re: [R] Replace for loop when vector calling itself

From: David Winsemius <dwinsemius_at_comcast.net>
Date: Mon, 07 Mar 2011 12:53:00 -0500

On Mar 7, 2011, at 12:27 PM, David Reiner wrote:

I think it just might be. There is no random or systematic factor that modifies that alternating sign in the series.

• other David.
>
> -- David
>
>
> -----Original Message-----
> From: r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-project.org
> ] On Behalf Of David Winsemius
> Sent: Monday, March 07, 2011 10:52 AM
> To: rivercode
> Cc: r-help_at_r-project.org
> Subject: Re: [R] Replace for loop when vector calling itself
>
>
> On Mar 7, 2011, at 11:12 AM, rivercode wrote:
>
>>
>> Hope this clarifies my Q.
>>
>> Creating a vector where each element is (except the first which is
>> 0) is:
>> the previous element + a calculation from another vector theoP[i] -
>> theoP[i-1]
>>
>> I could not figure out how to do this without a for loop, as the
>> to reference itself for the next element...I am missing something
>> obvious,
>> but not too sure what.
>>
>> d = vector(mode = "numeric", length= len)
>> d[1] = 0
>> if (len>1) for (i in 2:len) { d[i] = d[i-1] + theoP[i] - theoP[i-1] }
>
> so why not:
>
> c(0, cumsum(diff(theoP)) )
>
>> theoP <- sample(1:10, 15, replace=TRUE); len=length(theoP); d =
> vector(mode = "numeric", length= len)
>> d[1] = 0
>> if (len>1) for (i in 2:len) { d[i] = d[i-1] + theoP[i] - theoP[i-1] }
>>
>> d
> [1] 0 1 -6 -5 -4 0 1 0 1 -4 3 -5 -5 1 -3
>> c(0, cumsum(diff(theoP)) )
> [1] 0 1 -6 -5 -4 0 1 0 1 -4 3 -5 -5 1 -3
>> all.equal(d, c(0, cumsum(diff(theoP)) ) )
> [1] TRUE
>
> --
> David.
>>
>> Thanks,
>> Chris
>>
>>> Hi,
>>>
>>> I am missing something obvious.
>>>
>>> Need to create vector as:
>>>
>>> (0, i-1 + TheoP(i) - TheoP(i-1), repeat....) Where i is the index
>>> position
>>> in the vector and i[1] is always 0.
>>
>> I think your prototype is not agreeing with the code below. Is "i"
>> suppose to be the index (as suggested above) or the prior term (as
>> implied below)?
>>>
>>> Found myself having to use a For Loop because I could not get sapply
>>> working. Any suggestions ?
>>
>> Assuming the code below, you construct the first three or four values
>> by hand I think you will find that the intermediate values of TheoP
>> will have alternating signs.
>>
>> term2 = 2-1 + TheoP(2) - TheoP(1)
>> term3 = 3-1 + TheoP(3) - (2-1 + TheoP(2) - TheoP(1))
>> term4 = 4-1 + TheoP(4) - (3-1 + TheoP(3) - (2-1 + TheoP(2) -
>> TheoP(1)) )
>>
>> The answer to the first question will determine how you proceed. If
>> the index is being used then there are two series of cumulative sums
>> and perhaps you can construct an expression that can be fed to the
>> cumsum function.
>>
>> The diff function is also available and if the index version is
>> correct, then it might even be as simple as c(0,
>> ((1:len)-1)+diff(TheoP) )
>>
>> So clarify what is intended.
>> --
>> David.
>>
>>>
>>> delta <- function(x) {
>>>
>>> start = index[x]
>>> end = index[x+1] - 1
>>> iTheo = start:end
>>> len = length(iTheo)
>>> theoP = as.numeric(TheoBA\$Bid[iTheo])
>>> d = vector(mode = "numeric", length= len)
>>> d[1] = 0
>>> if (len>1) for (i in 2:len) { d[i] = d[i-1] + theoP[i] -
>>> theoP[i-1] }
>>> return(d)
>>> }
>>>
>>> Thanks,
>>> Chris
>>>
>>> --
>>
>>
>>
>> --
>> View this message in context: http://r.789695.n4.nabble.com/Replace-for-loop-when-vector-calling-itself-tp3338383p3339351.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> 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.
>
> David Winsemius, MD
> West Hartford, CT
>
> ______________________________________________
> 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.
>
>
> This e-mail and any materials attached hereto, including, without
> limitation, all content hereof and thereof (collectively, "XR
> Content") are confidential and proprietary to XR Trading, LLC ("XR")
> and/or its affiliates, and are protected by intellectual property
> laws. Without the prior written consent of XR, the XR Content may
> not (i) be disclosed to any third party or (ii) be reproduced or
> otherwise used by anyone other than current employees of XR or its
> affiliates, on behalf of XR or its affiliates.
>
> THE XR CONTENT IS PROVIDED AS IS, WITHOUT REPRESENTATIONS OR
> WARRANTIES OF ANY KIND. TO THE MAXIMUM EXTENT PERMISSIBLE UNDER
> APPLICABLE LAW, XR HEREBY DISCLAIMS ANY AND ALL WARRANTIES, EXPRESS
> AND IMPLIED, RELATING TO THE XR CONTENT, AND NEITHER XR NOR ANY OF
> ITS AFFILIATES SHALL IN ANY EVENT BE LIABLE FOR ANY DAMAGES OF ANY
> NATURE WHATSOEVER, INCLUDING, BUT NOT LIMITED TO, DIRECT, INDIRECT,
> CONSEQUENTIAL, SPECIAL AND PUNITIVE DAMAGES, LOSS OF PROFITS AND
> TRADING LOSSES, RESULTING FROM ANY PERSON'S USE OR RELIANCE UPON, OR
> INABILITY TO USE, ANY XR CONTENT, EVEN IF XR IS ADVISED OF THE
> POSSIBILITY OF SUCH DAMAGES OR IF SUCH DAMAGES WERE FORESEEABLE.
David Winsemius, MD
West Hartford, CT

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. Received on Mon 07 Mar 2011 - 17:56:48 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 08 Mar 2011 - 05:20:19 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.