[R] Replace for loop when vector calling itself

2011-03-07 Thread rivercode
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. Found myself having to use a For Loop because I could not get sapply working. Any suggestions ? delta - function(x)

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

2011-03-07 Thread David Winsemius
On Mar 7, 2011, at 12:34 AM, rivercode wrote: 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

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

2011-03-07 Thread rivercode
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 vector had to reference itself for the next

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

2011-03-07 Thread David Winsemius
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

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

2011-03-07 Thread William Dunlap
Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of rivercode Sent: Monday, March 07, 2011 8:12 AM To: r-help@r-project.org Subject: Re: [R] Replace for loop when vector calling itself Hope this clarifies my Q. Creating a vector where each

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

2011-03-07 Thread David Reiner
] 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

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

2011-03-07 Thread David Winsemius
-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org ] On Behalf Of David Winsemius Sent: Monday, March 07, 2011 10:52 AM To: rivercode Cc: r-help@r-project.org Subject: Re: [R] Replace for loop when vector calling itself On Mar 7, 2011, at 11:12 AM, rivercode wrote: Hope

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

2011-03-07 Thread rivercode
Hi, Thanks for your replies. In summary: 1. Replace code with c(0, cumsum(diff(theoP)) ). This is indeed correct and I had not realized it !! d = vector(mode = numeric, length= len) d[1] = 0 if (len1) for (i in 2:len) { d[i] = d[i-1] + theoP[i] - theoP[i-1] } 2. How to create