这是我第一次上堆。我试过寻找答案,但我似乎找不到任何相关的东西。我希望有人能帮帮忙。
我这里有一个数据框:每个科目做6次试验,有105个科目。
我想找到每个subj的6次试验的'skip'的平均值。
请有人给我一个关于如何开始的提示。
> 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
答案 0 :(得分:2)
您可以使用plyr包中的ddply
:(您提到将有六个试验,因此通过将观察数量除以6来计算平均值,只选择=跳过每个主题)
library(plyr)
ddply(df,.(subj),summarise,mymean=(length(which(choice=="skip")))/6)
subj mymean
1 0 1
2 1 0
注意:df是您的数据
答案 1 :(得分:0)
如果我必须猜测,那么您打算为n_gambles
的每个主题获得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
编辑:
据我了解,您需要skip
每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