Re: [R] dependent nested for loops in R

2021-02-02 Thread Shaami
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

Re: [R] dependent nested for loops in R

2021-02-01 Thread David Winsemius
Sent from my iPhone > On Feb 1, 2021, at 10:16 PM, David Winsemius 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

Re: [R] dependent nested for loops in R

2021-02-01 Thread 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 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 wrote: >>> >> Just drop the “+” if you

Re: [R] dependent nested for loops in R

2021-02-01 Thread 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 wrote: > > Just drop the “+” if you want logical. > > Sent from my iPhone > >>> On Feb 1, 2021, at 9:36 PM, Shaami wrote: >>> >>  >> Hi Prof. David >> >> Thank you. I will

Re: [R] dependent nested for loops in R

2021-02-01 Thread David Winsemius
Just drop the “+” if you want logical. Sent from my iPhone > On Feb 1, 2021, at 9:36 PM, Shaami 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

Re: [R] dependent nested for loops in R

2021-02-01 Thread David Winsemius
IMTS length 2000 Sent from my iPhone > On Feb 1, 2021, at 9:16 PM, David Winsemius 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

Re: [R] dependent nested for loops in R

2021-02-01 Thread David Winsemius
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 wrote: > >  > Hi Prof. David > > In the following state > > W = (1:2000)[z >4|z<2) > > Could you

Re: [R] dependent nested for loops in R

2021-02-01 Thread Shaami
Hi Duncan It worked. Thank you. Best Regards On Mon, Feb 1, 2021 at 6:26 PM Duncan Murdoch 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

Re: [R] dependent nested for loops in R

2021-02-01 Thread Duncan Murdoch
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

Re: [R] dependent nested for loops in R

2021-02-01 Thread 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

Re: [R] dependent nested for loops in R

2021-01-31 Thread David Winsemius
On 1/31/21 1:26 PM, Berry, Charles wrote: On Jan 30, 2021, at 9:32 PM, Shaami 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

Re: [R] dependent nested for loops in R

2021-01-31 Thread Berry, Charles
> On Jan 30, 2021, at 9:32 PM, Shaami 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

Re: [R] dependent nested for loops in R

2021-01-31 Thread 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)

Re: [R] dependent nested for loops in R

2021-01-30 Thread 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] dependent nested for loops in R

2021-01-30 Thread 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 =