2017-08-17 127 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%。