简体   繁体   中英

How to calculate the mean in R with several conditions

I am trying to calculate the means of this data frame and group them in a tale! I know how to do that using averageifs in excel but because I want to eventually get the standard deviation and coefficient of variation (CV) I need to learn that in R.

For now I just need the mean. Here are my conditions:

I need a table where the "stim_ending_t" which is my time intervals are arranged from 1.0 to 3.5 in a rows. For time intervals I need these three conditions to be met while calculating the mean which is "key_resp_2.rt"

only image Visibility and soundvolume(V=1 & s=0)

Only Sound (V=0 & s=1)

Blank (V=0 & s=0)

The data frame

Expected out come

This will compute stim_ending_t (6) x modality (3) = 18 group means.

First I generate some data like your analysis_v or analysis_a data frames:

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))

Then I pipe the object into the code block:

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)

Which results in the requested output format:

# 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

You would need to repeat the code block using analysis_a to get those means.

Thank you for your help @Matthew Schuelke, but with your code I was getting different results every time I run the code.

Here is how I solved the problem with this code:

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))

The result as I wanted:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM