2017-08-17 98 views
0

我有,参与者看到的面孔,不得不按下7个按键一个(对应于情感的每个按钮)给我留下了这样的数据的数据:R:计算百分比正确/不正确的按钮按下

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%是'中性'按钮等...)

我想出了如何获得个别国家TS的每一种情绪和正确/不正确:

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 

是否有人知道一种方法来计算百分比我想要的方式?还有如何在响应类型中“细分”不正确的答案?

回答

0

我使用此代码固定它:

freq <- count(df_fert, vars = c('X...Emotion','Response','Correct')) 
freq$perc <- (freq$freq/40)*100 
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 

计数

要通过算答案Emotion和Response对组合:

# 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%。