這是很好的看到你自己解決它。我這給了一個嘗試,這裏是我是如何做的:
數據建立
# 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%。