Ok...
I have 33 patients with each two legs (0 and 1).
I want to create a random sample of 33 legs but NOT with left and right leg of one patient
I tried the following (small example):
library(janitor)
data<-list()
df_HS<-data.frame()
data$x<-c(1,1,2,2,3,3,4,4,5,5,6,6)
data$y<-c(0,1,0,1,0,1,0,1,0,1,0,1)
df<-data.frame(data)
# x is subjectID
# y is leg (0=Left; 1=Right)
k=0
for(i in unique(df$x)){
k=k+1
stratdf<-df[df$x==i,]
df_HS[k+1,1:ncol(stratdf)] <- stratdf[sample(nrow(stratdf), size=1), ]
}
df_HS<-df_HS[-1,]
tabyl(df_HS$y)
df_HS$y n percent
0 4 0.6666667
1 2 0.3333333
However, I want to have 3 zeros and 3 ones every time I run this script, or at max one different (in case of uneven samples e.g. 5 patients).
This is a small example, the actual dataset is bigger.
Thanks