I wrote an R script to randomly assign participants for and RCT. I used set.seed()
to ensure I would have reproducible results.
I now want to document what I have done in an R markdown document and confusingly I don't get the same results, despite using the same seed.
Here is the code chunk:
knitr::opts_chunk$set(cache = T)
set.seed(4321)
Group <- sample(1:3, 5, replace=TRUE)
couple.df <- data.frame(couple.id=1:5,
partner1=paste0("FRS0", c(35, 36, 41, 50, 61)),
partner2=paste0("FRS0", c(38, 37, 42, 51, 62)),
Group)
print(couple.df)
And here is the output I get when running it as a chunk:
couple.id
<int>
partner1
<chr>
partner2
<chr>
Group
<int>
1 FRS035 FRS038 2
2 FRS036 FRS037 3
3 FRS041 FRS042 2
4 FRS050 FRS051 1
5 FRS061 FRS062 3
(not sure how to get this to format)
This is the same as I had when I wrote the original code as an R script.
However, when I knit the markdown file I get the following output in my html document (sorry again about the formatting - I have just copied and pasted from the html document, adding in the ticks to format it as code, pointers on how to do this properly would also be welcome)
knitr::opts_chunk$set(cache = T)
set.seed(4321)
Group <- sample(1:3, 5, replace=TRUE)
couple.df <- data.frame(couple.id=1:5,
partner1=paste0("FRS0", c(35, 36, 41, 50, 61)),
partner2=paste0("FRS0", c(38, 37, 42, 51, 62)),
Group)
print(couple.df)
## couple.id partner1 partner2 Group
## 1 1 FRS035 FRS038 1
## 2 2 FRS036 FRS037 2
## 3 3 FRS041 FRS042 3
## 4 4 FRS050 FRS051 2
## 5 5 FRS061 FRS062 1
That is, they are different. What is going on here and how can I get the markdown document to give the same results? I am committed to using the allocation I arrived at using the original script.