dependent nested for loops in R

classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

dependent nested for loops in R

Shaami
Hi
I have very large dependent nested for loops that are quite expensive
computationally. It takes weeks and does not end to give me results. Could
anyone please guide how could I use apply function or any other suggestion
for such big and dependent loops in R? A sample code is as follows.

w = NULL
for(j in 1:1000)
{
  x = rnorm(2000)
  z = x[1]
  for(i in 2:2000)
  {
    z = x[i]+5*z[i-1]
    if(z>4 | z<1) {
      w[j]=i
      break
    } else {
      w[j] = 0
    }
  }
}


Thank you

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: dependent nested for loops in R

David Winsemius

On 1/30/21 8:26 PM, Shaami wrote:

> Hi
> I have very large dependent nested for loops that are quite expensive
> computationally. It takes weeks and does not end to give me results. Could
> anyone please guide how could I use apply function or any other suggestion
> for such big and dependent loops in R? A sample code is as follows.
>
> w = NULL
> for(j in 1:1000)
> {
>    x = rnorm(2000)
>    z = x[1]
>    for(i in 2:2000)
>    {
>      z = x[i]+5*z[i-1]

I'm guessing you meant to type:

         z[i] <- x[i]+5*z[i-1]

>      if(z>4 | z<1) {

And more guesses (in the absence of any sort of problem description)
that you really wanted:


  if(z[i]>4 | z[i]<1) {  ....


>        w[j]=i
>        break
>      } else {
>        w[j] = 0
>      }
>    }
> }


Are you sure you need a for-loop? Seems like you could have done this
with a couple of vectorized operations. And the `break` looked entirely
superfluous.



> Thank you
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: dependent nested for loops in R

Shaami
Hi
I have made the sample code again. Could you please guide how to use
vectorization for variables whose next value depends on the previous one?

w = NULL

for(j in 1:1000)

{

  z = NULL

  x = rnorm(2000)

  z[1] = x[1]

  for(i in 2:2000)

  {

    z[i] = x[i]+5*z[i-1]

    if(z[i]>4 | z[i]<1) {

      w[j]=i

    } else {

      w[j] = 0

    }

  }

}


On Sun, Jan 31, 2021 at 10:01 AM David Winsemius <[hidden email]>
wrote:

>
> On 1/30/21 8:26 PM, Shaami wrote:
> > Hi
> > I have very large dependent nested for loops that are quite expensive
> > computationally. It takes weeks and does not end to give me results.
> Could
> > anyone please guide how could I use apply function or any other
> suggestion
> > for such big and dependent loops in R? A sample code is as follows.
> >
> > w = NULL
> > for(j in 1:1000)
> > {
> >    x = rnorm(2000)
> >    z = x[1]
> >    for(i in 2:2000)
> >    {
> >      z = x[i]+5*z[i-1]
>
> I'm guessing you meant to type:
>
>          z[i] <- x[i]+5*z[i-1]
>
> >      if(z>4 | z<1) {
>
> And more guesses (in the absence of any sort of problem description)
> that you really wanted:
>
>
>   if(z[i]>4 | z[i]<1) {  ....
>
>
> >        w[j]=i
> >        break
> >      } else {
> >        w[j] = 0
> >      }
> >    }
> > }
>
>
> Are you sure you need a for-loop? Seems like you could have done this
> with a couple of vectorized operations. And the `break` looked entirely
> superfluous.
>
>
>
> > Thank you
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: dependent nested for loops in R

Berry, Charles-2


> On Jan 30, 2021, at 9:32 PM, Shaami <[hidden email]> wrote:
>
> Hi
> I have made the sample code again. Could you please guide how to use
> vectorization for variables whose next value depends on the previous one?
>


Glad to help.

First, it could help you to trace your code.  I suspect that the results are not at all what you want and tracing would help you see that.

I suggest running this revision and printing out x, z, and w.

#+begin_src R
  w = NULL
  for(j in 1:2)
  {
    z = NULL
    x = rnorm(10)
    z[1] = x[1]
    for(i in 2:10)
    {
      z[i] = x[i]+5*z[i-1]
      if(z[i]>4 | z[i]<1) {
        w[j]=i
      } else {
        w[j] = 0
      }
    }
  }
#+end_src


You should be able to see that the value of w can easily be obtained outside of the `i' loop.

--

If inspecting those results did not make you go back and rethink your approach, try writing the expression for

z[n] = x[n] + ... + k * x[1]

If you are not good with algebra write out each of z[1], z[2], z[3] and z[4] in terms of x[1:4] and then look at what you have.

Perhaps the value of `k' will surprise you.

In any case, if the code is truly what you intended, you only need x[1:25] to get z[n] to double precision for any n.  So,

Also, you will hardly ever be able to compute the value of z[n] for n > 450.

If these last two statements are puzzling, see https://en.wikipedia.org/wiki/Floating-point_arithmetic

HTH,
Chuck

> w = NULL
>
> for(j in 1:1000)
>
> {
>
>  z = NULL
>
>  x = rnorm(2000)
>
>  z[1] = x[1]
>
>  for(i in 2:2000)
>
>  {
>
>    z[i] = x[i]+5*z[i-1]
>
>    if(z[i]>4 | z[i]<1) {
>
>      w[j]=i
>
>    } else {
>
>      w[j] = 0
>
>    }
>
>  }
>
> }
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: dependent nested for loops in R

David Winsemius

On 1/31/21 1:26 PM, Berry, Charles wrote:
>
>> On Jan 30, 2021, at 9:32 PM, Shaami <[hidden email]> wrote:
>>
>> Hi
>> I have made the sample code again. Could you please guide how to use
>> vectorization for variables whose next value depends on the previous one?
>>

I agree with Charles that I suspect your results are not what you
expect. You should try using cat or print to output intermediate results
to the console. I would suggest you limit your examination to a more
manageable length, say the first 10 results while you are working out
your logic. After you have the logic debugged, you can move on to long
sequences.


This is my suggestion for a more compact solution (at least for the
inner loop calculation):

set.seed(123)

x <- rnorm(2000)

z <- Reduce( function(x,y) { sum(y+5*x) }, x, accumulate=TRUE)

w<- numeric(2000)

w <-  (1:2000)[ z >4 | z < 1 ]  # In your version the w values get
overwritten and end up all being 2000


I would also advise making a natural language statement of the problem
and goals. I'm thinking that you may be missing certain aspects of the
underying problem.

--

David.

>
> Glad to help.
>
> First, it could help you to trace your code.  I suspect that the results are not at all what you want and tracing would help you see that.
>
> I suggest running this revision and printing out x, z, and w.
>
> #+begin_src R
>    w = NULL
>    for(j in 1:2)
>    {
>      z = NULL
>      x = rnorm(10)
>      z[1] = x[1]
>      for(i in 2:10)
>      {
>        z[i] = x[i]+5*z[i-1]
>        if(z[i]>4 | z[i]<1) {
> w[j]=i
>        } else {
> w[j] = 0
>        }
>      }
>    }
> #+end_src
>
>
> You should be able to see that the value of w can easily be obtained outside of the `i' loop.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: dependent nested for loops in R

Shaami
Hi David and Charles

Your suggestions helped me a lot. Could you please suggest how I could
vectorize the following for loop?
z = NULL
p = 0.25
x = rnorm(100)
z[1] = p*x[1] + (1-p)*5
for(i in 2:100)
{
  z[i] = p*x[i]+(1-p)*z[i-1]
}
 Thank you

Regards

On Mon, Feb 1, 2021 at 11:01 AM David Winsemius <[hidden email]>
wrote:

>
> On 1/31/21 1:26 PM, Berry, Charles wrote:
> >
> >> On Jan 30, 2021, at 9:32 PM, Shaami <[hidden email]> wrote:
> >>
> >> Hi
> >> I have made the sample code again. Could you please guide how to use
> >> vectorization for variables whose next value depends on the previous
> one?
> >>
>
> I agree with Charles that I suspect your results are not what you
> expect. You should try using cat or print to output intermediate results
> to the console. I would suggest you limit your examination to a more
> manageable length, say the first 10 results while you are working out
> your logic. After you have the logic debugged, you can move on to long
> sequences.
>
>
> This is my suggestion for a more compact solution (at least for the
> inner loop calculation):
>
> set.seed(123)
>
> x <- rnorm(2000)
>
> z <- Reduce( function(x,y) { sum(y+5*x) }, x, accumulate=TRUE)
>
> w<- numeric(2000)
>
> w <-  (1:2000)[ z >4 | z < 1 ]  # In your version the w values get
> overwritten and end up all being 2000
>
>
> I would also advise making a natural language statement of the problem
> and goals. I'm thinking that you may be missing certain aspects of the
> underying problem.
>
> --
>
> David.
>
> >
> > Glad to help.
> >
> > First, it could help you to trace your code.  I suspect that the results
> are not at all what you want and tracing would help you see that.
> >
> > I suggest running this revision and printing out x, z, and w.
> >
> > #+begin_src R
> >    w = NULL
> >    for(j in 1:2)
> >    {
> >      z = NULL
> >      x = rnorm(10)
> >      z[1] = x[1]
> >      for(i in 2:10)
> >      {
> >        z[i] = x[i]+5*z[i-1]
> >        if(z[i]>4 | z[i]<1) {
> >       w[j]=i
> >        } else {
> >       w[j] = 0
> >        }
> >      }
> >    }
> > #+end_src
> >
> >
> > You should be able to see that the value of w can easily be obtained
> outside of the `i' loop.
> >
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: dependent nested for loops in R

Duncan Murdoch-2
On 01/02/2021 7:03 a.m., Shaami wrote:
> z = NULL
> p = 0.25
> x = rnorm(100)
> z[1] = p*x[1] + (1-p)*5
> for(i in 2:100)
> {
>    z[i] = p*x[i]+(1-p)*z[i-1]
> }

That's the same as

p <- 0.25
x <- rnorm(100)
z <- stats::filter(p*x, 1-p, init = 5, method="recursive")

This leaves z as a time series; if that causes trouble, follow it with

z <- as.numeric(z)

Duncan Murdoch

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: dependent nested for loops in R

Shaami
Hi Duncan

It worked. Thank you.

Best Regards

On Mon, Feb 1, 2021 at 6:26 PM Duncan Murdoch <[hidden email]>
wrote:

> On 01/02/2021 7:03 a.m., Shaami wrote:
> > z = NULL
> > p = 0.25
> > x = rnorm(100)
> > z[1] = p*x[1] + (1-p)*5
> > for(i in 2:100)
> > {
> >    z[i] = p*x[i]+(1-p)*z[i-1]
> > }
>
> That's the same as
>
> p <- 0.25
> x <- rnorm(100)
> z <- stats::filter(p*x, 1-p, init = 5, method="recursive")
>
> This leaves z as a time series; if that causes trouble, follow it with
>
> z <- as.numeric(z)
>
> Duncan Murdoch
>
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: dependent nested for loops in R

David Winsemius
In reply to this post by Shaami
Cc’ed the list as should always be your practice.

Here’s one way (untested):

W <- +(z>4| z<2) # assume z is of length 20


David

Sent from my iPhone

> On Feb 1, 2021, at 7:08 PM, Shaami <[hidden email]> wrote:
>
> 
> Hi Prof. David
>
> In the following state
>
> W = (1:2000)[z >4|z<2)
>
> Could you please guide how  I can assign zero if condition is not satisfied?
>
> Best Regards
>
> Shaami
>
>> On Mon, 1 Feb 2021, 11:01 am David Winsemius, <[hidden email]> wrote:
>>
>> On 1/31/21 1:26 PM, Berry, Charles wrote:
>> >
>> >> On Jan 30, 2021, at 9:32 PM, Shaami <[hidden email]> wrote:
>> >>
>> >> Hi
>> >> I have made the sample code again. Could you please guide how to use
>> >> vectorization for variables whose next value depends on the previous one?
>> >>
>>
>> I agree with Charles that I suspect your results are not what you
>> expect. You should try using cat or print to output intermediate results
>> to the console. I would suggest you limit your examination to a more
>> manageable length, say the first 10 results while you are working out
>> your logic. After you have the logic debugged, you can move on to long
>> sequences.
>>
>>
>> This is my suggestion for a more compact solution (at least for the
>> inner loop calculation):
>>
>> set.seed(123)
>>
>> x <- rnorm(2000)
>>
>> z <- Reduce( function(x,y) { sum(y+5*x) }, x, accumulate=TRUE)
>>
>> w<- numeric(2000)
>>
>> w <-  (1:2000)[ z >4 | z < 1 ]  # In your version the w values get
>> overwritten and end up all being 2000
>>
>>
>> I would also advise making a natural language statement of the problem
>> and goals. I'm thinking that you may be missing certain aspects of the
>> underying problem.
>>
>> --
>>
>> David.
>>
>> >
>> > Glad to help.
>> >
>> > First, it could help you to trace your code.  I suspect that the results are not at all what you want and tracing would help you see that.
>> >
>> > I suggest running this revision and printing out x, z, and w.
>> >
>> > #+begin_src R
>> >    w = NULL
>> >    for(j in 1:2)
>> >    {
>> >      z = NULL
>> >      x = rnorm(10)
>> >      z[1] = x[1]
>> >      for(i in 2:10)
>> >      {
>> >        z[i] = x[i]+5*z[i-1]
>> >        if(z[i]>4 | z[i]<1) {
>> >       w[j]=i
>> >        } else {
>> >       w[j] = 0
>> >        }
>> >      }
>> >    }
>> > #+end_src
>> >
>> >
>> > You should be able to see that the value of w can easily be obtained outside of the `i' loop.
>> >

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: dependent nested for loops in R

David Winsemius
IMTS length 2000

Sent from my iPhone

> On Feb 1, 2021, at 9:16 PM, David Winsemius <[hidden email]> wrote:
>
> Cc’ed the list as should always be your practice.
>
> Here’s one way (untested):
>
> W <- +(z>4| z<2) # assume z is of length 20
>
> —
> David
>
> Sent from my iPhone
>
>>> On Feb 1, 2021, at 7:08 PM, Shaami <[hidden email]> wrote:
>>>
>> 
>> Hi Prof. David
>>
>> In the following state
>>
>> W = (1:2000)[z >4|z<2)
>>
>> Could you please guide how  I can assign zero if condition is not satisfied?
>>
>> Best Regards
>>
>> Shaami
>>
>>> On Mon, 1 Feb 2021, 11:01 am David Winsemius, <[hidden email]> wrote:
>>>
>>> On 1/31/21 1:26 PM, Berry, Charles wrote:
>>> >
>>> >> On Jan 30, 2021, at 9:32 PM, Shaami <[hidden email]> wrote:
>>> >>
>>> >> Hi
>>> >> I have made the sample code again. Could you please guide how to use
>>> >> vectorization for variables whose next value depends on the previous one?
>>> >>
>>>
>>> I agree with Charles that I suspect your results are not what you
>>> expect. You should try using cat or print to output intermediate results
>>> to the console. I would suggest you limit your examination to a more
>>> manageable length, say the first 10 results while you are working out
>>> your logic. After you have the logic debugged, you can move on to long
>>> sequences.
>>>
>>>
>>> This is my suggestion for a more compact solution (at least for the
>>> inner loop calculation):
>>>
>>> set.seed(123)
>>>
>>> x <- rnorm(2000)
>>>
>>> z <- Reduce( function(x,y) { sum(y+5*x) }, x, accumulate=TRUE)
>>>
>>> w<- numeric(2000)
>>>
>>> w <-  (1:2000)[ z >4 | z < 1 ]  # In your version the w values get
>>> overwritten and end up all being 2000
>>>
>>>
>>> I would also advise making a natural language statement of the problem
>>> and goals. I'm thinking that you may be missing certain aspects of the
>>> underying problem.
>>>
>>> --
>>>
>>> David.
>>>
>>> >
>>> > Glad to help.
>>> >
>>> > First, it could help you to trace your code.  I suspect that the results are not at all what you want and tracing would help you see that.
>>> >
>>> > I suggest running this revision and printing out x, z, and w.
>>> >
>>> > #+begin_src R
>>> >    w = NULL
>>> >    for(j in 1:2)
>>> >    {
>>> >      z = NULL
>>> >      x = rnorm(10)
>>> >      z[1] = x[1]
>>> >      for(i in 2:10)
>>> >      {
>>> >        z[i] = x[i]+5*z[i-1]
>>> >        if(z[i]>4 | z[i]<1) {
>>> >       w[j]=i
>>> >        } else {
>>> >       w[j] = 0
>>> >        }
>>> >      }
>>> >    }
>>> > #+end_src
>>> >
>>> >
>>> > You should be able to see that the value of w can easily be obtained outside of the `i' loop.
>>> >

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: dependent nested for loops in R

Shaami
In reply to this post by David Winsemius
Hi Prof. David

Thank you. I will always follow your advice. The suggested code worked. It
gives either 1 or 0 depending on the condition to be true. I want index of
z for which the condition is true (instead of 1) else zero. Could you
please suggest?

Thank you

Shaami

On Tue, Feb 2, 2021 at 10:16 AM David Winsemius <[hidden email]>
wrote:

> Cc’ed the list as should always be your practice.
>
> Here’s one way (untested):
>
> W <- +(z>4| z<2) # assume z is of length 20
>
> —
> David
>
> Sent from my iPhone
>
> On Feb 1, 2021, at 7:08 PM, Shaami <[hidden email]> wrote:
>
> 
> Hi Prof. David
>
> In the following state
>
> W = (1:2000)[z >4|z<2)
>
> Could you please guide how  I can assign zero if condition is not
> satisfied?
>
> Best Regards
>
> Shaami
>
> On Mon, 1 Feb 2021, 11:01 am David Winsemius, <[hidden email]>
> wrote:
>
>>
>> On 1/31/21 1:26 PM, Berry, Charles wrote:
>> >
>> >> On Jan 30, 2021, at 9:32 PM, Shaami <[hidden email]> wrote:
>> >>
>> >> Hi
>> >> I have made the sample code again. Could you please guide how to use
>> >> vectorization for variables whose next value depends on the previous
>> one?
>> >>
>>
>> I agree with Charles that I suspect your results are not what you
>> expect. You should try using cat or print to output intermediate results
>> to the console. I would suggest you limit your examination to a more
>> manageable length, say the first 10 results while you are working out
>> your logic. After you have the logic debugged, you can move on to long
>> sequences.
>>
>>
>> This is my suggestion for a more compact solution (at least for the
>> inner loop calculation):
>>
>> set.seed(123)
>>
>> x <- rnorm(2000)
>>
>> z <- Reduce( function(x,y) { sum(y+5*x) }, x, accumulate=TRUE)
>>
>> w<- numeric(2000)
>>
>> w <-  (1:2000)[ z >4 | z < 1 ]  # In your version the w values get
>> overwritten and end up all being 2000
>>
>>
>> I would also advise making a natural language statement of the problem
>> and goals. I'm thinking that you may be missing certain aspects of the
>> underying problem.
>>
>> --
>>
>> David.
>>
>> >
>> > Glad to help.
>> >
>> > First, it could help you to trace your code.  I suspect that the
>> results are not at all what you want and tracing would help you see that.
>> >
>> > I suggest running this revision and printing out x, z, and w.
>> >
>> > #+begin_src R
>> >    w = NULL
>> >    for(j in 1:2)
>> >    {
>> >      z = NULL
>> >      x = rnorm(10)
>> >      z[1] = x[1]
>> >      for(i in 2:10)
>> >      {
>> >        z[i] = x[i]+5*z[i-1]
>> >        if(z[i]>4 | z[i]<1) {
>> >       w[j]=i
>> >        } else {
>> >       w[j] = 0
>> >        }
>> >      }
>> >    }
>> > #+end_src
>> >
>> >
>> > You should be able to see that the value of w can easily be obtained
>> outside of the `i' loop.
>> >
>>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: dependent nested for loops in R

David Winsemius
Just drop the “+” if you want logical.

Sent from my iPhone

> On Feb 1, 2021, at 9:36 PM, Shaami <[hidden email]> wrote:
>
> 
> Hi Prof. David
>
> Thank you. I will always follow your advice. The suggested code worked. It gives either 1 or 0 depending on the condition to be true. I want index of z for which the condition is true (instead of 1) else zero. Could you please suggest?
>
> Thank you
>
> Shaami
>
>> On Tue, Feb 2, 2021 at 10:16 AM David Winsemius <[hidden email]> wrote:
>> Cc’ed the list as should always be your practice.
>>
>> Here’s one way (untested):
>>
>> W <- +(z>4| z<2) # assume z is of length 20
>>
>> —
>> David
>>
>> Sent from my iPhone
>>
>>>> On Feb 1, 2021, at 7:08 PM, Shaami <[hidden email]> wrote:
>>>>
>>> 
>>> Hi Prof. David
>>>
>>> In the following state
>>>
>>> W = (1:2000)[z >4|z<2)
>>>
>>> Could you please guide how  I can assign zero if condition is not satisfied?
>>>
>>> Best Regards
>>>
>>> Shaami
>>>
>>>> On Mon, 1 Feb 2021, 11:01 am David Winsemius, <[hidden email]> wrote:
>>>>
>>>> On 1/31/21 1:26 PM, Berry, Charles wrote:
>>>> >
>>>> >> On Jan 30, 2021, at 9:32 PM, Shaami <[hidden email]> wrote:
>>>> >>
>>>> >> Hi
>>>> >> I have made the sample code again. Could you please guide how to use
>>>> >> vectorization for variables whose next value depends on the previous one?
>>>> >>
>>>>
>>>> I agree with Charles that I suspect your results are not what you
>>>> expect. You should try using cat or print to output intermediate results
>>>> to the console. I would suggest you limit your examination to a more
>>>> manageable length, say the first 10 results while you are working out
>>>> your logic. After you have the logic debugged, you can move on to long
>>>> sequences.
>>>>
>>>>
>>>> This is my suggestion for a more compact solution (at least for the
>>>> inner loop calculation):
>>>>
>>>> set.seed(123)
>>>>
>>>> x <- rnorm(2000)
>>>>
>>>> z <- Reduce( function(x,y) { sum(y+5*x) }, x, accumulate=TRUE)
>>>>
>>>> w<- numeric(2000)
>>>>
>>>> w <-  (1:2000)[ z >4 | z < 1 ]  # In your version the w values get
>>>> overwritten and end up all being 2000
>>>>
>>>>
>>>> I would also advise making a natural language statement of the problem
>>>> and goals. I'm thinking that you may be missing certain aspects of the
>>>> underying problem.
>>>>
>>>> --
>>>>
>>>> David.
>>>>
>>>> >
>>>> > Glad to help.
>>>> >
>>>> > First, it could help you to trace your code.  I suspect that the results are not at all what you want and tracing would help you see that.
>>>> >
>>>> > I suggest running this revision and printing out x, z, and w.
>>>> >
>>>> > #+begin_src R
>>>> >    w = NULL
>>>> >    for(j in 1:2)
>>>> >    {
>>>> >      z = NULL
>>>> >      x = rnorm(10)
>>>> >      z[1] = x[1]
>>>> >      for(i in 2:10)
>>>> >      {
>>>> >        z[i] = x[i]+5*z[i-1]
>>>> >        if(z[i]>4 | z[i]<1) {
>>>> >       w[j]=i
>>>> >        } else {
>>>> >       w[j] = 0
>>>> >        }
>>>> >      }
>>>> >    }
>>>> > #+end_src
>>>> >
>>>> >
>>>> > You should be able to see that the value of w can easily be obtained outside of the `i' loop.
>>>> >

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: dependent nested for loops in R

David Winsemius
Or perhaps you wanted:

W <- z
W[z>4|z<2] <- 0

Sent from my iPhone

> On Feb 1, 2021, at 9:41 PM, David Winsemius <[hidden email]> wrote:
>
> Just drop the “+” if you want logical.
>
> Sent from my iPhone
>
>>> On Feb 1, 2021, at 9:36 PM, Shaami <[hidden email]> wrote:
>>>
>> 
>> Hi Prof. David
>>
>> Thank you. I will always follow your advice. The suggested code worked. It gives either 1 or 0 depending on the condition to be true. I want index of z for which the condition is true (instead of 1) else zero. Could you please suggest?
>>
>> Thank you
>>
>> Shaami
>>
>>> On Tue, Feb 2, 2021 at 10:16 AM David Winsemius <[hidden email]> wrote:
>>> Cc’ed the list as should always be your practice.
>>>
>>> Here’s one way (untested):
>>>
>>> W <- +(z>4| z<2) # assume z is of length 20
>>>
>>> —
>>> David
>>>
>>> Sent from my iPhone
>>>
>>>>> On Feb 1, 2021, at 7:08 PM, Shaami <[hidden email]> wrote:
>>>>>
>>>> 
>>>> Hi Prof. David
>>>>
>>>> In the following state
>>>>
>>>> W = (1:2000)[z >4|z<2)
>>>>
>>>> Could you please guide how  I can assign zero if condition is not satisfied?
>>>>
>>>> Best Regards
>>>>
>>>> Shaami
>>>>
>>>>> On Mon, 1 Feb 2021, 11:01 am David Winsemius, <[hidden email]> wrote:
>>>>>
>>>>> On 1/31/21 1:26 PM, Berry, Charles wrote:
>>>>> >
>>>>> >> On Jan 30, 2021, at 9:32 PM, Shaami <[hidden email]> wrote:
>>>>> >>
>>>>> >> Hi
>>>>> >> I have made the sample code again. Could you please guide how to use
>>>>> >> vectorization for variables whose next value depends on the previous one?
>>>>> >>
>>>>>
>>>>> I agree with Charles that I suspect your results are not what you
>>>>> expect. You should try using cat or print to output intermediate results
>>>>> to the console. I would suggest you limit your examination to a more
>>>>> manageable length, say the first 10 results while you are working out
>>>>> your logic. After you have the logic debugged, you can move on to long
>>>>> sequences.
>>>>>
>>>>>
>>>>> This is my suggestion for a more compact solution (at least for the
>>>>> inner loop calculation):
>>>>>
>>>>> set.seed(123)
>>>>>
>>>>> x <- rnorm(2000)
>>>>>
>>>>> z <- Reduce( function(x,y) { sum(y+5*x) }, x, accumulate=TRUE)
>>>>>
>>>>> w<- numeric(2000)
>>>>>
>>>>> w <-  (1:2000)[ z >4 | z < 1 ]  # In your version the w values get
>>>>> overwritten and end up all being 2000
>>>>>
>>>>>
>>>>> I would also advise making a natural language statement of the problem
>>>>> and goals. I'm thinking that you may be missing certain aspects of the
>>>>> underying problem.
>>>>>
>>>>> --
>>>>>
>>>>> David.
>>>>>
>>>>> >
>>>>> > Glad to help.
>>>>> >
>>>>> > First, it could help you to trace your code.  I suspect that the results are not at all what you want and tracing would help you see that.
>>>>> >
>>>>> > I suggest running this revision and printing out x, z, and w.
>>>>> >
>>>>> > #+begin_src R
>>>>> >    w = NULL
>>>>> >    for(j in 1:2)
>>>>> >    {
>>>>> >      z = NULL
>>>>> >      x = rnorm(10)
>>>>> >      z[1] = x[1]
>>>>> >      for(i in 2:10)
>>>>> >      {
>>>>> >        z[i] = x[i]+5*z[i-1]
>>>>> >        if(z[i]>4 | z[i]<1) {
>>>>> >       w[j]=i
>>>>> >        } else {
>>>>> >       w[j] = 0
>>>>> >        }
>>>>> >      }
>>>>> >    }
>>>>> > #+end_src
>>>>> >
>>>>> >
>>>>> > You should be able to see that the value of w can easily be obtained outside of the `i' loop.
>>>>> >

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: dependent nested for loops in R

David Winsemius
Or perhaps:

W <- 1:2000
W[z>4|z<2] <- 0

Sent from my iPhone

> On Feb 1, 2021, at 9:56 PM, David Winsemius <[hidden email]> wrote:
>
> Or perhaps you wanted:
>
> W <- z
> W[z>4|z<2] <- 0
>
> Sent from my iPhone
>
>>> On Feb 1, 2021, at 9:41 PM, David Winsemius <[hidden email]> wrote:
>>>
>> Just drop the “+” if you want logical.
>>
>> Sent from my iPhone
>>
>>>> On Feb 1, 2021, at 9:36 PM, Shaami <[hidden email]> wrote:
>>>>
>>> 
>>> Hi Prof. David
>>>
>>> Thank you. I will always follow your advice. The suggested code worked. It gives either 1 or 0 depending on the condition to be true. I want index of z for which the condition is true (instead of 1) else zero. Could you please suggest?
>>>
>>> Thank you
>>>
>>> Shaami
>>>
>>>> On Tue, Feb 2, 2021 at 10:16 AM David Winsemius <[hidden email]> wrote:
>>>> Cc’ed the list as should always be your practice.
>>>>
>>>> Here’s one way (untested):
>>>>
>>>> W <- +(z>4| z<2) # assume z is of length 20
>>>>
>>>> —
>>>> David
>>>>
>>>> Sent from my iPhone
>>>>
>>>>>> On Feb 1, 2021, at 7:08 PM, Shaami <[hidden email]> wrote:
>>>>>>
>>>>> 
>>>>> Hi Prof. David
>>>>>
>>>>> In the following state
>>>>>
>>>>> W = (1:2000)[z >4|z<2)
>>>>>
>>>>> Could you please guide how  I can assign zero if condition is not satisfied?
>>>>>
>>>>> Best Regards
>>>>>
>>>>> Shaami
>>>>>
>>>>>> On Mon, 1 Feb 2021, 11:01 am David Winsemius, <[hidden email]> wrote:
>>>>>>
>>>>>> On 1/31/21 1:26 PM, Berry, Charles wrote:
>>>>>> >
>>>>>> >> On Jan 30, 2021, at 9:32 PM, Shaami <[hidden email]> wrote:
>>>>>> >>
>>>>>> >> Hi
>>>>>> >> I have made the sample code again. Could you please guide how to use
>>>>>> >> vectorization for variables whose next value depends on the previous one?
>>>>>> >>
>>>>>>
>>>>>> I agree with Charles that I suspect your results are not what you
>>>>>> expect. You should try using cat or print to output intermediate results
>>>>>> to the console. I would suggest you limit your examination to a more
>>>>>> manageable length, say the first 10 results while you are working out
>>>>>> your logic. After you have the logic debugged, you can move on to long
>>>>>> sequences.
>>>>>>
>>>>>>
>>>>>> This is my suggestion for a more compact solution (at least for the
>>>>>> inner loop calculation):
>>>>>>
>>>>>> set.seed(123)
>>>>>>
>>>>>> x <- rnorm(2000)
>>>>>>
>>>>>> z <- Reduce( function(x,y) { sum(y+5*x) }, x, accumulate=TRUE)
>>>>>>
>>>>>> w<- numeric(2000)
>>>>>>
>>>>>> w <-  (1:2000)[ z >4 | z < 1 ]  # In your version the w values get
>>>>>> overwritten and end up all being 2000
>>>>>>
>>>>>>
>>>>>> I would also advise making a natural language statement of the problem
>>>>>> and goals. I'm thinking that you may be missing certain aspects of the
>>>>>> underying problem.
>>>>>>
>>>>>> --
>>>>>>
>>>>>> David.
>>>>>>
>>>>>> >
>>>>>> > Glad to help.
>>>>>> >
>>>>>> > First, it could help you to trace your code.  I suspect that the results are not at all what you want and tracing would help you see that.
>>>>>> >
>>>>>> > I suggest running this revision and printing out x, z, and w.
>>>>>> >
>>>>>> > #+begin_src R
>>>>>> >    w = NULL
>>>>>> >    for(j in 1:2)
>>>>>> >    {
>>>>>> >      z = NULL
>>>>>> >      x = rnorm(10)
>>>>>> >      z[1] = x[1]
>>>>>> >      for(i in 2:10)
>>>>>> >      {
>>>>>> >        z[i] = x[i]+5*z[i-1]
>>>>>> >        if(z[i]>4 | z[i]<1) {
>>>>>> >       w[j]=i
>>>>>> >        } else {
>>>>>> >       w[j] = 0
>>>>>> >        }
>>>>>> >      }
>>>>>> >    }
>>>>>> > #+end_src
>>>>>> >
>>>>>> >
>>>>>> > You should be able to see that the value of w can easily be obtained outside of the `i' loop.
>>>>>> >

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: dependent nested for loops in R

David Winsemius


Sent from my iPhone

> On Feb 1, 2021, at 10:16 PM, David Winsemius <[hidden email]> wrote:
>
> Or perhaps:
>
> W <- 1:2000
> W[z>4|z<2] <- 0

Another way:

W <- (1:2000)*(z>4|z<2)

As I said earlier you really should study logical class vectors and the operators that use them and how to apply them as indices.

> Sent from my iPhone
>
>>> On Feb 1, 2021, at 9:56 PM, David Winsemius <[hidden email]> wrote:
>>>
>> Or perhaps you wanted:
>>
>> W <- z
>> W[z>4|z<2] <- 0
>>
>> Sent from my iPhone
>>
>>>> On Feb 1, 2021, at 9:41 PM, David Winsemius <[hidden email]> wrote:
>>>>
>>> Just drop the “+” if you want logical.
>>>
>>> Sent from my iPhone
>>>
>>>>> On Feb 1, 2021, at 9:36 PM, Shaami <[hidden email]> wrote:
>>>>>
>>>> 
>>>> Hi Prof. David
>>>>
>>>> Thank you. I will always follow your advice. The suggested code worked. It gives either 1 or 0 depending on the condition to be true. I want index of z for which the condition is true (instead of 1) else zero. Could you please suggest?
>>>>
>>>> Thank you
>>>>
>>>> Shaami
>>>>
>>>>> On Tue, Feb 2, 2021 at 10:16 AM David Winsemius <[hidden email]> wrote:
>>>>> Cc’ed the list as should always be your practice.
>>>>>
>>>>> Here’s one way (untested):
>>>>>
>>>>> W <- +(z>4| z<2) # assume z is of length 20
>>>>>
>>>>> —
>>>>> David
>>>>>
>>>>> Sent from my iPhone
>>>>>
>>>>>>> On Feb 1, 2021, at 7:08 PM, Shaami <[hidden email]> wrote:
>>>>>>>
>>>>>> 
>>>>>> Hi Prof. David
>>>>>>
>>>>>> In the following state
>>>>>>
>>>>>> W = (1:2000)[z >4|z<2)
>>>>>>
>>>>>> Could you please guide how  I can assign zero if condition is not satisfied?
>>>>>>
>>>>>> Best Regards
>>>>>>
>>>>>> Shaami
>>>>>>
>>>>>>> On Mon, 1 Feb 2021, 11:01 am David Winsemius, <[hidden email]> wrote:
>>>>>>>
>>>>>>> On 1/31/21 1:26 PM, Berry, Charles wrote:
>>>>>>> >
>>>>>>> >> On Jan 30, 2021, at 9:32 PM, Shaami <[hidden email]> wrote:
>>>>>>> >>
>>>>>>> >> Hi
>>>>>>> >> I have made the sample code again. Could you please guide how to use
>>>>>>> >> vectorization for variables whose next value depends on the previous one?
>>>>>>> >>
>>>>>>>
>>>>>>> I agree with Charles that I suspect your results are not what you
>>>>>>> expect. You should try using cat or print to output intermediate results
>>>>>>> to the console. I would suggest you limit your examination to a more
>>>>>>> manageable length, say the first 10 results while you are working out
>>>>>>> your logic. After you have the logic debugged, you can move on to long
>>>>>>> sequences.
>>>>>>>
>>>>>>>
>>>>>>> This is my suggestion for a more compact solution (at least for the
>>>>>>> inner loop calculation):
>>>>>>>
>>>>>>> set.seed(123)
>>>>>>>
>>>>>>> x <- rnorm(2000)
>>>>>>>
>>>>>>> z <- Reduce( function(x,y) { sum(y+5*x) }, x, accumulate=TRUE)
>>>>>>>
>>>>>>> w<- numeric(2000)
>>>>>>>
>>>>>>> w <-  (1:2000)[ z >4 | z < 1 ]  # In your version the w values get
>>>>>>> overwritten and end up all being 2000
>>>>>>>
>>>>>>>
>>>>>>> I would also advise making a natural language statement of the problem
>>>>>>> and goals. I'm thinking that you may be missing certain aspects of the
>>>>>>> underying problem.
>>>>>>>
>>>>>>> --
>>>>>>>
>>>>>>> David.
>>>>>>>
>>>>>>> >
>>>>>>> > Glad to help.
>>>>>>> >
>>>>>>> > First, it could help you to trace your code.  I suspect that the results are not at all what you want and tracing would help you see that.
>>>>>>> >
>>>>>>> > I suggest running this revision and printing out x, z, and w.
>>>>>>> >
>>>>>>> > #+begin_src R
>>>>>>> >    w = NULL
>>>>>>> >    for(j in 1:2)
>>>>>>> >    {
>>>>>>> >      z = NULL
>>>>>>> >      x = rnorm(10)
>>>>>>> >      z[1] = x[1]
>>>>>>> >      for(i in 2:10)
>>>>>>> >      {
>>>>>>> >        z[i] = x[i]+5*z[i-1]
>>>>>>> >        if(z[i]>4 | z[i]<1) {
>>>>>>> >       w[j]=i
>>>>>>> >        } else {
>>>>>>> >       w[j] = 0
>>>>>>> >        }
>>>>>>> >      }
>>>>>>> >    }
>>>>>>> > #+end_src
>>>>>>> >
>>>>>>> >
>>>>>>> > You should be able to see that the value of w can easily be obtained outside of the `i' loop.
>>>>>>> >

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.