Home > OS >  Limit the amount of data from rnorm with over a certain number
Limit the amount of data from rnorm with over a certain number

Time:03-10

I want that only 20 datasets will be bigger or equal to 60 and the other 20 will be under 60. Can anyone help me?

X_old_q <- rnorm(40, mean = 64.03125, sd = 10)

CodePudding user response:

This is the first idea I came up with. I am sure there is a much, much better approach to your problem, but this one fixes your issue:

lowerSample = c()
while(length(lowerSample) < 20) {
  value = rnorm(1, mean = 64.03125, sd = 10)
  if(value < 60) {
    lowerSample = c(lowerSample, value)
  }
}

upperSample = c()
while(length(upperSample) < 20) {
  value = rnorm(1, mean = 64.03125, sd = 10)
  if(value >= 60) {
    upperSample = c(upperSample, value)
  }
}

X_old_q = c(lowerSample, upperSample)
  • Related