问题
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.
回答1:
This answer uses tidyverse
and outputs a list object data
where the names are the sample sizes. To access each sample size summary you have to use backticks data$`125`
. data$`125`
is a tibble object. I made a comment in the output where you can change it to a data.frame
object if you need.
library(tidyverse)
num <- c(125, 150, 175, 200, 225, 250, 275, 300, 325, 350, 375, 400)
# create function to sample data by certain size and summarize by mean
get_mean <- function(x, n) {
dplyr::group_by(x, Participant) %>% # group by participant
dplyr::sample_n(n) %>% # randomly sample observations
dplyr::summarize(mean = mean(RT), # get mean of RT
n = n(), # get sample size
.groups = "keep") %>%
dplyr::ungroup()
# add a pipe to as.data.frame if you don't want a tibble object
}
# create a list object where the names are the sample sizes
data <- lapply(setNames(num, num), function(sample_size) {get_mean(df, n = sample_size)})
head(data$`125`)
Participant mean n
<chr> <dbl> <int>
1 V1 20.2 125
2 V10 19.9 125
3 V11 19.8 125
4 V12 20.2 125
5 V2 20.5 125
6 V3 20.0 125
Data
I wasn't 100% sure what your dataset looked like, but I believe it looks something like this:
# create fake data for 45 participants with 500 obs per participant
df <- replicate(45, rnorm(500, 20, 4)) %>%
as.data.frame.matrix() %>%
tidyr::pivot_longer(everything(),
names_to = "Participant", # id column
values_to = "RT") %>% # value column
dplyr::arrange(Participant)
head(df) # Participant repeated 500 times, with 500 values in RT
Participant RT
<chr> <dbl>
1 V1 24.7
2 V1 15.2
3 V1 21.1
4 V1 21.6
5 V1 20.3
6 V1 25.6
If this is a similar structure (long with repeated participant IDs and a single column RT
of values) then the above should work.
来源:https://stackoverflow.com/questions/65723403/how-to-get-mean-for-all-participants-after-selecting-only-a-certain-number-of-tr