I have a dataframe, DF2. Here a reproducible example of a short version of my dataframe:
Scene2 = rep(c(1:10), times=9)
myDF2 <- data.frame(Scene2)
myDF2$Target <- rep(0,10, each=9)
myDF2$Target[myDF2$Scene2==7] <- 1 #actually, in my dataframe Scene2 could be equal to any number (not always 7) for Target to be equal to 1, but for simplicity I created this reproducible code.
myDF2$Trial <- rep(c(1:9),each=10)
myDF2$Route <- rep(LETTERS[1:6], each=10, length=nrow(myDF2))
I would like to create a new column Random, such that for each Trial and Route, if Target is equal to 0, then the value in Random could randomly be either 1 or 0. The important thing is that for each Trial and Route I end up with five 1, and five 0 (and when Target is equal to 1, then Random has always to be 1). The following code works, but the order doesn't look random.
library(plyr)
myDF3 <- myDF2 %>% group_by(Trial, Route) %>%
mutate(Random = ifelse(myDF2$Target==0,sample(c(0,1),replace=T, prob=c(0.5,0.5)),1)) %>% as.data.frame()
This gives me as result:
Scene2 Target Trial Route Random #I would like something more random, just an example:
1 0 1 A 1 #0
2 0 1 A 0 #0
3 0 1 A 1 #0
4 0 1 A 0 #0
5 0 1 A 1 #0
6 0 1 A 0 #1
7 1 1 A 1 #1
8 0 1 A 0 #1
9 0 1 A 1 #1
10 0 1 A 0 #1
1 0 2 B 1 #1
2 0 2 B 0 #0
3 0 2 B 1 #1
4 0 2 B 0 #0
5 0 2 B 1 #1
6 0 2 B 0 #0
7 1 2 B 1 #1
8 0 2 B 0 #0
9 0 2 B 1 #1
10 0 2 B 0 #0
1 0 3 C 1 #1
2 0 3 C 0 #1
3 0 3 C 1 #0
4 0 3 C 0 #0
5 0 3 C 1 #1
6 0 3 C 0 #0
7 1 3 C 1 #1
8 0 3 C 0 #0
9 0 3 C 1 #1
10 0 3 C 0 #0
1 0 4 D 1 #1
2 0 4 D 0 #1
3 0 4 D 1 #1
4 0 4 D 0 #1
5 0 4 D 1 #0
6 0 4 D 0 #0
7 1 4 D 1 #1
8 0 4 D 0 #0
9 0 4 D 1 #0
10 0 4 D 0 #0
How to create a more random assignment of the values 1 and 0, but fulfilling the requirement for five 1 and five 0?
In addition, as I will repeat this procedure 24 times (for 24 different participants), I will need to make sure that overall, for each Scene2 in each Trial and Route, I have the same number of 1 and 0 across the columns Random of each participant. Something like this:
Scene2 Target Trial Route Part.1 Part.2 Part.3 Part.4 … Part.24 Tot.1 Tot.0
1 0 1 A 0 1 1 0 0 12 12
2 0 1 A 1 0 1 0 0 12 12
3 0 1 A 1 0 0 0 0 12 12
4 0 1 A 0 1 0 1 0 12 12
5 0 1 A 1 0 1 1 0 12 12
6 0 1 A 1 0 0 0 1 12 12
7 1 1 A 1 1 1 1 1 24 0
8 0 1 A 0 0 1 1 1 12 12
9 0 1 A 0 1 1 1 1 12 12
10 0 1 A 0 1 0 0 1 12 12
Any suggestion would be very much appreciated. Thank you.
Aucun commentaire:
Enregistrer un commentaire