Skip to content

dependent nested for loops in R

7 messages · David Winsemius, 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

  
  
#
IMTS length 2000

Sent from my iPhone

  
  
#
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 <dwinsemius at comcast.net>
wrote:

  
  
#
Just drop the ?+? if you want logical. 

Sent from my iPhone

  
  
#
Or perhaps you wanted:

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

Sent from my iPhone

  
  
#
Or perhaps: 

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

Sent from my iPhone

  
  
#
Sent from my iPhone
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.