I have a dataset of 500 trials per participant that I want to sample from in various quantities (i.e. I want to sample the same number of trials from each participant) and then compute the mean for each participant. Instead of doing so, it is creating a file with a one mean for each participant separately for each "num", e.g. if the mean for participant 1 with 125 trials is 426 that will be the whole file, then another file for participant 1 with 150 trials with a single value, and that is what happens for all participants. I was aiming for a single file for 125 with the means for all participants, then another file with the means for 150, etc.
num <- c(125,150,175,200,225,250,275,300,325,350,375,400)
Subset2 <- list()
for (x in 1:12){
for (j in num){
Subset2[[x]] <- improb2 %>% group_by(Participant) %>% sample_n(j) %>% summarise(mean = mean(RT))
}}
Here is a reproducible example:
RT <- sample(200:600, 10000, replace=T)
df <- data.frame(Participant= letters[1:20])
df <- as.data.frame(df[rep(seq_len(nrow(df)), each = 500),])
improb2 <- cbind(RT, df)
improb2 <- improb2 %>% rename(Participant = `df[rep(seq_len(nrow(df)), each = 500), ]`)
One of the desired dataframes in subset2 would be something like:
Subset2[[1]]
Participant mean
<chr> <dbl>
1 P001 475.
2 P002 403.
3 P003 481.
4 P004 393.
5 P005 376.
6 P006 402.
7 P007 497.
8 P008 372.
9 P010 341.