Hi Sarah,

There is one thing you need to think about: how do you choose which values should not be removed if you have more than 20 and which should be if you have less than 20. In my code, I've just done it with sample(), which might not be what you need.

Here is what I have:
if (length(which(ynew==0))>20) {
y <- y[c(which(ynew==1), sample(which(ynew==0),20-length(which(ynew==1))))]
} else {
   y <- y[sample(which(ynew==1),20)]
}

Does it make what you're looking for?

HTH,
Ivan


Le 10/29/2010 14:46, Sarah a écrit :
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)}
}

--
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calan...@uni-hamburg.de

**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php

______________________________________________
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.

Reply via email to