【问题标题】:How to calculate the mean in R with several conditions如何在多个条件下计算 R 中的平均值
【发布时间】:2019-11-06 13:55:52
【问题描述】:

我正在尝试计算此数据框的均值并将它们分组为一个故事!我知道如何在 excel 中使用 averageifs 来做到这一点,但因为我想最终获得标准差和变异系数 (CV),所以我需要在 R 中学习。

现在我只需要平均值。这是我的条件:

我需要一个表格,其中“stim_ending_t”是我的时间间隔从 1.0 到 3.5 连续排列。对于时间间隔,我需要在计算“key_resp_2.rt”的平均值时满足这三个条件

仅图像可见性和音量(V=1 & s=0)

只有声音(V=0 & s=1)

空白(V=0 & s=0)

The data frame

Expected out come

【问题讨论】:

    标签: r conditional-statements mean


    【解决方案1】:

    这将计算 stim_ending_t (6) x 模态 (3) = 18 组均值。

    首先,我生成一些数据,例如您的 analysis_vanalysis_a 数据框:

    library(dplyr)
    library(tidyr)
    
    analysis_v <- data.frame(stim_ending_t = rep(seq(1, 3.5, 0.5), each = 30), 
                             visbility = rep(c(1, 0, 0), 60), 
                             soundvolume = rep(c(0, 1, 0), 60), 
                             key_resp_2.rt = runif(180, 1, 5))
    

    然后我将对象通过管道传输到代码块中:

    analysis_v %>% 
      group_by(stim_ending_t, visbility, soundvolume) %>% 
      summarize(average = mean(key_resp_2.rt)) %>% 
      ungroup() %>% 
      mutate(key = case_when(visbility == 0 & soundvolume == 0 ~ "blank", 
                             visbility == 0 & soundvolume == 1 ~ "only_sound", 
                             visbility == 1 & soundvolume == 0 ~ "only_images")) %>% 
      select(-visbility, -soundvolume) %>% 
      spread(key, average)
    

    这会导致请求的输出格式:

    # A tibble: 6 x 4
      stim_ending_t blank only_images only_sound
              <dbl> <dbl>       <dbl>      <dbl>
    1           1    3.28        3.55       2.84
    2           1.5  2.64        3.11       2.32
    3           2    3.27        3.72       2.42
    4           2.5  2.14        3.01       2.30
    5           3    2.47        3.03       3.02
    6           3.5  2.93        2.92       2.78
    

    您需要使用 analysis_a 重复代码块才能获得这些方法。

    【讨论】:

    • 感谢您的帮助,但我只得到一张 6 * 4 结果的表格?另外,我使用“AVERAGEIFS”从我的 excel 中获得的数字与我从 6*4 表中获得的数字不同?有什么想法吗?
    • 有六行,因为您有六个级别的区间(即 1.0、1.5、2.0、2.5、3.0 和 3.5)。有四列,因为有一个间隔列,组意味着只有图像、只有声音和空白。此格式与显示 6x4 的 expected output format 匹配。
    • 要调查为什么输出与 Excel 不同,请分享用于将数据读入 R 的代码。
    • # 根据模态对数据进行子集化:V & A. 而 p1 是原始数据框 p1_visual &lt;- p1[p1$Opening_text=="For these trials attend to Visual", ] p1_auditory &lt;- p1[p1$Opening_text=="For these trials attend to Auditory", ] # 为视觉变量创建胜利者 blank_V &lt;- c('stim_ending_t','visbility','soundvolume','key_resp_2.rt') # 创建数据框仅用于视觉变量 analysis_v &lt;- p1_visual[blank_V] # 我对 A 做了类似的事情,然后使用你的代码并将 your_data 更改为 analysis_A 让我知道这是否是你要求的?
    • 请分享head(p1)的输出
    【解决方案2】:

    感谢您@Matthew Schuelke 的帮助,但是对于您的代码,我每次运行代码时都会得到不同的结果。

    这是我用这段代码解决问题的方法:

    name of the new data = (name of the data frame without the parentheses) %>%
      group_by(stim_ending_t, visbility, soundvolume, Opening_text) %>%
      summarize(m = mean(key_resp_2.rt),
                sd = sd(key_resp_2.rt),
                coefVar = cv(key_resp_2.rt))
    

    我想要的结果:

    stim_ending_t visbility soundvolume Opening_text               m     sd coefVar
              <dbl>     <dbl>       <dbl> <chr>                  <dbl>  <dbl>   <dbl>
    1             1         0           0 Now focus on the Image  1.70  1.14    0.670
    2             1         0           0 Now focus on the Sound  1.57  0.794   0.504
    3             1         0           1 Now focus on the Image  1.62  1.25    0.772
    4             1         0           1 Now focus on the Sound  1.84  1.17    0.637
    5             1         1           0 Now focus on the Image  3.19 17.2     5.38 
    6             1         1           0 Now focus on the Sound  1.59  0.706   0.444
    

    【讨论】:

    • 当然,每次运行我的代码都会得到不同的输出。我的代码每次都会生成随机数据,因为我只有一小部分数据可供使用。最好将更大比例的数据提供为可以直接复制和粘贴的文本,以便其他人将来可以更快地帮助您。此外,该输出与您要求的 here 不匹配。最好更明确地说明您的最终目标是什么,以便其他人将来可以更好地帮助您。
    • 很抱歉,这是我第一次使用 Stackoverflow。我已经学会了如何更简洁地提出我的问题,并且我从这里的人们那里学到了很多东西。因此,再次感谢您的帮助,请不要气馁,在未来做出更多贡献
    猜你喜欢
    • 1970-01-01
    • 2020-11-09
    • 1970-01-01
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-06
    相关资源
    最近更新 更多