lundi 3 juin 2019

R Writing prompt in a for loop to go back and change variables or proceed

I'm trying to write a prompt using readline and if/else so that I can either proceed if my data looks okay or go back and change variables if my data doesn't look okay.

I tried to make a reproducible example like below:

for (i in 1:10){
  y <- rnorm(10, mean = 0, sd = 1)
  print(min(y))
  ask<-readline(prompt="Enter 'y' if y_min is lower than -1")
  if(ask=='y'){#go back and change mean and sd of y#}else{#proceed to next line#}
}

In this example, I'm trying to make sure all y values are above -1 for each i. So if I print min(y) and if it gives me a value above -1, I'd answer n and proceed to the next line of code. Or if it gives me a value below -1, I'd answer y to the readline prompt and have it go back to the second line so that I can change mean and sd of y, try printing min(y) again, and either answer y or n and so on.

(I'm aware that there are simpler ways to ensure y doesn't go below -1 without using the readline prompt, but here I was just trying to come up with a simple reproducible example that I can post here)

I'm having trouble finding ways to have R go back to the line that I need to fix and re-run those lines again. Can anyone give me some advice on this?

Aucun commentaire:

Enregistrer un commentaire