I'm a little stuck trying to use the sample
function for my task, which is to sample n random rows from each level of a factor and create a new variable based on this and the value of another variable.
A simplified example:
Subject = c("100","100","100","100", "100", "200", "200", "200", "200", "200")
Condition = c("Blue","Blue","Blue","Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue")
Response = rnorm(10)
df = data.frame(Subject,Condition, Response)
For instance, a goal would be to sample 3 random rows for each level of Subject
, create a new variable, let's say Condition.Rand
which has the randomly selected rows labeled "Red" and the remaining ones labeled with whatever value was in Condition
-- in this case, "Blue". Thus for each Subject
, 60% of Condition.Rand
would be labeled "Red" and 40% labeled "Blue".
Thank you!
Aucun commentaire:
Enregistrer un commentaire