R:计算按下正确/错误按钮的百分比

时间:2017-08-17 12:06:53

标签: r count response aggregate percentage

我有数据显示参与者看到面孔并且不得不按下7个按钮中的一个(每个按钮对应一个情感),这样就留下了这样的数据:

    X...Emotion Intensity Response   Correct Button.RB
1       Anger       40%      Sad Incorrect         5
2         Sad      100%      Sad   Correct         5
3       Happy       50%    Happy   Correct         4
4       Anger      100%    Anger   Correct         1
5        Fear      100%     Fear   Correct         3

现在,我想计算每种情绪的正确按钮百分比和不正确的总百分比,以及某人所犯的错误(例如,对于'愤怒的面孔',有35%的错误回答,其中7, 5%是'悲伤'按钮按下,22.5%是'中性'按钮按下等...)

我想出了如何获得每种情绪的个人计数并纠正/错误:

count(df_fert, vars = c('X...Emotion','Correct'))

这为我提供了:

X...Emotion   Correct freq
1        Anger   Correct   26
2        Anger Incorrect   14
3      Disgust   Correct   11
4      Disgust Incorrect   29

有人知道按我想要的方式计算百分比的方法吗?以及如何“细分”响应类型中的错误响应?

2 个答案:

答案 0 :(得分:1)

很高兴看到你自己解决了这个问题。我试了一下,这就是我做的方式:

数据设置

# Setup
set.seed(1110)
Emot = c("Sad", "Happy", "Angry", "Fear", "Joy", "Neutral")
Emotion = sample(x = Emot, size = 50, replace = T)
Response = sample(x = Emot, size = 50, replace = T)
df = data.frame(Emotion,Response)
df$Correct = ifelse(Emotion==Response, "Correct", "Incorrect")

这给出了:

> head(df,10)
   Emotion Response   Correct
1    Angry      Joy Incorrect
2      Joy  Neutral Incorrect
3  Neutral  Neutral   Correct
4     Fear    Happy Incorrect
5    Happy  Neutral Incorrect
6      Sad    Happy Incorrect
7    Angry    Angry   Correct
8  Neutral      Sad Incorrect
9     Fear     Fear   Correct
10   Angry    Happy Incorrect

<强>计数

通过情感和响应的对组合来计算答案:

# Counting by Emotion and Response
df2 = aggregate(data = df, Correct ~ Emotion + Response, FUN = length)

这给出了:

> head(df2,10)
   Emotion Response Correct
1    Angry    Angry       1
2    Happy    Angry       1
3      Joy    Angry       1
4  Neutral    Angry       1
5      Sad    Angry       4
6    Angry     Fear       1
7     Fear     Fear       1
8    Happy     Fear       1
9      Joy     Fear       2
10 Neutral     Fear       2

<强>百分比

要计算所有情绪和每种类型的回复的正确和不正确的百分比,请执行以下操作:

library(reshape2)
results = dcast(df2, Emotion ~ Response, value.var = "Correct")
results[is.na(results)] = 0
results[,-1] = round( results[,-1]/rowSums(results[,-1])*100, digits = 2)

这给出了:

> results
  Emotion Angry  Fear Happy   Joy Neutral   Sad
1   Angry  9.09  9.09 18.18 27.27   27.27  9.09
2    Fear  0.00 16.67 33.33 16.67   16.67 16.67
3   Happy 20.00 20.00  0.00 40.00   20.00  0.00
4     Joy 12.50 25.00 12.50 12.50   12.50 25.00
5 Neutral  9.09 18.18 27.27  0.00   18.18 27.27
6     Sad 44.44  0.00 11.11 22.22   22.22  0.00

例如:愤怒情绪被正确点击了9.09%,并被错误地点击为快乐18.18%。

答案 1 :(得分:0)

我用这段代码修复了它:

freq <- count(df_fert, vars = c('X...Emotion','Response','Correct'))
freq$perc <- (freq$freq/40)*100