calcuate the mean of trials for each subject in R

2019-03-02 13:11发布

问题:

This is my first time on stack. I have tried searching for an answer but i can't seem to find anything relevant. I hope someone can help.

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.

Please can someone give me a hint as to how to 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

回答1:

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


回答2:

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



标签: r mean