Hello, I would like to run a script in which a loop is included. Since I'm new to R, I cannot manage the following problem. I really hope someone could help me out.
Data in the variable Y should be removed from the simulated data set with probability 0.50 if the variable X has a value below zero, and with probability 0.10 if X has a value above zero (see script). However, the total number of removed values from Y should be 20 when X < 0, and 4 when X>0. Whenever the total number of removed values is less than 20 (when X<0), R should remove more values from Y at random (until a total of 20 values has been removed). When R has removed more than 20 values from Y, R should restore some removed values such that a maximum of 20 values has been removed from variable Y. How can I tell R to put some removed values back in the data set, or to remove more values until a maximum number of removed values has been reached? y <- rnorm(40,1,3) x <- 1+2*y1+ rnorm(40,0,5) #Remove values in Y dependent on X: ynew <- rep(NA,40) for (j in 1:40) { if (x[j] < 0){ynew[j] <- rbinom(1,1,0.50)} if (x[j] > 0){ynew[j] <- rbinom(1,1,0.10)} } -- View this message in context: http://r.789695.n4.nabble.com/Simulating-data-loop-tp3019044p3019044.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.