Home > Net >  How to "skip" or leave a blank in ifelse function?
How to "skip" or leave a blank in ifelse function?

Time:12-11

I made a program that stores negative values from deviates of a normal distribution using ifelse():

n <- 100000
z <- rnorm(n)
ifelse(z<0,z,___)

However, I want the function to work so that if the deviate is positive, the function would skip that value and proceed to the next one (I don't want to put "NA" in the no argument of the ifelse function). Is there any argument I can put in the ___ to get what I need? Thank you!

CodePudding user response:

An option could be:

n <- 100000
z <- rnorm(n)


if(z < 0) {
  print(z)
}

CodePudding user response:

If sounds to me like you really want to subset the vector:

n <- 100000
z <- rnorm(n)
z <- z[z < 0]
  • Related