dimanche 6 août 2017

How do I get a random assignment of 1 and 0 for a subset of my data, and 0 for the rest in R

I have a data frame divided by groups, and each group has the same number of observations. I have randomly assigned each group a value of 1 or 0. For all the observations in the groups given a value of 1, I want a variable ysp to be filled with a set number of random 1s and 0s. For the groups assigned 0s, I want that same variable ysp to be filled with all 0s.

This is the code that I have so far:

rm(list=ls(all=TRUE))

set.seed(1984)
ngroup <- 50 # Number of groups
obs <- 50      # Number of observations per group
pgroup <- 0.5 # (1 - p) probability of groups with at least 1 non zero obs (only works if the answer is a round number)
p <- 0.5 # Once chosen the number of groups I want to have with at least one non zero obs, I want p% of 1s in those groups.

constantdata <- data.frame(id=1:ngroup)

dummies <- c(0,1)
dummies[sample(1:nrow(constantdata), nrow(constantdata), FALSE)] <- rep(dummies, c(pgroup*ngroup,(1-pgroup)*ngroup))
constantdata["probgr"] <- dummies

fulldata <- constantdata[rep(1:ngroup, each=obs),] 

fulldata$ys <- rnorm(ngroup*obs) 

#This is how I try to do it

if(fulldata$probgr=1){
fulldata$ysp[fulldata$ys > quantile(fulldata$ys, 1 - p)] <- 1 
fulldata$ysp[fulldata$ys <= quantile(fulldata$ys, 1 - p)] <- 0
}else{
fulldata$ysp=0}

Of course, this is not working. I want the variable ysp to have 50% (pgroup%) random groups all 0s and the other 50% (1 - pgroup%) of groups with a random assignment of p% 1s and 0s.




Aucun commentaire:

Enregistrer un commentaire