#
# Cushings example
#
library(pacman)
#load &see
pacman::p_load(MASS, tidyverse)
help(Cushings)
## starting httpd help server ... done
##First,understand data
head(Cushings)
##    Tetrahydrocortisone Pregnanetriol Type
## a1                 3.1         11.70    a
## a2                 3.0          1.30    a
## a3                 1.9          0.10    a
## a4                 3.8          0.04    a
## a5                 4.1          1.10    a
## a6                 1.9          0.40    a
str(Cushings)
## 'data.frame':    27 obs. of  3 variables:
##  $ Tetrahydrocortisone: num  3.1 3 1.9 3.8 4.1 1.9 8.3 3.8 3.9 7.8 ...
##  $ Pregnanetriol      : num  11.7 1.3 0.1 0.04 1.1 0.4 1 0.2 0.6 1.2 ...
##  $ Type               : Factor w/ 4 levels "a","b","c","u": 1 1 1 1 1 1 2 2 2 2 ...
# method 1
## Classify data,compute mean
aggregate( . ~ Type, data = Cushings, mean)
##   Type Tetrahydrocortisone Pregnanetriol
## 1    a            2.966667          2.44
## 2    b            8.180000          1.12
## 3    c           19.720000          5.50
## 4    u           14.016667          1.20
# method 2
#In order to understand and review it,I disassemble it step by step.

dta<-split(Cushings[,-3], Cushings$Type)
head(dta)
## $a
##    Tetrahydrocortisone Pregnanetriol
## a1                 3.1         11.70
## a2                 3.0          1.30
## a3                 1.9          0.10
## a4                 3.8          0.04
## a5                 4.1          1.10
## a6                 1.9          0.40
## 
## $b
##     Tetrahydrocortisone Pregnanetriol
## b1                  8.3           1.0
## b2                  3.8           0.2
## b3                  3.9           0.6
## b4                  7.8           1.2
## b5                  9.1           0.6
## b6                 15.4           3.6
## b7                  7.7           1.6
## b8                  6.5           0.4
## b9                  5.7           0.4
## b10                13.6           1.6
## 
## $c
##    Tetrahydrocortisone Pregnanetriol
## c1                10.2           6.4
## c2                 9.2           7.9
## c3                 9.6           3.1
## c4                53.8           2.5
## c5                15.8           7.6
## 
## $u
##    Tetrahydrocortisone Pregnanetriol
## u1                 5.1           0.4
## u2                12.9           5.0
## u3                13.0           0.8
## u4                 2.6           0.1
## u5                30.0           0.1
## u6                20.5           0.8
##split Cushings by Type, Cushings[,-3] expresses the type that does not display in columns 3

sapply(split(Cushings[,-3], Cushings$Type), function(x) apply(x, 2, mean))
##                            a    b     c        u
## Tetrahydrocortisone 2.966667 8.18 19.72 14.01667
## Pregnanetriol       2.440000 1.12  5.50  1.20000
##Then compute the mean With Pregnanetriol and Tetrahydrocortisone by Type. as shown above.

# method 3
##
?do.call
## "do.call" constructs and executes a function call from a name or a function and a list of arguments to be passed to it.

##
do.call("rbind", as.list(
  by(Cushings, list(Cushings$Type), function(x) {
    y <- subset(x, select =  -Type)
    apply(y, 2, mean)
  }
)))
##   Tetrahydrocortisone Pregnanetriol
## a            2.966667          2.44
## b            8.180000          1.12
## c           19.720000          5.50
## u           14.016667          1.20
## if I delete "apply(y, 2, mean)",then we can see
do.call("rbind", as.list(
  by(Cushings, list(Cushings$Type), function(x) {
    y <- subset(x, select =  -Type) })))
##       Tetrahydrocortisone Pregnanetriol
## a.a1                  3.1         11.70
## a.a2                  3.0          1.30
## a.a3                  1.9          0.10
## a.a4                  3.8          0.04
## a.a5                  4.1          1.10
## a.a6                  1.9          0.40
## b.b1                  8.3          1.00
## b.b2                  3.8          0.20
## b.b3                  3.9          0.60
## b.b4                  7.8          1.20
## b.b5                  9.1          0.60
## b.b6                 15.4          3.60
## b.b7                  7.7          1.60
## b.b8                  6.5          0.40
## b.b9                  5.7          0.40
## b.b10                13.6          1.60
## c.c1                 10.2          6.40
## c.c2                  9.2          7.90
## c.c3                  9.6          3.10
## c.c4                 53.8          2.50
## c.c5                 15.8          7.60
## u.u1                  5.1          0.40
## u.u2                 12.9          5.00
## u.u3                 13.0          0.80
## u.u4                  2.6          0.10
## u.u5                 30.0          0.10
## u.u6                 20.5          0.80
## if I delete -Type in  select , then type will display
do.call("rbind", as.list(
  by(Cushings, list(Cushings$Type), function(x) {
    y <- subset(x, select = ) })))
##       Tetrahydrocortisone Pregnanetriol Type
## a.a1                  3.1         11.70    a
## a.a2                  3.0          1.30    a
## a.a3                  1.9          0.10    a
## a.a4                  3.8          0.04    a
## a.a5                  4.1          1.10    a
## a.a6                  1.9          0.40    a
## b.b1                  8.3          1.00    b
## b.b2                  3.8          0.20    b
## b.b3                  3.9          0.60    b
## b.b4                  7.8          1.20    b
## b.b5                  9.1          0.60    b
## b.b6                 15.4          3.60    b
## b.b7                  7.7          1.60    b
## b.b8                  6.5          0.40    b
## b.b9                  5.7          0.40    b
## b.b10                13.6          1.60    b
## c.c1                 10.2          6.40    c
## c.c2                  9.2          7.90    c
## c.c3                  9.6          3.10    c
## c.c4                 53.8          2.50    c
## c.c5                 15.8          7.60    c
## u.u1                  5.1          0.40    u
## u.u2                 12.9          5.00    u
## u.u3                 13.0          0.80    u
## u.u4                  2.6          0.10    u
## u.u5                 30.0          0.10    u
## u.u6                 20.5          0.80    u
##Conclusion:The method 3 use rbind and list Cushings by Type,then use apply compute the mean


# method 4

## Application of dplyr
##Use group_by classfy the Cushings by type.Second,summarize it to compute mean

Cushings %>%
 group_by(Type) %>%
 summarize( t_m = mean(Tetrahydrocortisone), p_m = mean(Pregnanetriol))
## # A tibble: 4 x 3
##   Type    t_m   p_m
##   <fct> <dbl> <dbl>
## 1 a      2.97  2.44
## 2 b      8.18  1.12
## 3 c     19.7   5.5 
## 4 u     14.0   1.2
# method 5

##
##nest() creates a list of data frames containing all the nested variables
#try it to understand
a<-Cushings %>%
 nest(-Type)
## Warning: All elements of `...` must be named.
## Did you want `data = c(Tetrahydrocortisone, Pregnanetriol)`?
a
## # A tibble: 4 x 2
##   Type  data             
##   <fct> <list>           
## 1 a     <tibble [6 x 2]> 
## 2 b     <tibble [10 x 2]>
## 3 c     <tibble [5 x 2]> 
## 4 u     <tibble [6 x 2]>
#we can see the list by a
#
##mutate  add new variable by column

Cushings %>%
 nest(-Type) %>%
 mutate(avg = map(data, ~ apply(., 2, mean)), 
        res_1 = map_dbl(avg, "Tetrahydrocortisone"), 
        res_2 = map_dbl(avg, "Pregnanetriol")) 
## Warning: All elements of `...` must be named.
## Did you want `data = c(Tetrahydrocortisone, Pregnanetriol)`?
## # A tibble: 4 x 5
##   Type  data              avg       res_1 res_2
##   <fct> <list>            <list>    <dbl> <dbl>
## 1 a     <tibble [6 x 2]>  <dbl [2]>  2.97  2.44
## 2 b     <tibble [10 x 2]> <dbl [2]>  8.18  1.12
## 3 c     <tibble [5 x 2]>  <dbl [2]> 19.7   5.5 
## 4 u     <tibble [6 x 2]>  <dbl [2]> 14.0   1.2
###
#Conclusion,I think method 1 is great for me, because is simply for me
#I am not good at dplyr