【问题标题】:R: calculate percentages for correct/incorrect button pressesR:计算正确/错误按钮按下的百分比
【发布时间】:2017-08-17 12:06:52
【问题描述】:

我有数据表明参与者看到面孔并且必须按下 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

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

【问题讨论】:

    标签: r count response aggregate percentage


    【解决方案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%。

    【讨论】:

      【解决方案2】:

      我用这段代码修复了它:

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

      【讨论】:

        猜你喜欢
        • 2022-01-21
        • 1970-01-01
        • 2016-06-15
        • 2016-09-04
        • 1970-01-01
        • 2020-06-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多