calculating the mean for each subj in r -
this question has answer here:
i have question regarding calculating means each subj.
i have dataframe follows:
subj entropy n_gambles trial response rt 1 0 high 2 0 sample 4205 2 0 high 2 0 sample 676 3 0 high 2 0 skip 0 4 0 high 2 1 sample 883 5 0 high 2 1 sample 697 6 0 high 2 1 skip 0 7 0 high 2 2 sample 1493 8 0 high 2 2 sample 507 9 0 high 2 2 skip 0 10 0 high 2 3 sample 1016 and want work out means of sampling each subj.
i have worked down here don't know code next.
note: proportion of sampling each subj different.
subj trial n_gambles entropy response n_sample 2497 0 0 2 high sample 2 2498 1 0 2 high sample 0 2499 2 0 2 high sample 0 2500 3 0 2 high sample 0 2501 4 0 2 high sample 27 2502 5 0 2 high sample 0 2503 6 0 2 high sample 0 2504 7 0 2 high sample 0 2505 8 0 2 high sample 19 2506 9 0 2 high sample 0 2507 10 0 2 high sample 0 below codes i've far.
rm(list=ls()) # import 'sub.csv' data file dataframe data_subj <- read.csv ('subj.csv') head (data_subj) # import 'response.csv' data file dataframe data_response <- read.csv ('response.csv') head(data_response) # merge 'response' , 'trial' data <- merge (data_subj, data_response, by='subj') head(data) data <- as.data.frame(table(data$subj, data$trial, data$n_gambles, data$entropy, data$response)) colnames(data) <- c('subj', 'trial', 'n_gambles', 'entropy', 'response', 'n_sample') # subset "sample" data <- data[ data$response == "sample",] head(data) could please me out?
i'd expect output this:
subj trial n_gambles entropy response n_sample mean_sample/trials 0 0 2 high sample 2 1 0 2 high sample 0 2 0 2 high sample 0 3 0 2 high sample 0
this similar answer of earlier question:
library(plyr) ddply(df,.(subj),summarize,mymean=(length(which(response=="sample")))/6) subj mymean 1 0 1.166667
Comments
Post a Comment