The package will used in this example

library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(yihanfinalproject)

Read the data and input it into R.

data<- read.csv(file.choose(),header = TRUE)

data
##    participants gender age height weight.1..Before. weight.2..after.
## 1             1      M  35    181               102               92
## 2             2      F  20    186                85               76
## 3             3      M  14    170                85               75
## 4             4      F  75    164               104               94
## 5             5      M  60    182                84               75
## 6             6      F  88    164                88               80
## 7             7      M  43    157                90               82
## 8             8      F  22    156               103               93
## 9             9      M  10    112               117              107
## 10           10      F  22    167                80               72
## 11           11      M  20    162               115              110
## 12           12      F  55    153                85               79
## 13           13      F  65    171               113              108
## 14           14      M  55    165                91               87
## 15           15      F  45    167               120              115
## 16           16      M  42    173               108              103
## 17           17      M  44    183                82               79
## 18           18      F  49    169               103              100
## 19           19      F  87    155                91               89
## 20           20      M  78    164               102               99
## 21           21      F  32    158                98               95
## 22           22      F  12    140                91               88
## 23           23      F   7    124                28               23
## 24           24      M  15    155               108              105
## 25           25      M  88    164               109              107
## 26           26      M  60    172                96               94
## 27           27      F  73    155                99               96
## 28           28      M  43    160               104              101
## 29           29      F  33    163               119              116
## 30           30      M  59    175               114              111
## 31           31      M  34    178                96               96
## 32           32      F  57    170                82               82
## 33           33      M  65    170               107              107
## 34           34      F  24    160                80               80
## 35           35      M  22    169                93               93
## 36           36      M  17    173                97               97
## 37           37      F   9    128                23               23
## 38           38      F  60    154                78               78
## 39           39      M   5    103                20               20
## 40           40      F  33    171                99               99
##    drug_frequency
## 1               5
## 2               5
## 3               5
## 4               5
## 5               5
## 6               5
## 7               5
## 8               5
## 9               5
## 10              5
## 11              3
## 12              3
## 13              3
## 14              3
## 15              3
## 16              3
## 17              3
## 18              3
## 19              3
## 20              3
## 21              1
## 22              1
## 23              1
## 24              1
## 25              1
## 26              1
## 27              1
## 28              1
## 29              1
## 30              1
## 31              0
## 32              0
## 33              0
## 34              0
## 35              0
## 36              0
## 37              0
## 38              0
## 39              0
## 40              0

Calculate the two BMI(before & after) of participants by using function cal_bmi() and insert new column for the results

data$first_BMI <- cal_bmi(data$height, data$weight.1..Before.)
data$second_BMI <-cal_bmi(data$height, data$weight.2..after.)

data
##    participants gender age height weight.1..Before. weight.2..after.
## 1             1      M  35    181               102               92
## 2             2      F  20    186                85               76
## 3             3      M  14    170                85               75
## 4             4      F  75    164               104               94
## 5             5      M  60    182                84               75
## 6             6      F  88    164                88               80
## 7             7      M  43    157                90               82
## 8             8      F  22    156               103               93
## 9             9      M  10    112               117              107
## 10           10      F  22    167                80               72
## 11           11      M  20    162               115              110
## 12           12      F  55    153                85               79
## 13           13      F  65    171               113              108
## 14           14      M  55    165                91               87
## 15           15      F  45    167               120              115
## 16           16      M  42    173               108              103
## 17           17      M  44    183                82               79
## 18           18      F  49    169               103              100
## 19           19      F  87    155                91               89
## 20           20      M  78    164               102               99
## 21           21      F  32    158                98               95
## 22           22      F  12    140                91               88
## 23           23      F   7    124                28               23
## 24           24      M  15    155               108              105
## 25           25      M  88    164               109              107
## 26           26      M  60    172                96               94
## 27           27      F  73    155                99               96
## 28           28      M  43    160               104              101
## 29           29      F  33    163               119              116
## 30           30      M  59    175               114              111
## 31           31      M  34    178                96               96
## 32           32      F  57    170                82               82
## 33           33      M  65    170               107              107
## 34           34      F  24    160                80               80
## 35           35      M  22    169                93               93
## 36           36      M  17    173                97               97
## 37           37      F   9    128                23               23
## 38           38      F  60    154                78               78
## 39           39      M   5    103                20               20
## 40           40      F  33    171                99               99
##    drug_frequency first_BMI second_BMI
## 1               5  31.13458   28.08217
## 2               5  24.56931   21.96786
## 3               5  29.41176   25.95156
## 4               5  38.66746   34.94943
## 5               5  25.35926   22.64219
## 6               5  32.71862   29.74420
## 7               5  36.51264   33.26707
## 8               5  42.32413   38.21499
## 9               5  93.27168   85.29974
## 10              5  28.68514   25.81663
## 11              3  43.81954   41.91434
## 12              3  36.31082   33.74770
## 13              3  38.64437   36.93444
## 14              3  33.42516   31.95592
## 15              3  43.02772   41.23490
## 16              3  36.08540   34.41478
## 17              3  24.48565   23.58984
## 18              3  36.06316   35.01278
## 19              3  37.87721   37.04475
## 20              3  37.92385   36.80845
## 21              1  39.25653   38.05480
## 22              1  46.42857   44.89796
## 23              1  18.21020   14.95838
## 24              1  44.95317   43.70447
## 25              1  40.52647   39.78287
## 26              1  32.44997   31.77393
## 27              1  41.20708   39.95838
## 28              1  40.62500   39.45312
## 29              1  44.78904   43.65990
## 30              1  37.22449   36.24490
## 31              0  30.29920   30.29920
## 32              0  28.37370   28.37370
## 33              0  37.02422   37.02422
## 34              0  31.25000   31.25000
## 35              0  32.56189   32.56189
## 36              0  32.41004   32.41004
## 37              0  14.03809   14.03809
## 38              0  32.88919   32.88919
## 39              0  18.85192   18.85192
## 40              0  33.85657   33.85657

Check the qualify of each participants (the weight should use weight 1). Increase a column to show the results. After that, using filter() to filter the participants who are “Eligile”.

data$checkstate <- check_check(data$age, data$height, data$weight.1..Before.)

newdata <- filter(data, data$checkstate == "Eligile")

newdata
##    participants gender age height weight.1..Before. weight.2..after.
## 1             1      M  35    181               102               92
## 2             4      F  75    164               104               94
## 3             5      M  60    182                84               75
## 4             6      F  88    164                88               80
## 5             7      M  43    157                90               82
## 6             8      F  22    156               103               93
## 7            10      F  22    167                80               72
## 8            12      F  55    153                85               79
## 9            13      F  65    171               113              108
## 10           14      M  55    165                91               87
## 11           15      F  45    167               120              115
## 12           16      M  42    173               108              103
## 13           18      F  49    169               103              100
## 14           19      F  87    155                91               89
## 15           20      M  78    164               102               99
## 16           21      F  32    158                98               95
## 17           25      M  88    164               109              107
## 18           26      M  60    172                96               94
## 19           27      F  73    155                99               96
## 20           28      M  43    160               104              101
## 21           29      F  33    163               119              116
## 22           30      M  59    175               114              111
## 23           31      M  34    178                96               96
## 24           32      F  57    170                82               82
## 25           33      M  65    170               107              107
## 26           34      F  24    160                80               80
## 27           35      M  22    169                93               93
## 28           38      F  60    154                78               78
## 29           40      F  33    171                99               99
##    drug_frequency first_BMI second_BMI checkstate
## 1               5  31.13458   28.08217    Eligile
## 2               5  38.66746   34.94943    Eligile
## 3               5  25.35926   22.64219    Eligile
## 4               5  32.71862   29.74420    Eligile
## 5               5  36.51264   33.26707    Eligile
## 6               5  42.32413   38.21499    Eligile
## 7               5  28.68514   25.81663    Eligile
## 8               3  36.31082   33.74770    Eligile
## 9               3  38.64437   36.93444    Eligile
## 10              3  33.42516   31.95592    Eligile
## 11              3  43.02772   41.23490    Eligile
## 12              3  36.08540   34.41478    Eligile
## 13              3  36.06316   35.01278    Eligile
## 14              3  37.87721   37.04475    Eligile
## 15              3  37.92385   36.80845    Eligile
## 16              1  39.25653   38.05480    Eligile
## 17              1  40.52647   39.78287    Eligile
## 18              1  32.44997   31.77393    Eligile
## 19              1  41.20708   39.95838    Eligile
## 20              1  40.62500   39.45312    Eligile
## 21              1  44.78904   43.65990    Eligile
## 22              1  37.22449   36.24490    Eligile
## 23              0  30.29920   30.29920    Eligile
## 24              0  28.37370   28.37370    Eligile
## 25              0  37.02422   37.02422    Eligile
## 26              0  31.25000   31.25000    Eligile
## 27              0  32.56189   32.56189    Eligile
## 28              0  32.88919   32.88919    Eligile
## 29              0  33.85657   33.85657    Eligile

Summay each group(drug frequency) and input extra participants to each group have same participants

count(newdata, drug_frequency)
##   drug_frequency n
## 1              0 7
## 2              1 7
## 3              3 8
## 4              5 7
data2 <-read.csv(file.choose(),header = TRUE)
#calculate BMI(before & after)
data2$first_BMI <- cal_bmi(data2$height, data2$weight.1..Before.)
data2$second_BMI <-cal_bmi(data2$height, data2$weight.2..after.)
#check qualify
data2$checkstate <- check_check(data2$age, data2$height, data2$weight.1..Before.)

#combine two dataframe
total <- rbind(newdata, data2)

head(total)
##   participants gender age height weight.1..Before. weight.2..after.
## 1            1      M  35    181               102               92
## 2            4      F  75    164               104               94
## 3            5      M  60    182                84               75
## 4            6      F  88    164                88               80
## 5            7      M  43    157                90               82
## 6            8      F  22    156               103               93
##   drug_frequency first_BMI second_BMI checkstate
## 1              5  31.13458   28.08217    Eligile
## 2              5  38.66746   34.94943    Eligile
## 3              5  25.35926   22.64219    Eligile
## 4              5  32.71862   29.74420    Eligile
## 5              5  36.51264   33.26707    Eligile
## 6              5  42.32413   38.21499    Eligile
# organize dataframe
arrange(total,drug_frequency)
##    participants gender age height weight.1..Before. weight.2..after.
## 1            31      M  34    178                96               96
## 2            32      F  57    170                82               82
## 3            33      M  65    170               107              107
## 4            34      F  24    160                80               80
## 5            35      M  22    169                93               93
## 6            38      F  60    154                78               78
## 7            40      F  33    171                99               99
## 8            49      M  76    173               107              107
## 9            50      M  66    161                80               80
## 10           51      M  27    170                96               96
## 11           21      F  32    158                98               95
## 12           25      M  88    164               109              107
## 13           26      M  60    172                96               94
## 14           27      F  73    155                99               96
## 15           28      M  43    160               104              101
## 16           29      F  33    163               119              116
## 17           30      M  59    175               114              111
## 18           46      F  79    156                70               68
## 19           47      M  81    154                71               68
## 20           48      M  56    179               109              105
## 21           12      F  55    153                85               79
## 22           13      F  65    171               113              108
## 23           14      M  55    165                91               87
## 24           15      F  45    167               120              115
## 25           16      M  42    173               108              103
## 26           18      F  49    169               103              100
## 27           19      F  87    155                91               89
## 28           20      M  78    164               102               99
## 29           44      F  32    166                80               75
## 30           45      M  56    173                95               90
## 31            1      M  35    181               102               92
## 32            4      F  75    164               104               94
## 33            5      M  60    182                84               75
## 34            6      F  88    164                88               80
## 35            7      M  43    157                90               82
## 36            8      F  22    156               103               93
## 37           10      F  22    167                80               72
## 38           41      M  47    182               110              100
## 39           42      F  26    185               115              105
## 40           43      M  37    171                95               85
##    drug_frequency first_BMI second_BMI checkstate
## 1               0  30.29920   30.29920    Eligile
## 2               0  28.37370   28.37370    Eligile
## 3               0  37.02422   37.02422    Eligile
## 4               0  31.25000   31.25000    Eligile
## 5               0  32.56189   32.56189    Eligile
## 6               0  32.88919   32.88919    Eligile
## 7               0  33.85657   33.85657    Eligile
## 8               0  35.75128   35.75128    Eligile
## 9               0  30.86301   30.86301    Eligile
## 10              0  33.21799   33.21799    Eligile
## 11              1  39.25653   38.05480    Eligile
## 12              1  40.52647   39.78287    Eligile
## 13              1  32.44997   31.77393    Eligile
## 14              1  41.20708   39.95838    Eligile
## 15              1  40.62500   39.45312    Eligile
## 16              1  44.78904   43.65990    Eligile
## 17              1  37.22449   36.24490    Eligile
## 18              1  28.76397   27.94214    Eligile
## 19              1  29.93759   28.67263    Eligile
## 20              1  34.01891   32.77051    Eligile
## 21              3  36.31082   33.74770    Eligile
## 22              3  38.64437   36.93444    Eligile
## 23              3  33.42516   31.95592    Eligile
## 24              3  43.02772   41.23490    Eligile
## 25              3  36.08540   34.41478    Eligile
## 26              3  36.06316   35.01278    Eligile
## 27              3  37.87721   37.04475    Eligile
## 28              3  37.92385   36.80845    Eligile
## 29              3  29.03179   27.21730    Eligile
## 30              3  31.74179   30.07117    Eligile
## 31              5  31.13458   28.08217    Eligile
## 32              5  38.66746   34.94943    Eligile
## 33              5  25.35926   22.64219    Eligile
## 34              5  32.71862   29.74420    Eligile
## 35              5  36.51264   33.26707    Eligile
## 36              5  42.32413   38.21499    Eligile
## 37              5  28.68514   25.81663    Eligile
## 38              5  33.20855   30.18959    Eligile
## 39              5  33.60117   30.67933    Eligile
## 40              5  32.48863   29.06877    Eligile

Use total(dataset) to see the different results by different varies. First, to see the BMI distribution of participants of different ages by using finction plotage(). And using plotage_separate() to see in different drug frequency, the age distribution of participants.

Before1 <- plotage(total$age,total$height, total$weight.1..Before., total$drug_frequency)
After1 <- plotage(total$age, total$height, total$weight.2..after.,total$drug_frequency)

Before1

After1

Before_separte1 <- plotage_separate(total, total$age,total$height, total$weight.1..Before., total$drug_frequency)
After_separte1 <- plotage_separate(total, total$age, total$height, total$weight.2..after.,total$drug_frequency)

Before_separte1

After_separte1

Using plotgender() and plotgender_separate() to see the different results by gender.

genderbefore <-plotgender(total$age, total$height, total$weight.1..Before.)
genderafter <- plotgender(total$age, total$height, total$weight.2..after.)

genderbefore

genderafter

# using plotgender_separate() to separate in different plots

genderbeforese <-plotgender_separate(total, total$age, total$height, total$weight.1..Before., total$gender)
genderafterse <- plotgender_separate(total,total$age, total$height, total$weight.2..after., total$gender )

genderbeforese

genderafterse