I have 9 tests with 4 levels. My aim is to shuffle both test and level orders, making sure that each sequence of 9 tests only contains once each test. And I want to repeat this several times (let’s say 14 times for instance).
Here is an example of my data frame:
test | level |
---|---|
C | 1 |
V | 1 |
OB | 1 |
OM | 1 |
GB | 1 |
GM | 1 |
SA | 1 |
SG | 1 |
T | 1 |
C | 2 |
V | 2 |
OB | 2 |
OM | 2 |
GB | 2 |
GM | 2 |
SA | 2 |
SG | 2 |
T | 2 |
C | 3 |
V | 3 |
OB | 3 |
OM | 3 |
GB | 3 |
GM | 3 |
SA | 3 |
SG | 3 |
T | 3 |
C | 4 |
V | 4 |
OB | 4 |
OM | 4 |
GB | 4 |
GM | 4 |
SA | 4 |
SG | 4 |
T | 4 |
Here is an example of the data frame I am aiming to create:
sequence | repetition 1 | repetition 2 |
---|---|---|
1 | C2 | OB2 |
1 | GB4 | T2 |
1 | GM1 | SA2 |
1 | OB1 | V1 |
1 | OM1 | OM2 |
1 | SA4 | GM3 |
1 | SG4 | SG2 |
1 | T4 | GB4 |
1 | V4 | C1 |
2 | C1 | C4 |
2 | GB3 | GB2 |
2 | GM4 | OM3 |
2 | OB4 | SG1 |
2 | OM3 | V2 |
2 | SA1 | OB4 |
2 | SG3 | SA4 |
2 | T2 | GM2 |
2 | V2 | T1 |
3 | C4 | T3 |
3 | GB2 | C3 |
3 | GM2 | OB3 |
3 | OB2 | GM1 |
3 | OM2 | SA1 |
3 | SA2 | OM2 |
3 | SG2 | GB1 |
3 | T1 | V3 |
3 | V1 | SG4 |
4 | C3 | OM1 |
4 | GB1 | V4 |
4 | GM3 | OB1 |
4 | OB3 | C2 |
4 | OM4 | GB3 |
4 | SA3 | T4 |
4 | SG1 | SG3 |
4 | T3 | SA3 |
4 | V3 | GM4 |
I have tried the following, with df as my data frame (based on a previous stackoverflow answer: Shuffle rows in a dataframe based on a condition using R):
df$code<-paste(df$test,df$level)
set.seed(123)
H1 <- df %>%
split(f = as.factor(.$test)) %>%
map_dfr(~sample(.x$code)) %>%
pivot_longer(everything(),
names_to = "test",
values_to = "code")
def <- H1$code
repetition = 1
while (repetition < 15){
tempo <- df %>%
split(f = as.factor(.$test)) %>%
map_dfr(~sample(.x$code)) %>%
pivot_longer(everything(),
names_to = "test",
values_to = "code")
def <- cbind(def, tempo$code)
repetition = repetition + 1
}
def <- as.data.frame(def)
def['sequence'] <- NA
def[c(1:9), 15] = "1"
def[c(10:18), 15] = "2"
def[c(19:27), 15] = "3"
def[c(28:36), 15] = "4"
Forgetting the fact that it is probably not very elegant, this code only works partially. I do get a data frame with 15 columns. For each sequence of 9 tests, I do not have the same tests repeated, and the level order are well shuffled within and between columns. However, all columns have the same test order. Here is an example of the data frame I obtain:
sequence | repetition 1 | repetition 2 |
---|---|---|
1 | C2 | C1 |
1 | GB4 | GB4 |
1 | GM1 | GM3 |
1 | OB1 | OB2 |
1 | OM1 | OM2 |
1 | SA4 | SA2 |
1 | SG4 | SG2 |
1 | T4 | T2 |
1 | V4 | V1 |
2 | C1 | C4 |
2 | GB3 | GB2 |
2 | GM4 | GM2 |
2 | OB4 | OB4 |
2 | OM3 | OM3 |
2 | SA1 | SA4 |
2 | SG3 | SG1 |
2 | T2 | T1 |
2 | V2 | V2 |
What should I change in my code in order to have both the level and the test order randomized in my columns?
Thanks a lot for your help!
Melissa Peignier is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.