1
我有一個數據框分組,並且每個組具有相同數量的觀察值。我已經爲每個組隨機分配了1或0的值。對於給定值爲1的組中的所有觀察值,我想要用變量ysp填充一定數量的隨機1和0。對於分配給0的組,我希望同樣的變量ysp用全0填充。我如何得到我的數據的一個子集1和0的隨機分配,其餘爲0的R
這是我到目前爲止的代碼:
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}
當然,這是行不通的。 我希望變量ysp有隨機分配p%1s和0s的組的50%(pgroup%)隨機組全部爲0,另一組爲50%(1 - pgroup%)。
這是一個非常優雅的解決方案。只有一個問題。我添加了樣本()函數結尾的概率(否則它默認爲.5): fulldata $ ysp = 0 fulldata $ ysp [fulldata $ probgr == 1] = sample(0:1,sum( fulldata $ probgr == 1),replace = TRUE,prob = c(1-p,p)) 但它並不完全給出1和0的50%,而是每次都改變我認爲的概率函數)。 – Quixo1986