1

我这里有一个数据框:每个受试者进行 6 次试验,有 105 个受试者。

我想为每个主题找到 6 次试验的“跳过”平均值。

我该如何开始?

>     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
4

2 回答 2

2

您可以ddply从 plyr 包中使用:(您提到将进行六次试验,因此平均值是通过将 6 除以观察次数来计算的,每个受试者只需选择=跳过)

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

注意:df 是您的数据

于 2013-08-30T13:44:40.463 回答
0

如果我不得不猜测,那么您打算n_gambles对每个主题求平均值 where choice==skip,那么这可能会起作用:

# 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

编辑:据我了解,您想要skipper的频率subj:试试这个:

# 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
于 2013-08-30T12:55:50.997 回答