2

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.

Susan
  • 95
  • 1
  • 8
  • 1
    Does this answer your question? [sample function gives different result in console and in knitted document when seed is set](https://stackoverflow.com/questions/56268011/sample-function-gives-different-result-in-console-and-in-knitted-document-when-s) – Ralf Stubner Feb 10 '20 at 09:05
  • You are quite right. And I had even looked at that post and thought it wasn't relevant. Thank you very much and apologies for posting a duplicate. – Susan Feb 10 '20 at 09:26
  • Duplicates are not bad at all! Once they are marked as such (you might be able to do that now, otherwise we have to wait for review process), it will even help other people to find the right answer. as you have just experienced first hand, it is far from trivial to identify the correct answer. – Ralf Stubner Feb 10 '20 at 09:30
  • 1
    BTW, as for inserting Rmd code: I usually insert a full document and format it as code by indenting it four spaces (Ctrl-K or the editor button). This way one can insert the triple tick marks as intended. – Ralf Stubner Feb 10 '20 at 09:31

0 Answers0