0

I need to use stratified random assignment of 8 subjects to control and treatment groups based on their body condition score (BCS) which can be high or low.

1. high
2. high
3. high
4. high
5. low
6. low
7. low
8. low

I need to randomly get 2 groups with equal number of high and low BCS in each group.

zx8754
  • 52,746
  • 12
  • 114
  • 209

1 Answers1

0

Using data.table package you can do the following (assuming your subject ids are in a column names subject)

library(data.table)

foo = setDT(your.data.frame) # transform your df into a data.table

dat = foo[, .SD[sample(.N, round(.N * 0.5))], by = BCS] # sample 50% of rows of each BCS

foo[subject %in% dat[, .(subject), status := "treatment"][is.na(status), status:= "control"] # assign a control treatment column
Felipe Alvarenga
  • 2,572
  • 1
  • 17
  • 36