简体   繁体   中英

calculate the mean of trials for each subject in R

I have a dataframe here: each subject does 6 trials, there are 105 subjects.

I want to find the mean of 'skip' for 6 trials for each subj.

How do I start?

>     subj entropy n_gambles trial choice
1      0    high         2     0   skip
2      0    high         2     1   skip
3      0    high         2     2   skip
4      0    high         2     3   skip
5      0    high         2     4   skip
6      0    high         2     5   skip
7      1    high        32     0    buy
8      1    high        32     1    buy
9      1    high        32     2    buy
10     1    high        32     3    buy
11     1    high        32     4    buy
12     1    high        32     5    buy

You can use ddply from plyr package: (You mentioned that there will be six trials, so mean is computed by dividing 6 for number of observations with just choice=skip for each subject)

library(plyr)
ddply(df,.(subj),summarise,mymean=(length(which(choice=="skip")))/6)
  subj mymean
1    0      1
2    1      0

Note: df is your data

If I have to guess, then you intend to get mean of n_gambles for each subject where choice==skip , then this might work:

# Data
df<- read.table(text="subj  entropy n_gambles   trial   choice
0   high    2   0   skip
0   high    2   1   skip
0   high    2   2   skip
0   high    2   3   skip
0   high    2   4   skip
0   high    2   5   skip
1   high    32  0   buy
1   high    32  1   buy
1   high    32  2   buy
1   high    32  3   buy
1   high    32  4   buy
1   high    32  5   buy",header=T)

# Get mean
aggregate(df[df$choice == "skip","n_gambles"],
          list(subj=df[df$choice == "skip","subj"]),
          mean)

# Output
#  subj x
# 1 0 2

EDIT: As I understand you want frequency of skip per subj : Try this:

# Get counts
result <- as.data.frame(table(df$subj,df$choice))
colnames(result) <- c("subj","choice","Freq")
# Subset for "skip" and divide by 6
result <- result[ result$choice == "skip",]
result$Freq <- result$Freq/6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM