data("sleep")
#read data
sleep
##    extra group ID
## 1    0.7     1  1
## 2   -1.6     1  2
## 3   -0.2     1  3
## 4   -1.2     1  4
## 5   -0.1     1  5
## 6    3.4     1  6
## 7    3.7     1  7
## 8    0.8     1  8
## 9    0.0     1  9
## 10   2.0     1 10
## 11   1.9     2  1
## 12   0.8     2  2
## 13   1.1     2  3
## 14   0.1     2  4
## 15  -0.1     2  5
## 16   4.4     2  6
## 17   5.5     2  7
## 18   1.6     2  8
## 19   4.6     2  9
## 20   3.4     2 10
names(sleep)
## [1] "extra" "group" "ID"

The data shows the effect of two drugs by the increasment of sleeping hours on 10 participants. The data contains total 20 observations on three variables(extra, group, ID). For the group variable, 1 = control group and 2 = treatment group, each contains 10 participants. For the ID variable, ID 1 to 10 belong to control group, and ID 11 to 20 belong to treatment group.

#structure
str(sleep)
## 'data.frame':    20 obs. of  3 variables:
##  $ extra: num  0.7 -1.6 -0.2 -1.2 -0.1 3.4 3.7 0.8 0 2 ...
##  $ group: Factor w/ 2 levels "1","2": 1 1 1 1 1 1 1 1 1 1 ...
##  $ ID   : Factor w/ 10 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
#extra sleeping hours
summary(sleep$extra)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  -1.600  -0.025   0.950   1.540   3.400   5.500
#rename sleep
sp <- sleep
#偏態&峰度(num variable)
kurtosis(sp$extra)
## [1] 2.120121
skewness(sp$extra)
## [1] 0.4172363
#mean of extra sleeping hours of each group
aggregate(extra ~ group, data = sp, FUN = mean)
##   group extra
## 1     1  0.75
## 2     2  2.33
#sd of extra sleeping hours of each group
aggregate(extra ~ group, data = sp, sd)
##   group    extra
## 1     1 1.789010
## 2     2 2.002249
#boxplot of each group
boxplot(formula = extra ~ group, # the formula
 data = sp,                      
 xlab = "group1=control, group2=treatment",
 ylab = "extra sleeping hours",   
 frame.plot = FALSE,   
 boxfill = "grey90",     
 whisklty = 1            
)

透過boxplot可見兩組都沒有outlier

t.test(extra ~ group, data = sp)
## 
##  Welch Two Sample t-test
## 
## data:  extra by group
## t = -1.8608, df = 17.776, p-value = 0.07939
## alternative hypothesis: true difference in means between group 1 and group 2 is not equal to 0
## 95 percent confidence interval:
##  -3.3654832  0.2054832
## sample estimates:
## mean in group 1 mean in group 2 
##            0.75            2.33

Null hypothesis H₀= Treatment group - Control group = 0 Alternative hypothesis H₁:Treatment group - Control group ≠ 0

T-Value = -1.8608, df = 17.776, p-value = 0.07939

In these results, the null hypothesis states that the difference in the mean of extra sleeping hour between two group is 0. Because the p-value is 0.07939, which is not less than the significance level of 0.05, the decision is not to reject the null hypothesis and conclude that there are no significant differences between the control group and treatment group. In other words, the effect of two drugs is not significant.