-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patholive_oil_randomizer.Rmd
56 lines (41 loc) · 1.77 KB
/
olive_oil_randomizer.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
---
title: "Olive Oil Randomizer"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## This code will perform complete randomization on participants who have not yet been assigned to treatment or control groups. It will also perform a simple binary randomization on participants who have not yet been assigned to take either olive oil #1 or #2 first.
```{r import dependencies, eval=FALSE}
library(data.table)
library(readxl)
library("writexl")
set.seed(14)
```
```{r import data, eval=FALSE}
filename <- "Olive Oil Experiment Participant List_3_19_22.xlsx" # Change Me!
data <- read_excel(paste("./data/participant_data/", filename, sep=""),
sheet = 'Sheet1', skip = 2, col_names = TRUE)
data <- data.table(data)
head(data)
```
```{r random assignment to treatment and control, eval=FALSE}
azzigned <- data[is.na(azzignment) == FALSE]
needs_azzignment <- data[is.na(azzignment) == TRUE]
rand_assignment_vec <- sample(rep(c('control', 'treatment'), each=ceiling(nrow(needs_azzignment)/2)))
if (nrow(needs_azzignment) < length(rand_assignment_vec)) {
rand_assignment_vec <-rand_assignment_vec[-2]}
needs_azzignment[ , azzignment := rand_assignment_vec]
azzigned_data1 <- rbind(azzigned, needs_azzignment)
```
```{r random assignment to first olive oil, eval=FALSE}
azzigned <- azzigned_data1[is.na(first_oo) == FALSE]
needs_azzignment <- azzigned_data1[is.na(first_oo) == TRUE]
rand_assignment_vec <- rbinom(n=nrow(needs_azzignment), size=1, prob=0.5)
needs_azzignment[ , first_oo := rand_assignment_vec]
azzigned_data2 <- rbind(azzigned, needs_azzignment)
```
```{r write new excel file, eval=FALSE}
#random_file = paste("./data/randomized_participant_data/randomized_", filename, sep="")
#write_xlsx(azzigned_data2, random_file)
```