How can I show mean value, maximum value, minimum value, variance and z-values using tidyverse/dplyr?

Fellow R-Users,

I'm trying to convert these Base-R Codes into tidyverse-Codes but can't manage to get them to work. I tried using summarise() but I couldn't manage any of these tasks.

Would be thankful for any help! :raised_hands:

What have you tried so far? what is your specific problem?, we are more inclined towards helping you with specific coding problems rather than doing your work for you.

Could you please turn this into a self-contained REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.

If you've never heard of a reprex before, you might want to start by reading this FAQ:

  1. you need to have your data in "long"-format, see the ToothGrowth dataset:
> head(ToothGrowth, 20)
    len supp dose
1   4.2   VC  0.5
2  11.5   VC  0.5
3   7.3   VC  0.5
4   5.8   VC  0.5
5   6.4   VC  0.5
6  10.0   VC  0.5
7  11.2   VC  0.5
8  11.2   VC  0.5
9   5.2   VC  0.5
10  7.0   VC  0.5
11 16.5   VC  1.0
12 16.5   VC  1.0
13 15.2   VC  1.0
14 17.3   VC  1.0
15 22.5   VC  1.0
16 17.3   VC  1.0
17 13.6   VC  1.0
18 14.5   VC  1.0
19 18.8   VC  1.0
20 15.5   VC  1.0
  1. You may need to group your data as you want to calculate these values on multiple conditions at the same time, here by dose and supp.
  2. with summarise() the results are summarised in a single line per condition.
  3. adding na.rm = TRUE helps when there migth be missing values (not needed for the example data but is better to add it - just in case)
  4. you just define the column containing the data you want to calculate, without the $ notation.
  5. you may want to read this book:
    5 Data transformation | R for Data Science
summarised = ToothGrowth %>% 
  group_by(supp, dose) %>% 
  summarise(mean = mean(len, na.rm = TRUE),
            sd = sd(len, na.rm = TRUE))

summarised
# A tibble: 6 x 4
# Groups:   supp [2]
  supp   dose  mean    sd
  <fct> <dbl> <dbl> <dbl>
1 OJ      0.5 13.2   4.46
2 OJ      1   22.7   3.91
3 OJ      2   26.1   2.66
4 VC      0.5  7.98  2.75
5 VC      1   16.8   2.52
6 VC      2   26.1   4.80

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.