计算R中每个受试者的试验平均值 [英] calculate the mean of trials for each subject in R
本文介绍了计算R中每个受试者的试验平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这里有一个数据框:每个受试者进行6次试验,共有105个受试者.
I have a dataframe here: each subject does 6 trials, there are 105 subjects.
我想找到每个主题6次试验的跳过"平均值.
I want to find the mean of 'skip' for 6 trials for each subj.
我如何开始?
> 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
推荐答案
如果我不得不猜测,那么您打算针对choice==skip
的每个主题获取n_gambles
的平均值,那么这可能会起作用:
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
据我了解,您希望每个subj
的频率为skip
:
试试这个:
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
这篇关于计算R中每个受试者的试验平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文