I would like to assign a dummy variable named "sender", however, I want to do it randomly within each session only and not the entire experiment.
Assume I have a data of 180 students. Each session has 18 students. Thus I have 10 sessions. Within each of these sessions, there should be 9 senders (value of 1) and 9 receivers (value of 0).
So far, I only managed to do this, with the entire experiment as follow:
va <- c(1,0)
df$sender[sample(1:nrow(df1), nrow(df1), FALSE)] <- rep(va, 90,90)
I am thinking of doing it in a primitive way by applying the same code above 10 times repeatedly for each session, but the data may get larger than that. I would be grateful for some help! Thank you!
Not really sure what your expected output is, but this should help you in the right direction:
students = 5 # per trial
trials = 3
senders = 2 # per trial
df = data.frame(studentID = seq(1,students*trials),session =
rep(seq(trials), times = rep(students,trials)))
df$sender = unlist(sapply(seq(trials), function(x)
{as.numeric(seq(1,students) %in% sample(students,senders))}, simplify=F))
Output:
Now, we have got 3 sessions, each with 5 students and exactly 2 senders per trial.
studentID session sender
1 1 1 0
2 2 1 0
3 3 1 1
4 4 1 1
5 5 1 0
6 6 2 1
7 7 2 0
8 8 2 1
9 9 2 0
10 10 2 0
11 11 3 1
12 12 3 1
13 13 3 0
14 14 3 0
15 15 3 0
Hope this helps!