ALL SLIDE

slide 01 (Introduction to R)

n<-("welcome")
print(n)
## [1] "welcome"
1+2+3+4+5
## [1] 15
r <-(10^2)*3
print(r)
## [1] 300
x <- rnorm(5)
x
## [1] -0.2150953 -1.1677641  1.3404319 -0.6383681  0.4801132

Vector

x<-c(0,1,1,2,3,5,6,13,21,34,55)
print(x)
##  [1]  0  1  1  2  3  5  6 13 21 34 55

Matrix

testdata <- matrix(c(1,1,1,2,2,3,3,3,10,11,12,13,
14,15,16,17,28,30,25,40,50,47,62,23), ncol=3,
byrow=FALSE)
testdata
##      [,1] [,2] [,3]
## [1,]    1   10   28
## [2,]    1   11   30
## [3,]    1   12   25
## [4,]    2   13   40
## [5,]    2   14   50
## [6,]    3   15   47
## [7,]    3   16   62
## [8,]    3   17   23
age <- c(1, 3, 5, 2, 11, 9, 3, 9, 12, 3)
weight <- c(4.4, 5.3, 7.2, 5.2, 8.5, 7.3, 6.0, 10.4, 10.2, 6.1)
mean(weight)
## [1] 7.06
sd(weight)
## [1] 2.077498
cor(age, weight)
## [1] 0.9075655
plot(age, weight)

help

help.start()
## starting httpd help server ... done
## If nothing happens, you should open
## 'http://127.0.0.1:21228/doc/html/index.html' yourself
example(glm)
## 
## glm> ## Dobson (1990) Page 93: Randomized Controlled Trial :
## glm> counts <- c(18,17,15,20,10,20,25,13,12)
## 
## glm> outcome <- gl(3,1,9)
## 
## glm> treatment <- gl(3,3)
## 
## glm> data.frame(treatment, outcome, counts) # showing data
##   treatment outcome counts
## 1         1       1     18
## 2         1       2     17
## 3         1       3     15
## 4         2       1     20
## 5         2       2     10
## 6         2       3     20
## 7         3       1     25
## 8         3       2     13
## 9         3       3     12
## 
## glm> glm.D93 <- glm(counts ~ outcome + treatment, family = poisson())
## 
## glm> anova(glm.D93)
## Analysis of Deviance Table
## 
## Model: poisson, link: log
## 
## Response: counts
## 
## Terms added sequentially (first to last)
## 
## 
##           Df Deviance Resid. Df Resid. Dev
## NULL                          8    10.5814
## outcome    2   5.4523         6     5.1291
## treatment  2   0.0000         4     5.1291
## 
## glm> ## No test: 
## glm> ##D summary(glm.D93)
## glm> ## End(No test)
## glm> ## Computing AIC [in many ways]:
## glm> (A0 <- AIC(glm.D93))
## [1] 56.76132
## 
## glm> (ll <- logLik(glm.D93))
## 'log Lik.' -23.38066 (df=5)
## 
## glm> A1 <- -2*c(ll) + 2*attr(ll, "df")
## 
## glm> A2 <- glm.D93$family$aic(counts, mu=fitted(glm.D93), wt=1) +
## glm+         2 * length(coef(glm.D93))
## 
## glm> stopifnot(exprs = {
## glm+   all.equal(A0, A1)
## glm+   all.equal(A1, A2)
## glm+   all.equal(A1, glm.D93$aic)
## glm+ })
## 
## glm> ## No test: 
## glm> ##D ## an example with offsets from Venables & Ripley (2002, p.189)
## glm> ##D utils::data(anorexia, package = "MASS")
## glm> ##D 
## glm> ##D anorex.1 <- glm(Postwt ~ Prewt + Treat + offset(Prewt),
## glm> ##D                 family = gaussian, data = anorexia)
## glm> ##D summary(anorex.1)
## glm> ## End(No test)
## glm> 
## glm> # A Gamma example, from McCullagh & Nelder (1989, pp. 300-2)
## glm> clotting <- data.frame(
## glm+     u = c(5,10,15,20,30,40,60,80,100),
## glm+     lot1 = c(118,58,42,35,27,25,21,19,18),
## glm+     lot2 = c(69,35,26,21,18,16,13,12,12))
## 
## glm> summary(glm(lot1 ~ log(u), data = clotting, family = Gamma))
## 
## Call:
## glm(formula = lot1 ~ log(u), family = Gamma, data = clotting)
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -0.04008  -0.03756  -0.02637   0.02905   0.08641  
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -0.0165544  0.0009275  -17.85 4.28e-07 ***
## log(u)       0.0153431  0.0004150   36.98 2.75e-09 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for Gamma family taken to be 0.002446059)
## 
##     Null deviance: 3.51283  on 8  degrees of freedom
## Residual deviance: 0.01673  on 7  degrees of freedom
## AIC: 37.99
## 
## Number of Fisher Scoring iterations: 3
## 
## 
## glm> summary(glm(lot2 ~ log(u), data = clotting, family = Gamma))
## 
## Call:
## glm(formula = lot2 ~ log(u), family = Gamma, data = clotting)
## 
## Deviance Residuals: 
##      Min        1Q    Median        3Q       Max  
## -0.05574  -0.02925   0.01030   0.01714   0.06371  
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -0.0239085  0.0013265  -18.02 4.00e-07 ***
## log(u)       0.0235992  0.0005768   40.91 1.36e-09 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for Gamma family taken to be 0.001813354)
## 
##     Null deviance: 3.118557  on 8  degrees of freedom
## Residual deviance: 0.012672  on 7  degrees of freedom
## AIC: 27.032
## 
## Number of Fisher Scoring iterations: 3
## 
## 
## glm> ## Aliased ("S"ingular) -> 1 NA coefficient
## glm> (fS <- glm(lot2 ~ log(u) + log(u^2), data = clotting, family = Gamma))
## 
## Call:  glm(formula = lot2 ~ log(u) + log(u^2), family = Gamma, data = clotting)
## 
## Coefficients:
## (Intercept)       log(u)     log(u^2)  
##    -0.02391      0.02360           NA  
## 
## Degrees of Freedom: 8 Total (i.e. Null);  7 Residual
## Null Deviance:       3.119 
## Residual Deviance: 0.01267   AIC: 27.03
## 
## glm> tools::assertError(update(fS, singular.ok=FALSE), verbose=interactive())
## 
## glm> ## -> .. "singular fit encountered"
## glm> 
## glm> ## Not run: 
## glm> ##D ## for an example of the use of a terms object as a formula
## glm> ##D demo(glm.vr)
## glm> ## End(Not run)
## glm> 
## glm>
example("regression")
## Warning in example("regression"): no help found for 'regression'
example(lm)
## 
## lm> require(graphics)
## 
## lm> ## Annette Dobson (1990) "An Introduction to Generalized Linear Models".
## lm> ## Page 9: Plant Weight Data.
## lm> ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
## 
## lm> trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
## 
## lm> group <- gl(2, 10, 20, labels = c("Ctl","Trt"))
## 
## lm> weight <- c(ctl, trt)
## 
## lm> lm.D9 <- lm(weight ~ group)
## 
## lm> lm.D90 <- lm(weight ~ group - 1) # omitting intercept
## 
## lm> ## No test: 
## lm> ##D anova(lm.D9)
## lm> ##D summary(lm.D90)
## lm> ## End(No test)
## lm> opar <- par(mfrow = c(2,2), oma = c(0, 0, 1.1, 0))
## 
## lm> plot(lm.D9, las = 1)      # Residuals, Fitted, ...

## 
## lm> par(opar)
## 
## lm> ## Don't show: 
## lm> ## model frame :
## lm> stopifnot(identical(lm(weight ~ group, method = "model.frame"),
## lm+                     model.frame(lm.D9)))
## 
## lm> ## End(Don't show)
## lm> ### less simple examples in "See Also" above
## lm> 
## lm> 
## lm>
data()
data(cars)
cars
##    speed dist
## 1      4    2
## 2      4   10
## 3      7    4
## 4      7   22
## 5      8   16
## 6      9   10
## 7     10   18
## 8     10   26
## 9     10   34
## 10    11   17
## 11    11   28
## 12    12   14
## 13    12   20
## 14    12   24
## 15    12   28
## 16    13   26
## 17    13   34
## 18    13   34
## 19    13   46
## 20    14   26
## 21    14   36
## 22    14   60
## 23    14   80
## 24    15   20
## 25    15   26
## 26    15   54
## 27    16   32
## 28    16   40
## 29    17   32
## 30    17   40
## 31    17   50
## 32    18   42
## 33    18   56
## 34    18   76
## 35    18   84
## 36    19   36
## 37    19   46
## 38    19   68
## 39    20   32
## 40    20   48
## 41    20   52
## 42    20   56
## 43    20   64
## 44    22   66
## 45    23   54
## 46    24   70
## 47    24   92
## 48    24   93
## 49    24  120
## 50    25   85
data(Orange)
Orange
##    Tree  age circumference
## 1     1  118            30
## 2     1  484            58
## 3     1  664            87
## 4     1 1004           115
## 5     1 1231           120
## 6     1 1372           142
## 7     1 1582           145
## 8     2  118            33
## 9     2  484            69
## 10    2  664           111
## 11    2 1004           156
## 12    2 1231           172
## 13    2 1372           203
## 14    2 1582           203
## 15    3  118            30
## 16    3  484            51
## 17    3  664            75
## 18    3 1004           108
## 19    3 1231           115
## 20    3 1372           139
## 21    3 1582           140
## 22    4  118            32
## 23    4  484            62
## 24    4  664           112
## 25    4 1004           167
## 26    4 1231           179
## 27    4 1372           209
## 28    4 1582           214
## 29    5  118            30
## 30    5  484            49
## 31    5  664            81
## 32    5 1004           125
## 33    5 1231           142
## 34    5 1372           174
## 35    5 1582           177
help(lm)
RSiteSearch("lm")
## A search query has been submitted to https://search.r-project.org
## The results page should open in your browser shortly
??regression

save image

An example of commands used to manage the R workspace

setwd("D:\\2nd Year\\AST 230")
x <- runif(20)
summary(x)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
## 0.02316 0.18119 0.33347 0.43033 0.65301 0.93441
hist(x)

#savehistory()
save.image()

Installing a package

#install.packages("vcd")
help(package="vcd")
library(vcd)
## Warning: package 'vcd' was built under R version 4.2.2
## Loading required package: grid
help(Arthritis)
Arthritis
##    ID Treatment    Sex Age Improved
## 1  57   Treated   Male  27     Some
## 2  46   Treated   Male  29     None
## 3  77   Treated   Male  30     None
## 4  17   Treated   Male  32   Marked
## 5  36   Treated   Male  46   Marked
## 6  23   Treated   Male  58   Marked
## 7  75   Treated   Male  59     None
## 8  39   Treated   Male  59   Marked
## 9  33   Treated   Male  63     None
## 10 55   Treated   Male  63     None
## 11 30   Treated   Male  64     None
## 12  5   Treated   Male  64     Some
## 13 63   Treated   Male  69     None
## 14 83   Treated   Male  70   Marked
## 15 66   Treated Female  23     None
## 16 40   Treated Female  32     None
## 17  6   Treated Female  37     Some
## 18  7   Treated Female  41     None
## 19 72   Treated Female  41   Marked
## 20 37   Treated Female  48     None
## 21 82   Treated Female  48   Marked
## 22 53   Treated Female  55   Marked
## 23 79   Treated Female  55   Marked
## 24 26   Treated Female  56   Marked
## 25 28   Treated Female  57   Marked
## 26 60   Treated Female  57   Marked
## 27 22   Treated Female  57   Marked
## 28 27   Treated Female  58     None
## 29  2   Treated Female  59   Marked
## 30 59   Treated Female  59   Marked
## 31 62   Treated Female  60   Marked
## 32 84   Treated Female  61   Marked
## 33 64   Treated Female  62     Some
## 34 34   Treated Female  62   Marked
## 35 58   Treated Female  66   Marked
## 36 13   Treated Female  67   Marked
## 37 61   Treated Female  68     Some
## 38 65   Treated Female  68   Marked
## 39 11   Treated Female  69     None
## 40 56   Treated Female  69     Some
## 41 43   Treated Female  70     Some
## 42  9   Placebo   Male  37     None
## 43 14   Placebo   Male  44     None
## 44 73   Placebo   Male  50     None
## 45 74   Placebo   Male  51     None
## 46 25   Placebo   Male  52     None
## 47 18   Placebo   Male  53     None
## 48 21   Placebo   Male  59     None
## 49 52   Placebo   Male  59     None
## 50 45   Placebo   Male  62     None
## 51 41   Placebo   Male  62     None
## 52  8   Placebo   Male  63   Marked
## 53 80   Placebo Female  23     None
## 54 12   Placebo Female  30     None
## 55 29   Placebo Female  30     None
## 56 50   Placebo Female  31     Some
## 57 38   Placebo Female  32     None
## 58 35   Placebo Female  33   Marked
## 59 51   Placebo Female  37     None
## 60 54   Placebo Female  44     None
## 61 76   Placebo Female  45     None
## 62 16   Placebo Female  46     None
## 63 69   Placebo Female  48     None
## 64 31   Placebo Female  49     None
## 65 20   Placebo Female  51     None
## 66 68   Placebo Female  53     None
## 67 81   Placebo Female  54     None
## 68  4   Placebo Female  54     None
## 69 78   Placebo Female  54   Marked
## 70 70   Placebo Female  55   Marked
## 71 49   Placebo Female  57     None
## 72 10   Placebo Female  57     Some
## 73 47   Placebo Female  58     Some
## 74 44   Placebo Female  59     Some
## 75 24   Placebo Female  59   Marked
## 76 48   Placebo Female  61     None
## 77 19   Placebo Female  63     Some
## 78  3   Placebo Female  64     None
## 79 67   Placebo Female  65   Marked
## 80 32   Placebo Female  66     None
## 81 42   Placebo Female  66     None
## 82 15   Placebo Female  66     Some
## 83 71   Placebo Female  68     Some
## 84  1   Placebo Female  74   Marked
example(Arthritis)
## 
## Arthrt> data("Arthritis")
## 
## Arthrt> art <- xtabs(~ Treatment + Improved, data = Arthritis, subset = Sex == "Female")
## 
## Arthrt> art
##          Improved
## Treatment None Some Marked
##   Placebo   19    7      6
##   Treated    6    5     16
## 
## Arthrt> mosaic(art, gp = shading_Friendly)

## 
## Arthrt> mosaic(art, gp = shading_max)

slide 02 (Creating Dataset)

vectors

a<-c(1,2,5,3,6,-2,4)
a[3]
## [1] 5
a[c(1,3,5)]
## [1] 1 5 6
a[2:6]
## [1]  2  5  3  6 -2
b<-c("one","two","three")
c<-c(TRUE,TRUE,TRUE,FALSE,TRUE,FALSE)
b
## [1] "one"   "two"   "three"
c
## [1]  TRUE  TRUE  TRUE FALSE  TRUE FALSE

matrices

mymatrix <- matrix(vector, nrow=number_of_rows,ncol=number_of_columns, byrow=logical_value,dimnames=list(char_vector_rownames, char_vector_colnames))

y<-matrix(1:20,nrow=5,ncol=4)
y
##      [,1] [,2] [,3] [,4]
## [1,]    1    6   11   16
## [2,]    2    7   12   17
## [3,]    3    8   13   18
## [4,]    4    9   14   19
## [5,]    5   10   15   20
cells<-c(1,26,24,68)
rnames<-c("R1","R2")
cnames <- c("C1", "C2")
mymatrix <- matrix(cells, nrow=2, ncol=2, byrow=TRUE, dimnames=list(rnames, cnames))
mymatrix
##    C1 C2
## R1  1 26
## R2 24 68

Array

myarray <- array(vector, dimensions, dimnames)

dim1 <- c("A1", "A2")
dim2 <- c("B1", "B2", "B3")
dim3 <- c("C1", "C2", "C3", "C4")
z <- array(1:48, c(2, 3, 4), dimnames=list(dim1, dim2, dim3))
z
## , , C1
## 
##    B1 B2 B3
## A1  1  3  5
## A2  2  4  6
## 
## , , C2
## 
##    B1 B2 B3
## A1  7  9 11
## A2  8 10 12
## 
## , , C3
## 
##    B1 B2 B3
## A1 13 15 17
## A2 14 16 18
## 
## , , C4
## 
##    B1 B2 B3
## A1 19 21 23
## A2 20 22 24

Creating a data frame

patientID <- c(1, 2, 3, 4)
age <- c(25, 34, 28, 52)
diabetes <- c("Type1", "Type2", "Type1", "Type1")
status <- c("Poor", "Improved", "Excellent", "Poor")
patientdata <- data.frame(patientID, age, diabetes, status)
patientdata
##   patientID age diabetes    status
## 1         1  25    Type1      Poor
## 2         2  34    Type2  Improved
## 3         3  28    Type1 Excellent
## 4         4  52    Type1      Poor
patientdata[1:2]
##   patientID age
## 1         1  25
## 2         2  34
## 3         3  28
## 4         4  52
patientdata[c("diabetes", "status")]
##   diabetes    status
## 1    Type1      Poor
## 2    Type2  Improved
## 3    Type1 Excellent
## 4    Type1      Poor
patientdata$age
## [1] 25 34 28 52

without ATTACH, DETACH

mtcars
##                      mpg cyl  disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
## Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
## Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
## Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
## Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
## Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
## Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
## Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
## Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
## Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
## Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
## Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
## Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
## Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
## Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
## Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
## Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
## Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
## AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
## Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
## Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
## Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
## Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
## Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
## Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
## Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
## Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
## Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
summary(mtcars$mpg)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   10.40   15.43   19.20   20.09   22.80   33.90
plot(mtcars$mpg, mtcars$disp)

plot(mtcars$mpg, mtcars$wt)

ATTACH, DETACH

attach(mtcars)
summary(mpg)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   10.40   15.43   19.20   20.09   22.80   33.90
plot(mpg, disp)

plot(mpg, wt)

detach(mtcars)

more than one object can have the same name

mpg <- c(25, 36, 47) attach(mtcars) plot(mpg, wt) Error in xy.coords(x, y, xlabel, ylabel, log) : ‘x’ and ‘y’ lengths differ > mpg 1 25 36 47

WITH function

An alternative approach is to use the with() function.

with(mtcars, {
summary(mpg,disp,wt)
plot(mpg, disp)
plot(mpg, wt)
})

local environment

with(mtcars, {
stats <- summary(mpg)
stats
})
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   10.40   15.43   19.20   20.09   22.80   33.90

stats Error: object ‘stats’ not found

global environment

save the object to the global environment outside ofthe with() call by using <<-

with(mtcars, {
nokeepstats <- summary(mpg)
keepstats <<- summary(mpg)
})
keepstats
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   10.40   15.43   19.20   20.09   22.80   33.90

nokeepstats Error: object ‘nokeepstats’ not found

Lists

g <- "My First List"
h <- c(25, 26, 18, 39)
j <- matrix(1:10, nrow=5)
k <- c("one", "two", "three")
mylist <- list(title=g, ages=h, j, k)
mylist
## $title
## [1] "My First List"
## 
## $ages
## [1] 25 26 18 39
## 
## [[3]]
##      [,1] [,2]
## [1,]    1    6
## [2,]    2    7
## [3,]    3    8
## [4,]    4    9
## [5,]    5   10
## 
## [[4]]
## [1] "one"   "two"   "three"
#individually
mylist[[1]]
## [1] "My First List"
mylist[[2]]
## [1] 25 26 18 39
mylist[[3]]
##      [,1] [,2]
## [1,]    1    6
## [2,]    2    7
## [3,]    3    8
## [4,]    4    9
## [5,]    5   10
mylist[[4]]
## [1] "one"   "two"   "three"

create a data frame

create a data frame named mydata with three variables: age (numeric) , gender (character) , and weight (numeric)

mydata <- data.frame(age=numeric(0),
gender=character(0), weight=numeric(0))

edit data

mydata <- edit(mydata)

read text file

mydataframe <- read.table(file,header=logical_value,sep=“delimiter”, row.names=“name”)

income <- read.table("D://2nd Year//AST 230//EXCELL PRACTICE2.txt", header=T)
View(income)

read.xlsx(file,n)

library(xlsx) mydataframe <- read.xlsx(“D://2nd Year//AST 230//EXCELL PRACTICE2.xlsx”, 1)

Importing data from SPSS

1st way

#install.packages(“Hmisc”)

library(Hmisc) mydataframe <- spss.get(“mydata.sav”, use.value.labels=TRUE)

2nd way

library( foreign) testdata=read.spss(“C:/Users/ SNA.sav”, use.value.labels=TRUE, to.data.frame=TRUE)

Importing data from Stata

library(foreign) mydataframe <- read.dta(“mydata.dta”)

#after importing data from stata,sas,etc

example

patientID <- c(1, 2, 3, 4)
age <- c(25, 34, 28, 52)
gender<-c(1,1,2,1)
diabetes <- c("Type1", "Type2", "Type1", "Type1")
status <- c("Poor", "Improved", "Excellent", "Poor")
patientdata <- data.frame(patientID, age, gender, diabetes, status)
patientdata
##   patientID age gender diabetes    status
## 1         1  25      1    Type1      Poor
## 2         2  34      1    Type2  Improved
## 3         3  28      2    Type1 Excellent
## 4         4  52      1    Type1      Poor

Rename coloum title (age)

names(patientdata)[2] <- "Age at hospitalization (in
years)"
patientdata
##   patientID Age at hospitalization (in\nyears) gender diabetes    status
## 1         1                                 25      1    Type1      Poor
## 2         2                                 34      1    Type2  Improved
## 3         3                                 28      2    Type1 Excellent
## 4         4                                 52      1    Type1      Poor

value lables(coding)

patientdata
##   patientID Age at hospitalization (in\nyears) gender diabetes    status
## 1         1                                 25      1    Type1      Poor
## 2         2                                 34      1    Type2  Improved
## 3         3                                 28      2    Type1 Excellent
## 4         4                                 52      1    Type1      Poor
patientdata$gender <- factor(patientdata$gender,
levels = c(1,2),
labels = c("male", "female"))
patientdata
##   patientID Age at hospitalization (in\nyears) gender diabetes    status
## 1         1                                 25   male    Type1      Poor
## 2         2                                 34   male    Type2  Improved
## 3         3                                 28 female    Type1 Excellent
## 4         4                                 52   male    Type1      Poor

slide 03 (working with graphs)

Regression line

rm(list = ls())
show(mtcars)
##                      mpg cyl  disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
## Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
## Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
## Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
## Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
## Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
## Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
## Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
## Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
## Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
## Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
## Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
## Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
## Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
## Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
## Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
## Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
## Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
## AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
## Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
## Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
## Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
## Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
## Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
## Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
## Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
## Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
## Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
attach(mtcars)
plot(wt, mpg)
lm(mpg~wt)
## 
## Call:
## lm(formula = mpg ~ wt)
## 
## Coefficients:
## (Intercept)           wt  
##      37.285       -5.344
abline(lm(mpg~wt))
title("Regression of MPG on Weight")

detach(mtcars)

save pdf

pdf("mygraph11.pdf")
attach(mtcars)
plot(wt, mpg)
abline(lm(mpg~wt))
title("Regression of MPG on Weight")
detach(mtcars)
dev.off()
## png 
##   2
# Finally, close the file using the dev.off function.

Example

dose <- c(20, 30, 40, 45, 60)
drugA <- c(16, 20, 27, 40, 60)
drugB <- c(15, 18, 25, 31, 40)

plot(dose, drugA, type="b")

resulting graph

opar <- par(no.readonly=TRUE)
par(lty=2, pch=17)
plot(dose, drugA, type="b")

par(opar)

#For example, all graphs created after the statement par(font.lab=3, cex.lab=1.5, font.main=4, cex.main=2) will have italic axis labels that are 1.5 times the default text size, and bold italic titles that are twice the default text size.

#The code par(pin=c(4,3), mai=c(1,.5, 1, .2)) produces graphs that are 4 inches wide by 3 inches tall, with a 1-inch margin on the bottom and top, a 0.5-inch margin on the left, and a 0.2-inch margin on the right.

using graping parameter

dose <- c(20, 30, 40, 45, 60)
drugA <- c(16, 20, 27, 40, 60)
drugB <- c(15, 18, 25, 31, 40)
opar <- par(no.readonly=TRUE)
#2 inches wide by 3 inches tall(pin)
par(pin=c(2, 3))
#The line width(lwd)
#symbols width (cex)
par(lwd=2, cex=1.5)
#axis annotation(15,20,25...) size (cex.axis)
#axis annotation(15,20,25...) font (font.axis)
par(cex.axis=.75, font.axis=2)
plot(dose, drugA, type="b", pch=19, lty=2, col="red")

#bg>background 
plot(dose, drugB, type="b", pch=23, lty=6, col="blue",
bg="green")

par(opar)

Comparing graps

dose <- c(20, 30, 40, 45, 60)
drugA <- c(16, 20, 27, 40, 60)
drugB <- c(15, 18, 25, 31, 40)
opar <- par(no.readonly=TRUE)
par(lwd=2, cex=1.5, font.lab=2)
plot(dose, drugA, type="b",
     pch=15, lty=1, col="red", ylim=c(0, 60),
     main="Drug A vs. Drug B",
     xlab="Drug Dosage", ylab="Drug Response")
lines(dose, drugB, type="b",
      pch=17, lty=2, col="blue")
abline(h=c(30), lwd=1.5, lty=2, col="gray")
library(Hmisc)
## Warning: package 'Hmisc' was built under R version 4.2.2
## Loading required package: lattice
## Loading required package: survival
## Loading required package: Formula
## Warning: package 'Formula' was built under R version 4.2.2
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.2.2
## 
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:base':
## 
##     format.pval, units
minor.tick(nx=1, ny=1, tick.ratio=0.5)
legend("topleft", inset=.01, title="Drug Type", c("A","B"),
       lty=c(1, 2), pch=c(15, 17), col=c("red", "blue"))

par(opar)

slide 04 (Basic Data Management)

Creating new variables

mydata<-data.frame(x1 = c(2, 2, 6, 4),
x2 = c(3, 4, 2, 8))
mydata
##   x1 x2
## 1  2  3
## 2  2  4
## 3  6  2
## 4  4  8

1st way

mydata$sumx <- mydata$x1 + mydata$x2
mydata$meanx <- (mydata$x1 + mydata$x2)/2
mydata
##   x1 x2 sumx meanx
## 1  2  3    5   2.5
## 2  2  4    6   3.0
## 3  6  2    8   4.0
## 4  4  8   12   6.0

2nd way

attach(mydata)
mydata$sumx <- x1 + x2
mydata$meanx <- (x1 + x2)/2
detach(mydata)
mydata
##   x1 x2 sumx meanx
## 1  2  3    5   2.5
## 2  2  4    6   3.0
## 3  6  2    8   4.0
## 4  4  8   12   6.0

3rd way

mydata2<- transform(mydata,
sumx = x1 + x2,
meanx = (x1 + x2)/2)
mydata2
##   x1 x2 sumx meanx
## 1  2  3    5   2.5
## 2  2  4    6   3.0
## 3  6  2    8   4.0
## 4  4  8   12   6.0

Recoding variables

manager <- c(1, 2, 3, 4, 5)
date <- c("10/24/08", "10/28/08", "10/1/08", "10/12/08", "5/1/09")
country <- c("US", "US", "UK", "UK", "UK")
gender <- c("M", "F", "F", "M", "F")
age <- c(32, 45, 25, 39, 99)
q1 <- c(5, 3, 3, 3, 2)
q2 <- c(4, 5, 5, 3, 2)
q3 <- c(5, 2, 5, 4, 1)
q4 <- c(5, 5, 5, NA, 2)
q5 <- c(5, 5, 2, NA, 1)
leadership <<- data.frame(manager, date, country, gender, age,q1, q2,
q3, q4, q5, stringsAsFactors=FALSE)
leadership
##   manager     date country gender age q1 q2 q3 q4 q5
## 1       1 10/24/08      US      M  32  5  4  5  5  5
## 2       2 10/28/08      US      F  45  3  5  2  5  5
## 3       3  10/1/08      UK      F  25  3  5  5  5  2
## 4       4 10/12/08      UK      M  39  3  3  4 NA NA
## 5       5   5/1/09      UK      F  99  2  2  1  2  1

1st way

leadership$agecat[leadership$age == 99] <- NA
leadership$agecat[leadership$age > 75] <- "Elder"
leadership$agecat[leadership$age >= 55 & leadership$age <= 75] <- "Middle Aged"
leadership$agecat[leadership$age < 55] <- "Young" 

leadership
##   manager     date country gender age q1 q2 q3 q4 q5 agecat
## 1       1 10/24/08      US      M  32  5  4  5  5  5  Young
## 2       2 10/28/08      US      F  45  3  5  2  5  5  Young
## 3       3  10/1/08      UK      F  25  3  5  5  5  2  Young
## 4       4 10/12/08      UK      M  39  3  3  4 NA NA  Young
## 5       5   5/1/09      UK      F  99  2  2  1  2  1  Elder

2nd way

leadership <- within(leadership,{
agecat <- NA
agecat[age > 75] <- "Elder"
agecat[age >= 55 & age <= 75] <- "Middle Aged"
agecat[age < 55] <- "Young" })

leadership
##   manager     date country gender age q1 q2 q3 q4 q5 agecat
## 1       1 10/24/08      US      M  32  5  4  5  5  5  Young
## 2       2 10/28/08      US      F  45  3  5  2  5  5  Young
## 3       3  10/1/08      UK      F  25  3  5  5  5  2  Young
## 4       4 10/12/08      UK      M  39  3  3  4 NA NA  Young
## 5       5   5/1/09      UK      F  99  2  2  1  2  1  Elder

Renaming variables

#Install.packages("reshape")
library(reshape)
## Warning: package 'reshape' was built under R version 4.2.2
leadership <- rename(leadership,
c(manager="managerID", date="testDate"))
leadership
##   managerID testDate country gender age q1 q2 q3 q4 q5 agecat
## 1         1 10/24/08      US      M  32  5  4  5  5  5  Young
## 2         2 10/28/08      US      F  45  3  5  2  5  5  Young
## 3         3  10/1/08      UK      F  25  3  5  5  5  2  Young
## 4         4 10/12/08      UK      M  39  3  3  4 NA NA  Young
## 5         5   5/1/09      UK      F  99  2  2  1  2  1  Elder
#leadership$age[leadership$age == 99] <- NA

removes missing values

x <- c(1, 2, NA, 3)
x
## [1]  1  2 NA  3
y <- x[1] + x[2] + x[3] + x[4]
y
## [1] NA
z <- sum(x)
z
## [1] NA
y1=sum(x,na.rm = T)
y1
## [1] 6
## remove any observation with missing data by using the na.omit() function

Type conversions

a <- c(1,2,3)
a
## [1] 1 2 3
is.numeric(a)
## [1] TRUE
is.vector(a)
## [1] TRUE
is.matrix(a)
## [1] FALSE
is.complex(a)
## [1] FALSE
is.data.frame(a)
## [1] FALSE
is.logical(a)
## [1] FALSE
a <- as.character(a)
a
## [1] "1" "2" "3"
is.numeric(a)
## [1] FALSE
is.vector(a)
## [1] TRUE
is.character(a)
## [1] TRUE

Sorting data set

1st way

leadership
##   managerID testDate country gender age q1 q2 q3 q4 q5 agecat
## 1         1 10/24/08      US      M  32  5  4  5  5  5  Young
## 2         2 10/28/08      US      F  45  3  5  2  5  5  Young
## 3         3  10/1/08      UK      F  25  3  5  5  5  2  Young
## 4         4 10/12/08      UK      M  39  3  3  4 NA NA  Young
## 5         5   5/1/09      UK      F  99  2  2  1  2  1  Elder
newdata <- leadership[order( leadership$age),]
newdata
##   managerID testDate country gender age q1 q2 q3 q4 q5 agecat
## 3         3  10/1/08      UK      F  25  3  5  5  5  2  Young
## 1         1 10/24/08      US      M  32  5  4  5  5  5  Young
## 4         4 10/12/08      UK      M  39  3  3  4 NA NA  Young
## 2         2 10/28/08      US      F  45  3  5  2  5  5  Young
## 5         5   5/1/09      UK      F  99  2  2  1  2  1  Elder
newdata1 <- leadership[order( gender,age),]
newdata1
##   managerID testDate country gender age q1 q2 q3 q4 q5 agecat
## 3         3  10/1/08      UK      F  25  3  5  5  5  2  Young
## 2         2 10/28/08      US      F  45  3  5  2  5  5  Young
## 5         5   5/1/09      UK      F  99  2  2  1  2  1  Elder
## 1         1 10/24/08      US      M  32  5  4  5  5  5  Young
## 4         4 10/12/08      UK      M  39  3  3  4 NA NA  Young

2nd way using attach

attach(leadership)
## The following objects are masked _by_ .GlobalEnv:
## 
##     age, country, gender, q1, q2, q3, q4, q5
newdata <- leadership[order(gender, age),]
newdata
##   managerID testDate country gender age q1 q2 q3 q4 q5 agecat
## 3         3  10/1/08      UK      F  25  3  5  5  5  2  Young
## 2         2 10/28/08      US      F  45  3  5  2  5  5  Young
## 5         5   5/1/09      UK      F  99  2  2  1  2  1  Elder
## 1         1 10/24/08      US      M  32  5  4  5  5  5  Young
## 4         4 10/12/08      UK      M  39  3  3  4 NA NA  Young
detach(leadership)
attach(leadership)
## The following objects are masked _by_ .GlobalEnv:
## 
##     age, country, gender, q1, q2, q3, q4, q5
newdata1 <-leadership[order(gender, -age),]
newdata1
##   managerID testDate country gender age q1 q2 q3 q4 q5 agecat
## 5         5   5/1/09      UK      F  99  2  2  1  2  1  Elder
## 2         2 10/28/08      US      F  45  3  5  2  5  5  Young
## 3         3  10/1/08      UK      F  25  3  5  5  5  2  Young
## 4         4 10/12/08      UK      M  39  3  3  4 NA NA  Young
## 1         1 10/24/08      US      M  32  5  4  5  5  5  Young
detach(leadership)

Merging datasets

dataframeA<-data.frame(ID=c(30,31,32,33),A=c(1,2,3,4))
dataframeB<-data.frame(ID=c(30,31,32,33),B=c(3,6,9,8))
dataframeA
##   ID A
## 1 30 1
## 2 31 2
## 3 32 3
## 4 33 4
dataframeB
##   ID B
## 1 30 3
## 2 31 6
## 3 32 9
## 4 33 8

Adding columns

total <- merge(dataframeA, dataframeB, by="ID")
total
##   ID A B
## 1 30 1 3
## 2 31 2 6
## 3 32 3 9
## 4 33 4 8

two matrices or data frame >> cbind

#If you’re joining two matrices or data frames horizontally and don’t need to specify a common key, you can use the cbind() function
A<-c(1,2,3,4)
B<-c(3,6,9,8)
total1<- cbind(A, B)
total1
##      A B
## [1,] 1 3
## [2,] 2 6
## [3,] 3 9
## [4,] 4 8

Adding rows >>rbind

dataframeA1<-data.frame(ID=c(30,31,32,33,56),m=c(1,2,3,4,10))
dataframeA1
##   ID  m
## 1 30  1
## 2 31  2
## 3 32  3
## 4 33  4
## 5 56 10
dataframeB1<-data.frame(ID=c(35,90,30),m=c(8,6,07))
dataframeB1
##   ID m
## 1 35 8
## 2 90 6
## 3 30 7
total2 <- rbind(dataframeA1,dataframeB1)
total2
##   ID  m
## 1 30  1
## 2 31  2
## 3 32  3
## 4 33  4
## 5 56 10
## 6 35  8
## 7 90  6
## 8 30  7

Subsetting datasets

leadership
##   managerID testDate country gender age q1 q2 q3 q4 q5 agecat
## 1         1 10/24/08      US      M  32  5  4  5  5  5  Young
## 2         2 10/28/08      US      F  45  3  5  2  5  5  Young
## 3         3  10/1/08      UK      F  25  3  5  5  5  2  Young
## 4         4 10/12/08      UK      M  39  3  3  4 NA NA  Young
## 5         5   5/1/09      UK      F  99  2  2  1  2  1  Elder
newdata <- leadership[ ,c(6:10)]
newdata
##   q1 q2 q3 q4 q5
## 1  5  4  5  5  5
## 2  3  5  2  5  5
## 3  3  5  5  5  2
## 4  3  3  4 NA NA
## 5  2  2  1  2  1

Excluding (dropping) variables

#Excluding (dropping) variables/excluding column 8,9
newdata2 <- leadership[c(-8,-9)]
newdata2
##   managerID testDate country gender age q1 q2 q5 agecat
## 1         1 10/24/08      US      M  32  5  4  5  Young
## 2         2 10/28/08      US      F  45  3  5  5  Young
## 3         3  10/1/08      UK      F  25  3  5  2  Young
## 4         4 10/12/08      UK      M  39  3  3 NA  Young
## 5         5   5/1/09      UK      F  99  2  2  1  Elder

Selecting observations

leadership
##   managerID testDate country gender age q1 q2 q3 q4 q5 agecat
## 1         1 10/24/08      US      M  32  5  4  5  5  5  Young
## 2         2 10/28/08      US      F  45  3  5  2  5  5  Young
## 3         3  10/1/08      UK      F  25  3  5  5  5  2  Young
## 4         4 10/12/08      UK      M  39  3  3  4 NA NA  Young
## 5         5   5/1/09      UK      F  99  2  2  1  2  1  Elder
newdata <- leadership[1:3,]
newdata
##   managerID testDate country gender age q1 q2 q3 q4 q5 agecat
## 1         1 10/24/08      US      M  32  5  4  5  5  5  Young
## 2         2 10/28/08      US      F  45  3  5  2  5  5  Young
## 3         3  10/1/08      UK      F  25  3  5  5  5  2  Young
newdata <- leadership[which(leadership$gender=="M" &
leadership$age > 30),]
newdata
##   managerID testDate country gender age q1 q2 q3 q4 q5 agecat
## 1         1 10/24/08      US      M  32  5  4  5  5  5  Young
## 4         4 10/12/08      UK      M  39  3  3  4 NA NA  Young

alternative

attach(leadership) newdata <- leadership[which(gender==‘M’ & age > 30),] detach(leadership)

The subset() function

leadership
##   managerID testDate country gender age q1 q2 q3 q4 q5 agecat
## 1         1 10/24/08      US      M  32  5  4  5  5  5  Young
## 2         2 10/28/08      US      F  45  3  5  2  5  5  Young
## 3         3  10/1/08      UK      F  25  3  5  5  5  2  Young
## 4         4 10/12/08      UK      M  39  3  3  4 NA NA  Young
## 5         5   5/1/09      UK      F  99  2  2  1  2  1  Elder
newdata <- subset(leadership, age >= 35 | age < 24,
                  select=c(q1, q2, q3, q4))
newdata
##   q1 q2 q3 q4
## 2  3  5  2  5
## 4  3  3  4 NA
## 5  2  2  1  2
newdata <- subset(leadership, gender=="M" & age > 25,
                  select=gender:q4)
newdata
##   gender age q1 q2 q3 q4
## 1      M  32  5  4  5  5
## 4      M  39  3  3  4 NA

random sample

ex-1 row sample

leadership
##   managerID testDate country gender age q1 q2 q3 q4 q5 agecat
## 1         1 10/24/08      US      M  32  5  4  5  5  5  Young
## 2         2 10/28/08      US      F  45  3  5  2  5  5  Young
## 3         3  10/1/08      UK      F  25  3  5  5  5  2  Young
## 4         4 10/12/08      UK      M  39  3  3  4 NA NA  Young
## 5         5   5/1/09      UK      F  99  2  2  1  2  1  Elder
mysample1 <- leadership[sample(1:nrow(leadership), 3,
replace=FALSE),]
mysample1
##   managerID testDate country gender age q1 q2 q3 q4 q5 agecat
## 1         1 10/24/08      US      M  32  5  4  5  5  5  Young
## 3         3  10/1/08      UK      F  25  3  5  5  5  2  Young
## 2         2 10/28/08      US      F  45  3  5  2  5  5  Young

ex-2 row sample

CO2
##    Plant        Type  Treatment conc uptake
## 1    Qn1      Quebec nonchilled   95   16.0
## 2    Qn1      Quebec nonchilled  175   30.4
## 3    Qn1      Quebec nonchilled  250   34.8
## 4    Qn1      Quebec nonchilled  350   37.2
## 5    Qn1      Quebec nonchilled  500   35.3
## 6    Qn1      Quebec nonchilled  675   39.2
## 7    Qn1      Quebec nonchilled 1000   39.7
## 8    Qn2      Quebec nonchilled   95   13.6
## 9    Qn2      Quebec nonchilled  175   27.3
## 10   Qn2      Quebec nonchilled  250   37.1
## 11   Qn2      Quebec nonchilled  350   41.8
## 12   Qn2      Quebec nonchilled  500   40.6
## 13   Qn2      Quebec nonchilled  675   41.4
## 14   Qn2      Quebec nonchilled 1000   44.3
## 15   Qn3      Quebec nonchilled   95   16.2
## 16   Qn3      Quebec nonchilled  175   32.4
## 17   Qn3      Quebec nonchilled  250   40.3
## 18   Qn3      Quebec nonchilled  350   42.1
## 19   Qn3      Quebec nonchilled  500   42.9
## 20   Qn3      Quebec nonchilled  675   43.9
## 21   Qn3      Quebec nonchilled 1000   45.5
## 22   Qc1      Quebec    chilled   95   14.2
## 23   Qc1      Quebec    chilled  175   24.1
## 24   Qc1      Quebec    chilled  250   30.3
## 25   Qc1      Quebec    chilled  350   34.6
## 26   Qc1      Quebec    chilled  500   32.5
## 27   Qc1      Quebec    chilled  675   35.4
## 28   Qc1      Quebec    chilled 1000   38.7
## 29   Qc2      Quebec    chilled   95    9.3
## 30   Qc2      Quebec    chilled  175   27.3
## 31   Qc2      Quebec    chilled  250   35.0
## 32   Qc2      Quebec    chilled  350   38.8
## 33   Qc2      Quebec    chilled  500   38.6
## 34   Qc2      Quebec    chilled  675   37.5
## 35   Qc2      Quebec    chilled 1000   42.4
## 36   Qc3      Quebec    chilled   95   15.1
## 37   Qc3      Quebec    chilled  175   21.0
## 38   Qc3      Quebec    chilled  250   38.1
## 39   Qc3      Quebec    chilled  350   34.0
## 40   Qc3      Quebec    chilled  500   38.9
## 41   Qc3      Quebec    chilled  675   39.6
## 42   Qc3      Quebec    chilled 1000   41.4
## 43   Mn1 Mississippi nonchilled   95   10.6
## 44   Mn1 Mississippi nonchilled  175   19.2
## 45   Mn1 Mississippi nonchilled  250   26.2
## 46   Mn1 Mississippi nonchilled  350   30.0
## 47   Mn1 Mississippi nonchilled  500   30.9
## 48   Mn1 Mississippi nonchilled  675   32.4
## 49   Mn1 Mississippi nonchilled 1000   35.5
## 50   Mn2 Mississippi nonchilled   95   12.0
## 51   Mn2 Mississippi nonchilled  175   22.0
## 52   Mn2 Mississippi nonchilled  250   30.6
## 53   Mn2 Mississippi nonchilled  350   31.8
## 54   Mn2 Mississippi nonchilled  500   32.4
## 55   Mn2 Mississippi nonchilled  675   31.1
## 56   Mn2 Mississippi nonchilled 1000   31.5
## 57   Mn3 Mississippi nonchilled   95   11.3
## 58   Mn3 Mississippi nonchilled  175   19.4
## 59   Mn3 Mississippi nonchilled  250   25.8
## 60   Mn3 Mississippi nonchilled  350   27.9
## 61   Mn3 Mississippi nonchilled  500   28.5
## 62   Mn3 Mississippi nonchilled  675   28.1
## 63   Mn3 Mississippi nonchilled 1000   27.8
## 64   Mc1 Mississippi    chilled   95   10.5
## 65   Mc1 Mississippi    chilled  175   14.9
## 66   Mc1 Mississippi    chilled  250   18.1
## 67   Mc1 Mississippi    chilled  350   18.9
## 68   Mc1 Mississippi    chilled  500   19.5
## 69   Mc1 Mississippi    chilled  675   22.2
## 70   Mc1 Mississippi    chilled 1000   21.9
## 71   Mc2 Mississippi    chilled   95    7.7
## 72   Mc2 Mississippi    chilled  175   11.4
## 73   Mc2 Mississippi    chilled  250   12.3
## 74   Mc2 Mississippi    chilled  350   13.0
## 75   Mc2 Mississippi    chilled  500   12.5
## 76   Mc2 Mississippi    chilled  675   13.7
## 77   Mc2 Mississippi    chilled 1000   14.4
## 78   Mc3 Mississippi    chilled   95   10.6
## 79   Mc3 Mississippi    chilled  175   18.0
## 80   Mc3 Mississippi    chilled  250   17.9
## 81   Mc3 Mississippi    chilled  350   17.9
## 82   Mc3 Mississippi    chilled  500   17.9
## 83   Mc3 Mississippi    chilled  675   18.9
## 84   Mc3 Mississippi    chilled 1000   19.9
mysample2 <- CO2[sample(1:nrow(CO2), 10,
replace=FALSE),]
mysample2
##    Plant        Type  Treatment conc uptake
## 46   Mn1 Mississippi nonchilled  350   30.0
## 45   Mn1 Mississippi nonchilled  250   26.2
## 22   Qc1      Quebec    chilled   95   14.2
## 69   Mc1 Mississippi    chilled  675   22.2
## 64   Mc1 Mississippi    chilled   95   10.5
## 55   Mn2 Mississippi nonchilled  675   31.1
## 37   Qc3      Quebec    chilled  175   21.0
## 17   Qn3      Quebec nonchilled  250   40.3
## 13   Qn2      Quebec nonchilled  675   41.4
## 29   Qc2      Quebec    chilled   95    9.3

ex-3 coloum sample

mysample3 <- CO2[,sample(1:ncol(CO2), 2,
replace=FALSE)]
mysample3
##    uptake        Type
## 1    16.0      Quebec
## 2    30.4      Quebec
## 3    34.8      Quebec
## 4    37.2      Quebec
## 5    35.3      Quebec
## 6    39.2      Quebec
## 7    39.7      Quebec
## 8    13.6      Quebec
## 9    27.3      Quebec
## 10   37.1      Quebec
## 11   41.8      Quebec
## 12   40.6      Quebec
## 13   41.4      Quebec
## 14   44.3      Quebec
## 15   16.2      Quebec
## 16   32.4      Quebec
## 17   40.3      Quebec
## 18   42.1      Quebec
## 19   42.9      Quebec
## 20   43.9      Quebec
## 21   45.5      Quebec
## 22   14.2      Quebec
## 23   24.1      Quebec
## 24   30.3      Quebec
## 25   34.6      Quebec
## 26   32.5      Quebec
## 27   35.4      Quebec
## 28   38.7      Quebec
## 29    9.3      Quebec
## 30   27.3      Quebec
## 31   35.0      Quebec
## 32   38.8      Quebec
## 33   38.6      Quebec
## 34   37.5      Quebec
## 35   42.4      Quebec
## 36   15.1      Quebec
## 37   21.0      Quebec
## 38   38.1      Quebec
## 39   34.0      Quebec
## 40   38.9      Quebec
## 41   39.6      Quebec
## 42   41.4      Quebec
## 43   10.6 Mississippi
## 44   19.2 Mississippi
## 45   26.2 Mississippi
## 46   30.0 Mississippi
## 47   30.9 Mississippi
## 48   32.4 Mississippi
## 49   35.5 Mississippi
## 50   12.0 Mississippi
## 51   22.0 Mississippi
## 52   30.6 Mississippi
## 53   31.8 Mississippi
## 54   32.4 Mississippi
## 55   31.1 Mississippi
## 56   31.5 Mississippi
## 57   11.3 Mississippi
## 58   19.4 Mississippi
## 59   25.8 Mississippi
## 60   27.9 Mississippi
## 61   28.5 Mississippi
## 62   28.1 Mississippi
## 63   27.8 Mississippi
## 64   10.5 Mississippi
## 65   14.9 Mississippi
## 66   18.1 Mississippi
## 67   18.9 Mississippi
## 68   19.5 Mississippi
## 69   22.2 Mississippi
## 70   21.9 Mississippi
## 71    7.7 Mississippi
## 72   11.4 Mississippi
## 73   12.3 Mississippi
## 74   13.0 Mississippi
## 75   12.5 Mississippi
## 76   13.7 Mississippi
## 77   14.4 Mississippi
## 78   10.6 Mississippi
## 79   18.0 Mississippi
## 80   17.9 Mississippi
## 81   17.9 Mississippi
## 82   17.9 Mississippi
## 83   18.9 Mississippi
## 84   19.9 Mississippi

slide 05 (Advanced Data Management)

Statistical functions

#z <- mean(x, trim = 0.05, na.rm=TRUE)

ceiling(x = 3.551)
## [1] 4
floor(x = 3.551)
## [1] 3
round(3.551,2)
## [1] 3.55
signif(3.551,2)
## [1] 3.6
log(3)
## [1] 1.098612
log10(3)
## [1] 0.4771213
log(x=3,base = 10)
## [1] 0.4771213
x=c(1,2,3,3,6,59,09,30,20)
plot(x,sin(x))

x=c(1,2,3,4,5,6,7,8)
mean(x)
## [1] 4.5
median(x)
## [1] 4.5
sd(x)
## [1] 2.44949
var(x)
## [1] 6
mad(x)
## [1] 2.9652
quantile(x,c(.3,.84))
##  30%  84% 
## 3.10 6.88
range(x)
## [1] 1 8
sum(x)
## [1] 36
diff(x,lag = 1)
## [1] 1 1 1 1 1 1 1
diff(x,lag = 2)
## [1] 2 2 2 2 2 2
diff(x,lag = 3)
## [1] 3 3 3 3 3
min(x)
## [1] 1
max(x)
## [1] 8
scale(x,center = TRUE,scale = TRUE)
##            [,1]
## [1,] -1.4288690
## [2,] -1.0206207
## [3,] -0.6123724
## [4,] -0.2041241
## [5,]  0.2041241
## [6,]  0.6123724
## [7,]  1.0206207
## [8,]  1.4288690
## attr(,"scaled:center")
## [1] 4.5
## attr(,"scaled:scale")
## [1] 2.44949

Probability functions

d = density p = distribution function q = quantile function r = random generation (random deviates)

x<-pretty(c(-3,3),30) 
x
##  [1] -3.0 -2.8 -2.6 -2.4 -2.2 -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2
## [16]  0.0  0.2  0.4  0.6  0.8  1.0  1.2  1.4  1.6  1.8  2.0  2.2  2.4  2.6  2.8
## [31]  3.0
y<-dnorm(x)
y
##  [1] 0.004431848 0.007915452 0.013582969 0.022394530 0.035474593 0.053990967
##  [7] 0.078950158 0.110920835 0.149727466 0.194186055 0.241970725 0.289691553
## [13] 0.333224603 0.368270140 0.391042694 0.398942280 0.391042694 0.368270140
## [19] 0.333224603 0.289691553 0.241970725 0.194186055 0.149727466 0.110920835
## [25] 0.078950158 0.053990967 0.035474593 0.022394530 0.013582969 0.007915452
## [31] 0.004431848
plot(x,y,type ="l",xlab= "NORMAL DEVIATE",ylab = "DENSITY",yaxs= 'i')

#yaxs The style of axis interval calculation to be used for the y-axis.

Generating multivariate normal data

#install.packages("MASS")
library(MASS)
options(digits=7)
set.seed(1234)
mean <- c(250.7, 130.5, 5.6)
n <- 3
A <- matrix(runif((n^2),-3,3), ncol=n)
A
##            [,1]      [,2]       [,3]
## [1,] -2.3177795 0.7402767 -2.9430255
## [2,]  0.7337964 2.1654923 -1.6046970
## [3,]  0.6556484 0.8418636  0.9965025
#generating n by n matrix with values from uniform distribution
sigma<- t(A) %*% A #hypothetical var-cov matrix
sigma
##          [,1]      [,2]      [,3]
## [1,] 6.340434  0.425199  6.297119
## [2,] 0.425199  5.946101 -4.814693
## [3,] 6.297119 -4.814693 12.229469
mydata <- mvrnorm(400, mean, sigma) #generating 400 values from a
#multivariate normal
mydata <- as.data.frame(mydata)
names(mydata) <- c("y","x1","x2")
dim(mydata)
## [1] 400   3
head(mydata, n=15)
##           y       x1         x2
## 1  251.0184 130.8935  5.4007886
## 2  251.5151 132.6467  5.4306534
## 3  247.0263 131.7707  0.8881412
## 4  248.9954 132.7586  2.4520034
## 5  250.5811 127.8735  7.7745863
## 6  252.3736 129.7477  8.0861491
## 7  252.4378 130.2279  7.1547260
## 8  254.9901 128.2115 11.2019576
## 9  247.7025 130.1414  2.6478323
## 10 248.0582 133.2701  1.2118612
## 11 254.2280 128.2655 11.3189866
## 12 250.8919 127.6359  7.5363612
## 13 248.1130 127.3608  5.6819667
## 14 249.0960 130.1720  2.7826842
## 15 253.2586 131.2778  7.7792555

Pattern Matching and Replacement >>grep

grep("[a-z]", letters) 
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
## [26] 26
# letters will produce a,b,c,...z and will match a-z letters
letters 
##  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
## [20] "t" "u" "v" "w" "x" "y" "z"
#to see what letters appear
grep("[abc]", letters) 
## [1] 1 2 3
#match the places where any of 'abc' found in the sequence
grep("[ased]", letters)
## [1]  1  4  5 19
grep("[ased]", letters,fixed=T)
## integer(0)
grep("[ased]", letters,fixed=F)
## [1]  1  4  5 19
grep("[ased]", c(letters,"[ased]"),fixed=T)
## [1] 27
grep("[ased]", c(letters,"[ased]"),fixed=F)
## [1]  1  4  5 19 27
a <- 6
sqrt(a)
## [1] 2.44949
b <- c(1.532, 5.754, 3.99)
round(b)
## [1] 2 6 4
d <- matrix(runif(16), nrow=4)
d
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.9189159 0.2512678 0.9555143 0.5007705
## [2,] 0.8519516 0.6550916 0.7320949 0.2580541
## [3,] 0.7600845 0.1710871 0.9830077 0.5201532
## [4,] 0.7223479 0.5576245 0.1508911 0.1581700
log(d)
##            [,1]       [,2]        [,3]       [,4]
## [1,] -0.0845607 -1.3812359 -0.04550559 -0.6916074
## [2,] -0.1602256 -0.4229802 -0.31184510 -1.3545860
## [3,] -0.2743256 -1.7655828 -0.01713829 -0.6536319
## [4,] -0.3252484 -0.5840695 -1.89119695 -1.8440846
mean(d)
## [1] 0.5716892

Applying functions

mydata <- matrix(rnorm(30), nrow=6)
mydata
##            [,1]       [,2]       [,3]       [,4]       [,5]
## [1,]  1.2779592 -0.2144554 -0.2475043  0.4743173  1.3109869
## [2,]  0.1393738 -0.4587626  1.1982527 -1.0086812  0.4528040
## [3,]  1.6063475  0.2104320  0.8935556  0.5232864  2.1354266
## [4,] -0.9469487  0.3622436 -0.0294763 -0.1711696  0.4319354
## [5,] -1.2656885 -0.9867580 -0.5971864  0.4732156 -0.1613210
## [6,]  1.5545595  1.2751202  0.6129529  0.3315565 -0.3175865
#Calculate row means
apply(mydata, 1, mean) 
## [1]  0.52026074  0.06459732  1.07380963 -0.07068311 -0.50754765  0.69132053
#Calculate column means
apply(mydata, 2, mean) 
## [1] 0.3942671 0.0313033 0.3050990 0.1037542 0.6420409
apply(mydata, 2, mean, trim=0.2)
## [1]  0.5062360 -0.0251356  0.3073820  0.2769800  0.5086013

Data management problem

options(digits=3)

Student <- c("Asif Rahman", "Angela Dean", "Heera Das",
"Issac Robin", "Shamsul Huda", "Ashraf Uddin",
"Nazrul Islam", "Shamsur Rahman", "Hemonto Mukherjee",
"Salemun Bahar")
Math <- c(502, 600, 412, 358, 495, 512, 410, 625, 573, 522)
Science <- c(95, 99, 80, 82, 75, 85, 80, 95, 89, 86)
English <- c(25, 22, 18, 15, 20, 28, 15, 30, 27, 18)
roster <- data.frame(Student, Math, Science, English,
stringsAsFactors=FALSE)
roster 
##              Student Math Science English
## 1        Asif Rahman  502      95      25
## 2        Angela Dean  600      99      22
## 3          Heera Das  412      80      18
## 4        Issac Robin  358      82      15
## 5       Shamsul Huda  495      75      20
## 6       Ashraf Uddin  512      85      28
## 7       Nazrul Islam  410      80      15
## 8     Shamsur Rahman  625      95      30
## 9  Hemonto Mukherjee  573      89      27
## 10     Salemun Bahar  522      86      18
#Obtain performance scores
z <- scale(roster[,2:4])
z
##          Math Science English
##  [1,]  0.0127   1.078  0.5869
##  [2,]  1.1434   1.591  0.0367
##  [3,] -1.0257  -0.847 -0.6969
##  [4,] -1.6487  -0.590 -1.2471
##  [5,] -0.0681  -1.489 -0.3301
##  [6,]  0.1281  -0.205  1.1370
##  [7,] -1.0488  -0.847 -1.2471
##  [8,]  1.4318   1.078  1.5038
##  [9,]  0.8319   0.308  0.9536
## [10,]  0.2434  -0.077 -0.6969
## attr(,"scaled:center")
##    Math Science English 
##   500.9    86.6    21.8 
## attr(,"scaled:scale")
##    Math Science English 
##   86.67    7.79    5.45
#calculates row means i.e.one score from each student after combining math,science, English
score <- apply(z, 1, mean) 
score
##  [1]  0.559  0.924 -0.857 -1.162 -0.629  0.353 -1.048  1.338  0.698 -0.177
roster <- cbind(roster, score)
roster
##              Student Math Science English  score
## 1        Asif Rahman  502      95      25  0.559
## 2        Angela Dean  600      99      22  0.924
## 3          Heera Das  412      80      18 -0.857
## 4        Issac Robin  358      82      15 -1.162
## 5       Shamsul Huda  495      75      20 -0.629
## 6       Ashraf Uddin  512      85      28  0.353
## 7       Nazrul Islam  410      80      15 -1.048
## 8     Shamsur Rahman  625      95      30  1.338
## 9  Hemonto Mukherjee  573      89      27  0.698
## 10     Salemun Bahar  522      86      18 -0.177
#setting quantiles ofscore vector at 80%, 60%, 40%, 20%
y <- quantile(score, c(.8,.6,.4,.2)) 
y
##    80%    60%    40%    20% 
##  0.743  0.436 -0.358 -0.895
#will give natural quantile at 0%, 25%,50%, 75%, 100% points.
t=quantile(score)
t
##      0%     25%     50%     75%    100% 
## -1.1620 -0.7997  0.0882  0.6632  1.3379
roster$grade[score >= y[1]] <- "A"
roster$grade[score < y[1] & score >= y[2]] <- "B"
roster$grade[score < y[2] & score >= y[3]] <- "C"
roster$grade[score < y[3] & score >= y[4]] <- "D"
roster$grade[score < y[4]] <- "F"
roster
##              Student Math Science English  score grade
## 1        Asif Rahman  502      95      25  0.559     B
## 2        Angela Dean  600      99      22  0.924     A
## 3          Heera Das  412      80      18 -0.857     D
## 4        Issac Robin  358      82      15 -1.162     F
## 5       Shamsul Huda  495      75      20 -0.629     D
## 6       Ashraf Uddin  512      85      28  0.353     C
## 7       Nazrul Islam  410      80      15 -1.048     F
## 8     Shamsur Rahman  625      95      30  1.338     A
## 9  Hemonto Mukherjee  573      89      27  0.698     B
## 10     Salemun Bahar  522      86      18 -0.177     C
name <- strsplit((roster$Student), " ")
name
## [[1]]
## [1] "Asif"   "Rahman"
## 
## [[2]]
## [1] "Angela" "Dean"  
## 
## [[3]]
## [1] "Heera" "Das"  
## 
## [[4]]
## [1] "Issac" "Robin"
## 
## [[5]]
## [1] "Shamsul" "Huda"   
## 
## [[6]]
## [1] "Ashraf" "Uddin" 
## 
## [[7]]
## [1] "Nazrul" "Islam" 
## 
## [[8]]
## [1] "Shamsur" "Rahman" 
## 
## [[9]]
## [1] "Hemonto"   "Mukherjee"
## 
## [[10]]
## [1] "Salemun" "Bahar"
#extracting last name
lastname <- sapply(name, "[", 2) 
lastname
##  [1] "Rahman"    "Dean"      "Das"       "Robin"     "Huda"      "Uddin"    
##  [7] "Islam"     "Rahman"    "Mukherjee" "Bahar"
firstname <- sapply(name, "[", 1)
firstname 
##  [1] "Asif"    "Angela"  "Heera"   "Issac"   "Shamsul" "Ashraf"  "Nazrul" 
##  [8] "Shamsur" "Hemonto" "Salemun"
roster <- cbind(firstname,lastname, roster[,-1])
roster
##    firstname  lastname Math Science English  score grade
## 1       Asif    Rahman  502      95      25  0.559     B
## 2     Angela      Dean  600      99      22  0.924     A
## 3      Heera       Das  412      80      18 -0.857     D
## 4      Issac     Robin  358      82      15 -1.162     F
## 5    Shamsul      Huda  495      75      20 -0.629     D
## 6     Ashraf     Uddin  512      85      28  0.353     C
## 7     Nazrul     Islam  410      80      15 -1.048     F
## 8    Shamsur    Rahman  625      95      30  1.338     A
## 9    Hemonto Mukherjee  573      89      27  0.698     B
## 10   Salemun     Bahar  522      86      18 -0.177     C

Ordering

#orders according to frist name and then by last name
roster <- roster[order(firstname,lastname),]
roster
##    firstname  lastname Math Science English  score grade
## 2     Angela      Dean  600      99      22  0.924     A
## 6     Ashraf     Uddin  512      85      28  0.353     C
## 1       Asif    Rahman  502      95      25  0.559     B
## 3      Heera       Das  412      80      18 -0.857     D
## 9    Hemonto Mukherjee  573      89      27  0.698     B
## 4      Issac     Robin  358      82      15 -1.162     F
## 7     Nazrul     Islam  410      80      15 -1.048     F
## 10   Salemun     Bahar  522      86      18 -0.177     C
## 5    Shamsul      Huda  495      75      20 -0.629     D
## 8    Shamsur    Rahman  625      95      30  1.338     A
#orders according to last name and then by frist name
roster <- roster[order(lastname,firstname),]
roster
##    firstname  lastname Math Science English  score grade
## 8    Shamsur    Rahman  625      95      30  1.338     A
## 1       Asif    Rahman  502      95      25  0.559     B
## 6     Ashraf     Uddin  512      85      28  0.353     C
## 9    Hemonto Mukherjee  573      89      27  0.698     B
## 7     Nazrul     Islam  410      80      15 -1.048     F
## 5    Shamsul      Huda  495      75      20 -0.629     D
## 2     Angela      Dean  600      99      22  0.924     A
## 10   Salemun     Bahar  522      86      18 -0.177     C
## 3      Heera       Das  412      80      18 -0.857     D
## 4      Issac     Robin  358      82      15 -1.162     F
##orders according grade…….. roster
roster <-roster[order(roster$grade,firstname,lastname),]
roster
##    firstname  lastname Math Science English  score grade
## 8    Shamsur    Rahman  625      95      30  1.338     A
## 2     Angela      Dean  600      99      22  0.924     A
## 1       Asif    Rahman  502      95      25  0.559     B
## 9    Hemonto Mukherjee  573      89      27  0.698     B
## 6     Ashraf     Uddin  512      85      28  0.353     C
## 10   Salemun     Bahar  522      86      18 -0.177     C
## 5    Shamsul      Huda  495      75      20 -0.629     D
## 3      Heera       Das  412      80      18 -0.857     D
## 4      Issac     Robin  358      82      15 -1.162     F
## 7     Nazrul     Islam  410      80      15 -1.048     F

Repetition and looping

For

for (i in 1:5) 
print("Welcome")
## [1] "Welcome"
## [1] "Welcome"
## [1] "Welcome"
## [1] "Welcome"
## [1] "Welcome"

while

i <- 10
while (i > 0) {
  print("2nd year");
  i <- i - 1
}
## [1] "2nd year"
## [1] "2nd year"
## [1] "2nd year"
## [1] "2nd year"
## [1] "2nd year"
## [1] "2nd year"
## [1] "2nd year"
## [1] "2nd year"
## [1] "2nd year"
## [1] "2nd year"

IFELSE

score<-runif(50, 0,100)
score
##  [1] 15.14 42.96 36.86 61.42 83.11  8.97 35.30 73.66  6.31 58.19 69.58 73.87
## [13] 31.07 34.07 17.26 95.40 47.65 44.44  1.50 54.82 29.61 45.79 15.10 80.13
## [25] 77.78 22.11  1.98 57.98  1.07 71.72 44.10 10.65 93.54 59.01 29.83 27.63
## [37] 23.30 92.35 57.21 75.96 14.76  4.23 33.95 82.10 91.53 58.62 54.44 28.73
## [49] 65.59 36.11
outcome <- ifelse (score > 40.0, "Passed", "Failed")
outcome
##  [1] "Failed" "Passed" "Failed" "Passed" "Passed" "Failed" "Failed" "Passed"
##  [9] "Failed" "Passed" "Passed" "Passed" "Failed" "Failed" "Failed" "Passed"
## [17] "Passed" "Passed" "Failed" "Passed" "Failed" "Passed" "Failed" "Passed"
## [25] "Passed" "Failed" "Failed" "Passed" "Failed" "Passed" "Passed" "Failed"
## [33] "Passed" "Passed" "Failed" "Failed" "Failed" "Passed" "Passed" "Passed"
## [41] "Failed" "Failed" "Failed" "Passed" "Passed" "Passed" "Passed" "Failed"
## [49] "Passed" "Failed"
table(outcome)
## outcome
## Failed Passed 
##     23     27

Transpose

cars <- mtcars[1:8,1:5]
cars
##                    mpg cyl disp  hp drat
## Mazda RX4         21.0   6  160 110 3.90
## Mazda RX4 Wag     21.0   6  160 110 3.90
## Datsun 710        22.8   4  108  93 3.85
## Hornet 4 Drive    21.4   6  258 110 3.08
## Hornet Sportabout 18.7   8  360 175 3.15
## Valiant           18.1   6  225 105 2.76
## Duster 360        14.3   8  360 245 3.21
## Merc 240D         24.4   4  147  62 3.69
t(cars)
##      Mazda RX4 Mazda RX4 Wag Datsun 710 Hornet 4 Drive Hornet Sportabout
## mpg       21.0          21.0      22.80          21.40             18.70
## cyl        6.0           6.0       4.00           6.00              8.00
## disp     160.0         160.0     108.00         258.00            360.00
## hp       110.0         110.0      93.00         110.00            175.00
## drat       3.9           3.9       3.85           3.08              3.15
##      Valiant Duster 360 Merc 240D
## mpg    18.10      14.30     24.40
## cyl     6.00       8.00      4.00
## disp  225.00     360.00    146.70
## hp    105.00     245.00     62.00
## drat    2.76       3.21      3.69

Aggregation and restructuring

Aggregate function

#takes upto 4 digits for each variable
options(digits=4) 

attach(mtcars)
## The following object is masked from package:ggplot2:
## 
##     mpg
#Aggregates mtcars by varaiables cyl, and gear.
aggcars<-aggregate(mtcars,by=list(cyl,gear),FUN=mean, na.rm=TRUE)
aggcars
##   Group.1 Group.2   mpg cyl  disp    hp  drat    wt  qsec  vs   am gear  carb
## 1       4       3 21.50   4 120.1  97.0 3.700 2.465 20.01 1.0 0.00    3 1.000
## 2       6       3 19.75   6 241.5 107.5 2.920 3.337 19.83 1.0 0.00    3 1.000
## 3       8       3 15.05   8 357.6 194.2 3.121 4.104 17.14 0.0 0.00    3 3.083
## 4       4       4 26.93   4 102.6  76.0 4.110 2.378 19.61 1.0 0.75    4 1.500
## 5       6       4 19.75   6 163.8 116.5 3.910 3.094 17.67 0.5 0.50    4 4.000
## 6       4       5 28.20   4 107.7 102.0 4.100 1.827 16.80 0.5 1.00    5 2.000
## 7       6       5 19.70   6 145.0 175.0 3.620 2.770 15.50 0.0 1.00    5 6.000
## 8       8       5 15.40   8 326.0 299.5 3.880 3.370 14.55 0.0 1.00    5 6.000

Reshaping data (Use of melt() and cast() functions)

#install.packages("reshape")
library(reshape)
options(digits=2)
ID<-rep(1:5, each = 2)
Semester<-rep(1:2,times=5)
Math <- c(502, 560, 412, 358, 590, 595, 410, 423, 573, 522)
Science <- c(95, 99, 80, 82, 75, 85, 80, 95, 89, 86)
English <- c(25, 22, 18, 15, 25, 28, 15, 18, 27, 28)
examdata <- data.frame(ID,Semester, Math, Science, English,row.names = NULL,check.rows = F,check.names = T)
examdata
##    ID Semester Math Science English
## 1   1        1  502      95      25
## 2   1        2  560      99      22
## 3   2        1  412      80      18
## 4   2        2  358      82      15
## 5   3        1  590      75      25
## 6   3        2  595      85      28
## 7   4        1  410      80      15
## 8   4        2  423      95      18
## 9   5        1  573      89      27
## 10  5        2  522      86      28

melt() functions

library(reshape)
# for restructure
md <-  melt(examdata, id=c("ID", "Semester"))
md
##    ID Semester variable value
## 1   1        1     Math   502
## 2   1        2     Math   560
## 3   2        1     Math   412
## 4   2        2     Math   358
## 5   3        1     Math   590
## 6   3        2     Math   595
## 7   4        1     Math   410
## 8   4        2     Math   423
## 9   5        1     Math   573
## 10  5        2     Math   522
## 11  1        1  Science    95
## 12  1        2  Science    99
## 13  2        1  Science    80
## 14  2        2  Science    82
## 15  3        1  Science    75
## 16  3        2  Science    85
## 17  4        1  Science    80
## 18  4        2  Science    95
## 19  5        1  Science    89
## 20  5        2  Science    86
## 21  1        1  English    25
## 22  1        2  English    22
## 23  2        1  English    18
## 24  2        2  English    15
## 25  3        1  English    25
## 26  3        2  English    28
## 27  4        1  English    15
## 28  4        2  English    18
## 29  5        1  English    27
## 30  5        2  English    28

cast() functions *** problem

Aggregation
#library(reshape)
#library(reshape2)
#Aggregation
#cast(md, ID~variable, mean)
#cast(md, Semester~variable, mean)
#cast(md, ID~Semester, mean)
#cast(data = md,ID~.,mean)
#cast(md, Semester~., mean)

#average of 3 module scores in each semester

Without Aggregation
#Reshaping data (Use of melt() and cast() functions)
md <- melt(examdata, id=(c("ID", "Semester")))
md
##    ID Semester variable value
## 1   1        1     Math   502
## 2   1        2     Math   560
## 3   2        1     Math   412
## 4   2        2     Math   358
## 5   3        1     Math   590
## 6   3        2     Math   595
## 7   4        1     Math   410
## 8   4        2     Math   423
## 9   5        1     Math   573
## 10  5        2     Math   522
## 11  1        1  Science    95
## 12  1        2  Science    99
## 13  2        1  Science    80
## 14  2        2  Science    82
## 15  3        1  Science    75
## 16  3        2  Science    85
## 17  4        1  Science    80
## 18  4        2  Science    95
## 19  5        1  Science    89
## 20  5        2  Science    86
## 21  1        1  English    25
## 22  1        2  English    22
## 23  2        1  English    18
## 24  2        2  English    15
## 25  3        1  English    25
## 26  3        2  English    28
## 27  4        1  English    15
## 28  4        2  English    18
## 29  5        1  English    27
## 30  5        2  English    28
#Without Aggregation
cast(md, ID+Semester~variable)
##    ID Semester Math Science English
## 1   1        1  502      95      25
## 2   1        2  560      99      22
## 3   2        1  412      80      18
## 4   2        2  358      82      15
## 5   3        1  590      75      25
## 6   3        2  595      85      28
## 7   4        1  410      80      15
## 8   4        2  423      95      18
## 9   5        1  573      89      27
## 10  5        2  522      86      28
cast(md, ID+variable~Semester)
##    ID variable   1   2
## 1   1     Math 502 560
## 2   1  Science  95  99
## 3   1  English  25  22
## 4   2     Math 412 358
## 5   2  Science  80  82
## 6   2  English  18  15
## 7   3     Math 590 595
## 8   3  Science  75  85
## 9   3  English  25  28
## 10  4     Math 410 423
## 11  4  Science  80  95
## 12  4  English  15  18
## 13  5     Math 573 522
## 14  5  Science  89  86
## 15  5  English  27  28
cast(md, ID~variable+Semester)
##   ID Math_1 Math_2 Science_1 Science_2 English_1 English_2
## 1  1    502    560        95        99        25        22
## 2  2    412    358        80        82        18        15
## 3  3    590    595        75        85        25        28
## 4  4    410    423        80        95        15        18
## 5  5    573    522        89        86        27        28

slide 06 (Functions, Loops, Simulation)

Example 1

h<- function(number) {
for(i in seq_len(number)) {

cat("Hello, can you listen me?\n")
} }
h(5)
## Hello, can you listen me?
## Hello, can you listen me?
## Hello, can you listen me?
## Hello, can you listen me?
## Hello, can you listen me?

Example 2

sumdice=function(n){
k=sample(1:6, size=n, replace=TRUE)
#return(k)

return(sum(k))
}
sumdice(2)
## [1] 10
sumdice(3)
## [1] 7

Example 3

dice=function(n){
k=sample(1:6, size=n, replace=TRUE)
return(k)

#return(k)
}
dice(2)
## [1] 2 4
dice(3)
## [1] 5 3 2

Example 4

sumdice1=function(m, n){
k=sample(1:m, size=n, replace=TRUE)
return(k)
#return(sum(k))

}
sumdice1(7,2)
## [1] 7 2
sumdice1(7,3)
## [1] 4 7 1
sumdice1(500,30)
##  [1] 330 416 329   3 121 263 322 106 445  99 104 368 217  52  13 372 362 188 213
## [20]  75 249 262  88  90 253  22 342 327 180 290

Example 5

poisson2=function(lambda, x){
p=(exp(-lambda)*(lambda)^x)/factorial(x)

return(p)
}
poisson2(lambda=2, x=3)
## [1] 0.18
poisson2(lambda=(50/60), x=5)
## [1] 0.0015

slide 07 (Exploratory Data Analysis)

base plotting system

ex:1

data(airquality)
with(airquality, {
plot(Temp, Ozone)
lines(loess.smooth(Temp, Ozone))
  })

ex:2

data(cars)
## Create the plot / draw canvas
with(cars, plot(speed, dist))
## Add annotation
title("Speed vs. Stopping distance")

The Lattice System

#install lattice packages

library(lattice)

state <- data.frame(state.x77, region = state.region)
xyplot(Life.Exp ~ Income | region, data = state, layout =
c(4, 1))

The ggplot2 System

#install.packages("ggplot2")
library(ggplot2)
data(mpg)
qplot(displ, hwy, data = mpg)
## Warning: `qplot()` was deprecated in ggplot2 3.4.0.

slide 08 (The Base Plotting System)

Histogram

##The Base Plotting System
library(datasets)
hist(airquality$Ozone)

Boxplot

airquality <- transform(airquality,
Month = factor(Month))
boxplot(Ozone ~ Month, airquality, xlab
= "Month", ylab = "Ozone (ppb)")

slide 09 (The ggplot2 Plotting System)

Ex 1

with(airquality,{
  plot(Temp,Ozone)
  lines(loess.smooth(Temp,Ozone))
  
})

Ex 2

#install.packages("ggplot2")
library(ggplot2)
ggplot(airquality,aes(Temp,Ozone))+
  geom_point()+
  geom_smooth(method = "loess",se = F)
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 37 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 37 rows containing missing values (`geom_point()`).

Ex 3

library(ggplot2)
str(mpg)
## tibble [234 × 11] (S3: tbl_df/tbl/data.frame)
##  $ manufacturer: chr [1:234] "audi" "audi" "audi" "audi" ...
##  $ model       : chr [1:234] "a4" "a4" "a4" "a4" ...
##  $ displ       : num [1:234] 1.8 1.8 2 2 2.8 2.8 3.1 1.8 1.8 2 ...
##  $ year        : int [1:234] 1999 1999 2008 2008 1999 1999 2008 1999 1999 2008 ...
##  $ cyl         : int [1:234] 4 4 4 4 6 6 6 4 4 4 ...
##  $ trans       : chr [1:234] "auto(l5)" "manual(m5)" "manual(m6)" "auto(av)" ...
##  $ drv         : chr [1:234] "f" "f" "f" "f" ...
##  $ cty         : int [1:234] 18 21 20 21 16 18 18 18 16 20 ...
##  $ hwy         : int [1:234] 29 29 31 30 26 26 27 26 25 28 ...
##  $ fl          : chr [1:234] "p" "p" "p" "p" ...
##  $ class       : chr [1:234] "compact" "compact" "compact" "compact" ...
qplot(displ,hwy,data =mpg)

qplot(cty,cyl,data=mpg)

qplot(displ,cyl,data=mpg)

qplot(cyl,displ,data=mpg)

qplot(cty,displ,data=mpg)

qplot(displ,hwy,data=mpg,color=drv)

qplot(displ,hwy,data=mpg,color=fl)

qplot(displ,hwy,data=mpg,color=class)

View(mpg)

qplot(displ, hwy, data = mpg, geom = c("point", "smooth"))
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

slide 12 (R packages: Tidyverse)

Aesthetic Mappings

ggplot(data = ) + (mapping=aes())

#install.packages("tidyverse")
#install.packages(c("nycflights13", "gapminder", "Lahman"))

library(ggplot2)
#to know about mpg data set
ggplot(data = mpg) + 
  geom_point (mapping = aes(x = displ, y = hwy))

ggplot(data = mpg) +
  geom_point(mapping = aes(x = displ, y = hwy, color = class))

ggplot(data = mpg) +
  geom_point(mapping = aes(x = displ, y = hwy, size = class))
## Warning: Using size for a discrete variable is not advised.

#to get rid of warnings in the previous plot:

ggplot(data = mpg) + 
  geom_point(mapping =aes(x = displ, y = hwy, size = displ))

#adding color "blue" instead of default color "black"
ggplot(data = mpg) + geom_point(mapping =
aes(x = displ, y = hwy, size =
displ),color="blue")

### Facets

ggplot(data = mpg) + 
  geom_point(mapping =aes(x = displ, y = hwy)) + 
  facet_wrap(~class, nrow = 2)

Geometric Objects

# left
ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy))

# right
ggplot(data = mpg) +
  geom_smooth(mapping = aes(x = displ, y = hwy))
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

ggplot(data = mpg) +
  geom_smooth (mapping = aes(x = displ,y = hwy, linetype = drv))
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

ggplot(data = mpg) +
  geom_smooth(mapping = aes(x = displ, y = hwy, color=drv))
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

ggplot(data = mpg) +
  geom_smooth(mapping = aes(x = displ, y = hwy))
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

ggplot(data = mpg) + 
  geom_smooth(mapping = aes(x = displ, y = hwy, group = drv))
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

ggplot(data = mpg) + 
  geom_smooth(mapping = aes(x = displ, y = hwy, color = drv),show.legend = F)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

Statistical Transformations

ggplot(data = diamonds) +
  stat_count(mapping = aes(x = cut))

ggplot(data = diamonds) +
  geom_bar(mapping = aes(x = cut, y =..prop.., group = 1))
## Warning: The dot-dot notation (`..prop..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(prop)` instead.

ggplot(data = diamonds) +
stat_summary(
mapping = aes(x = cut, y = depth),
fun.ymin = min,
fun.ymax = max,
fun.y = median
)
## Warning: The `fun.y` argument of `stat_summary()` is deprecated as of ggplot2 3.3.0.
## ℹ Please use the `fun` argument instead.
## Warning: The `fun.ymin` argument of `stat_summary()` is deprecated as of ggplot2 3.3.0.
## ℹ Please use the `fun.min` argument instead.
## Warning: The `fun.ymax` argument of `stat_summary()` is deprecated as of ggplot2 3.3.0.
## ℹ Please use the `fun.max` argument instead.

ggplot(data = diamonds) +
stat_summary(
mapping = aes(x = cut, y = price),
fun.ymin = min,
fun.ymax = max,
fun.y = median
)

Position Adjustments

ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, color =
cut))

ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill =
cut))

ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill =
clarity))

ggplot(data = diamonds,
mapping = aes(x = cut, fill = clarity)
) +
geom_bar(alpha = 1/5, position = "identity")

ggplot( data = diamonds,
mapping = aes(x = cut, color = clarity)
) +
geom_bar(fill = NA, position = "identity")

INCOURSE SOLVE

1st incourse

2019

Q1

set.seed(100)
x<-rnorm(n = 100,mean = 15,sd = 4)
mean(x)
## [1] 15
var(x)
## [1] 17
min(x)
## [1] 5.9
max(x)
## [1] 25
hist(x)

Q2

set.seed(100)
x<-runif(n=10,min=10,20)
2a
c<-matrix(x,2,5,byrow =T)
c
##      [,1] [,2] [,3] [,4] [,5]
## [1,]   13   13   16   11   15
## [2,]   15   18   14   15   12
2b
d<-matrix(x,nrow = 5,ncol=2,byrow = F)
d
##      [,1] [,2]
## [1,]   13   15
## [2,]   13   18
## [3,]   16   14
## [4,]   11   15
## [5,]   15   12

Q3

a<-matrix(data = 1:24,nrow = 6,ncol = 4,byrow = T)
rownames(a)<-c("R1","R2","R3","R4","R5","R6")
colnames(a)<-c("C1","C2","C3","C4")
a
##    C1 C2 C3 C4
## R1  1  2  3  4
## R2  5  6  7  8
## R3  9 10 11 12
## R4 13 14 15 16
## R5 17 18 19 20
## R6 21 22 23 24
or
a<-c(1:24)
rnam<-c("R1","R2","R3","R4","R5","R6")
cnam<-c("C1","C2","C3","C4")
x<-matrix(a,nrow=6,ncol=4,byrow = T,dimnames = list(rnam,cnam))
x
##    C1 C2 C3 C4
## R1  1  2  3  4
## R2  5  6  7  8
## R3  9 10 11 12
## R4 13 14 15 16
## R5 17 18 19 20
## R6 21 22 23 24

Q5

age<-c(10,5,3,3,12,8,6,7,7,4,15,7,13,10,12)
weight<-c(30,20,15,11,35,28,16,19,22,20,55,27,46,35,39)
mean(age)
## [1] 8.1
mean(weight)
## [1] 28
sd(age)
## [1] 3.7
sd(weight)
## [1] 12
cor(age,weight)
## [1] 0.95
plot(age,weight)
abline(lm(weight~age))

#create scatter plot of x vs. y plot(x, y)

#add line of best fit to scatter plot abline(lm(y ~ x))

Q6

x<-c(1:120)
rnames<-c(1:10)
cnames<-c("a","b","c","d","e","f","g","h","i","j","k","l")
y<-matrix(x,nrow=10,ncol=12,byrow = T,dimnames = list(rnames,cnames))
y
##      a   b   c   d   e   f   g   h   i   j   k   l
## 1    1   2   3   4   5   6   7   8   9  10  11  12
## 2   13  14  15  16  17  18  19  20  21  22  23  24
## 3   25  26  27  28  29  30  31  32  33  34  35  36
## 4   37  38  39  40  41  42  43  44  45  46  47  48
## 5   49  50  51  52  53  54  55  56  57  58  59  60
## 6   61  62  63  64  65  66  67  68  69  70  71  72
## 7   73  74  75  76  77  78  79  80  81  82  83  84
## 8   85  86  87  88  89  90  91  92  93  94  95  96
## 9   97  98  99 100 101 102 103 104 105 106 107 108
## 10 109 110 111 112 113 114 115 116 117 118 119 120
sm<-y[,c(2,6,8)]
sm
##      b   f   h
## 1    2   6   8
## 2   14  18  20
## 3   26  30  32
## 4   38  42  44
## 5   50  54  56
## 6   62  66  68
## 7   74  78  80
## 8   86  90  92
## 9   98 102 104
## 10 110 114 116

Q7

dose<-c(10,15,20,30,45,50,60,65,70,80)
drugA<-c(16,18,24,32,40,48,50,65,65,80)
drugB<-c(15,16,20,25,35,42,47,55,56,70)
plot(x = dose,y =  drugA, type="b",
     pch=15, lty=1, col="red", ylim=c(0, 80),
     main="Drug A vs. Drug B",
     xlab="Drug Dosage", ylab="Drug Response")
lines(x = dose,y =  drugB, type="b",
      pch=17, lty=2, col="blue")
abline(h=c(30), lwd=1.5, lty=2, col="gray")
legend("topleft", inset=.05, title="Drug Type", c("A","B"),
       lty=c(1, 2), pch=c(15, 17), col=c("red", "blue"))

Q8

rm(list = ls())

library(ggplot2)
attach(mtcars)
## The following objects are masked from mtcars (pos = 3):
## 
##     am, carb, cyl, disp, drat, gear, hp, mpg, qsec, vs, wt
## The following object is masked from package:ggplot2:
## 
##     mpg
plot(wt,mpg)

plot(mpg,disp)

hist(mpg)

boxplot(wt)

detach(mtcars)

2020

Q.1

1a
sample<-(rpois(n = 500,lambda = 2))
mean(sample)
## [1] 2
var(sample)
## [1] 2
min(sample)
## [1] 0
max(sample)
## [1] 7
1b
hist(sample)

Q.2

sam<-rnorm(n = 16,mean = 5,sd = sqrt(2))
sam
##  [1] 4.4 2.3 7.7 3.2 4.3 5.3 5.6 3.3 5.4 6.3 5.7 6.2 3.7 4.8 6.6 5.7
a
matrix(data = sam,nrow = 2)
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,]  4.4  7.7  4.3  5.6  5.4  5.7  3.7  6.6
## [2,]  2.3  3.2  5.3  3.3  6.3  6.2  4.8  5.7
b
matrix(data = sam,ncol = 2,byrow = FALSE)
##      [,1] [,2]
## [1,]  4.4  5.4
## [2,]  2.3  6.3
## [3,]  7.7  5.7
## [4,]  3.2  6.2
## [5,]  4.3  3.7
## [6,]  5.3  4.8
## [7,]  5.6  6.6
## [8,]  3.3  5.7

Q.3

The attach() function adds the data frame to R search path attach (mtcars)

The detach() function removes the data frame from the search path

detach(mtcars)

The with()function evaliate an expression in R environment

with(data = mtcars,summary(mpg))
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##      10      15      19      20      23      34

Q.4

id<-c(1:16)
iq<-c(180,150,140,110,122,181,186,170,117,142,115,175,103,90,101,135)
math<-c(85,60,70,71,55,78,90,89,75,65,55,80,46,40,39,60)
data.frame(id,iq,math)
##    id  iq math
## 1   1 180   85
## 2   2 150   60
## 3   3 140   70
## 4   4 110   71
## 5   5 122   55
## 6   6 181   78
## 7   7 186   90
## 8   8 170   89
## 9   9 117   75
## 10 10 142   65
## 11 11 115   55
## 12 12 175   80
## 13 13 103   46
## 14 14  90   40
## 15 15 101   39
## 16 16 135   60
#ans
mean(iq)
## [1] 139
sd(iq)
## [1] 32
mean(math)
## [1] 66
sd(math)
## [1] 16
cor(iq,math)
## [1] 0.85
cor.test(iq,math)
## 
##  Pearson's product-moment correlation
## 
## data:  iq and math
## t = 6, df = 14, p-value = 3e-05
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.61 0.95
## sample estimates:
##  cor 
## 0.85
plot(iq,math)
abline(lm(math~iq))

#### Q.5

d=matrix(data = 1:110,nrow = 10)

#for column label
colnames(d )<-c("a","b","c","d","e","f","g","h","i","j","k")
d
##        a  b  c  d  e  f  g  h  i   j   k
##  [1,]  1 11 21 31 41 51 61 71 81  91 101
##  [2,]  2 12 22 32 42 52 62 72 82  92 102
##  [3,]  3 13 23 33 43 53 63 73 83  93 103
##  [4,]  4 14 24 34 44 54 64 74 84  94 104
##  [5,]  5 15 25 35 45 55 65 75 85  95 105
##  [6,]  6 16 26 36 46 56 66 76 86  96 106
##  [7,]  7 17 27 37 47 57 67 77 87  97 107
##  [8,]  8 18 28 38 48 58 68 78 88  98 108
##  [9,]  9 19 29 39 49 59 69 79 89  99 109
## [10,] 10 20 30 40 50 60 70 80 90 100 110
#for row label
rownames(d )<-c("a","b","c","d","e","f","g","h","i","j")
d
##    a  b  c  d  e  f  g  h  i   j   k
## a  1 11 21 31 41 51 61 71 81  91 101
## b  2 12 22 32 42 52 62 72 82  92 102
## c  3 13 23 33 43 53 63 73 83  93 103
## d  4 14 24 34 44 54 64 74 84  94 104
## e  5 15 25 35 45 55 65 75 85  95 105
## f  6 16 26 36 46 56 66 76 86  96 106
## g  7 17 27 37 47 57 67 77 87  97 107
## h  8 18 28 38 48 58 68 78 88  98 108
## i  9 19 29 39 49 59 69 79 89  99 109
## j 10 20 30 40 50 60 70 80 90 100 110
#for 2nd 4th 8th coloums
d[,c(2,4,8)]
##    b  d  h
## a 11 31 71
## b 12 32 72
## c 13 33 73
## d 14 34 74
## e 15 35 75
## f 16 36 76
## g 17 37 77
## h 18 38 78
## i 19 39 79
## j 20 40 80
#for 2nd 4th 8th rows
d[c(2,4,8),]
##   a  b  c  d  e  f  g  h  i  j   k
## b 2 12 22 32 42 52 62 72 82 92 102
## d 4 14 24 34 44 54 64 74 84 94 104
## h 8 18 28 38 48 58 68 78 88 98 108
alternative
colnames<-c("a","b","c","d","e","f","g","h","i","j","k")
rownames<-c("a","b","c","d","e","f","g","h","i","j")

matrix(data = 1:110,nrow = 10,dimnames = list(rownames,colnames))
##    a  b  c  d  e  f  g  h  i   j   k
## a  1 11 21 31 41 51 61 71 81  91 101
## b  2 12 22 32 42 52 62 72 82  92 102
## c  3 13 23 33 43 53 63 73 83  93 103
## d  4 14 24 34 44 54 64 74 84  94 104
## e  5 15 25 35 45 55 65 75 85  95 105
## f  6 16 26 36 46 56 66 76 86  96 106
## g  7 17 27 37 47 57 67 77 87  97 107
## h  8 18 28 38 48 58 68 78 88  98 108
## i  9 19 29 39 49 59 69 79 89  99 109
## j 10 20 30 40 50 60 70 80 90 100 110

Q.6

doses<-c(10,15,20,30,45,50,60,65,70,80,85,90)
doses
##  [1] 10 15 20 30 45 50 60 65 70 80 85 90
drugc<-c(16,18,24,32,40,48,50,65,65,80,82,87)
drugc
##  [1] 16 18 24 32 40 48 50 65 65 80 82 87
drugd<-c(15,16,20,25,35,42,47,55,56,70,75,79)
drugd
##  [1] 15 16 20 25 35 42 47 55 56 70 75 79
#two different way combining data

data.frame(doses,drugc,drugd)
##    doses drugc drugd
## 1     10    16    15
## 2     15    18    16
## 3     20    24    20
## 4     30    32    25
## 5     45    40    35
## 6     50    48    42
## 7     60    50    47
## 8     65    65    55
## 9     70    65    56
## 10    80    80    70
## 11    85    82    75
## 12    90    87    79
cbind(doses,drugc,drugd)
##       doses drugc drugd
##  [1,]    10    16    15
##  [2,]    15    18    16
##  [3,]    20    24    20
##  [4,]    30    32    25
##  [5,]    45    40    35
##  [6,]    50    48    42
##  [7,]    60    50    47
##  [8,]    65    65    55
##  [9,]    70    65    56
## [10,]    80    80    70
## [11,]    85    82    75
## [12,]    90    87    79
##main part
plot(x = doses,y = drugc,type = "b",main = "Drug c vs Drug d",xlab ="Drug Doses", ylab = "Response to Drug",col="red",pch="c")
lines(x = doses,y = drugd,type = "b",col="blue",pch="d")
legend("topleft",title = "Durg types",c("C","D"),fill=c("red",'blue'))

Q.7

attach(mtcars)
## The following objects are masked from mtcars (pos = 3):
## 
##     am, carb, cyl, disp, drat, gear, hp, mpg, qsec, vs, wt
## The following object is masked from package:ggplot2:
## 
##     mpg
7a
plot(wt,hp)

7b
plot(mpg,disp)

7c
hist(disp)

7d
boxplot(mpg)

detach(mtcars)

2021(same 19)

Q1

set.seed(100)
x<-rnorm(n = 100,mean = 15,sd = 4)
mean(x)
## [1] 15
var(x)
## [1] 17
min(x)
## [1] 5.9
max(x)
## [1] 25
hist(x)

Q2

set.seed(13)
x<-runif(n=25,min=5,10)
2a
c<-matrix(x,nrow=2,byrow =T)
## Warning in matrix(x, nrow = 2, byrow = T): data length [25] is not a
## sub-multiple or multiple of the number of rows [2]
c
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
## [1,]  8.6  6.2  6.9  5.5  9.8  5.1  7.9  8.8  9.4   5.2   8.3   9.4   9.5
## [2,]  7.8  8.0  6.8  6.8  8.0  9.3  8.4  5.7  7.7   8.4   7.6   5.4   8.6
2b
d<-matrix(x,ncol=2,byrow = F)
## Warning in matrix(x, ncol = 2, byrow = F): data length [25] is not a
## sub-multiple or multiple of the number of rows [13]
d
##       [,1] [,2]
##  [1,]  8.6  7.8
##  [2,]  6.2  8.0
##  [3,]  6.9  6.8
##  [4,]  5.5  6.8
##  [5,]  9.8  8.0
##  [6,]  5.1  9.3
##  [7,]  7.9  8.4
##  [8,]  8.8  5.7
##  [9,]  9.4  7.7
## [10,]  5.2  8.4
## [11,]  8.3  7.6
## [12,]  9.4  5.4
## [13,]  9.5  8.6

Q3

a<-matrix(data = 1:24,nrow = 6,ncol = 4,byrow = T)
rownames(a)<-c("R1","R2","R3","R4","R5","R6")
colnames(a)<-c("C1","C2","C3","C4")
a
##    C1 C2 C3 C4
## R1  1  2  3  4
## R2  5  6  7  8
## R3  9 10 11 12
## R4 13 14 15 16
## R5 17 18 19 20
## R6 21 22 23 24
or
a<-c(1:24)
rnam<-c("R1","R2","R3","R4","R5","R6")
cnam<-c("C1","C2","C3","C4")
x<-matrix(a,nrow=6,ncol=4,byrow = T,dimnames = list(rnam,cnam))
x
##    C1 C2 C3 C4
## R1  1  2  3  4
## R2  5  6  7  8
## R3  9 10 11 12
## R4 13 14 15 16
## R5 17 18 19 20
## R6 21 22 23 24

Q5

age<-c(10,5,3,3,12,8,6,7,7,4,15,7,13,10,12)
weight<-c(30,20,15,11,35,28,16,19,22,20,55,27,46,35,39)
mean(age)
## [1] 8.1
mean(weight)
## [1] 28
sd(age)
## [1] 3.7
sd(weight)
## [1] 12
cor(age,weight)
## [1] 0.95
plot(age,weight)
abline(lm(weight~age))

#create scatter plot of x vs. y plot(x, y)

#add line of best fit to scatter plot abline(lm(y ~ x))

Q6

x<-c(1:120)
rnames<-c(1:10)
cnames<-c("a","b","c","d","e","f","g","h","i","j","k","l")
y<-matrix(x,nrow=10,ncol=12,byrow = T,dimnames = list(rnames,cnames))
y
##      a   b   c   d   e   f   g   h   i   j   k   l
## 1    1   2   3   4   5   6   7   8   9  10  11  12
## 2   13  14  15  16  17  18  19  20  21  22  23  24
## 3   25  26  27  28  29  30  31  32  33  34  35  36
## 4   37  38  39  40  41  42  43  44  45  46  47  48
## 5   49  50  51  52  53  54  55  56  57  58  59  60
## 6   61  62  63  64  65  66  67  68  69  70  71  72
## 7   73  74  75  76  77  78  79  80  81  82  83  84
## 8   85  86  87  88  89  90  91  92  93  94  95  96
## 9   97  98  99 100 101 102 103 104 105 106 107 108
## 10 109 110 111 112 113 114 115 116 117 118 119 120
sm<-y[,c(2,6,8)]
sm
##      b   f   h
## 1    2   6   8
## 2   14  18  20
## 3   26  30  32
## 4   38  42  44
## 5   50  54  56
## 6   62  66  68
## 7   74  78  80
## 8   86  90  92
## 9   98 102 104
## 10 110 114 116

Q7

dose<-c(10,15,20,30,45,50,60,65,70,80)
drugA<-c(16,18,24,32,40,48,50,65,65,80)
drugB<-c(15,16,20,25,35,42,47,55,56,70)
plot(x = dose,y =  drugA, type="b",
     pch=15, lty=1, col="red", ylim=c(0, 80),
     main="Drug A vs. Drug B",
     xlab="Drug Dosage", ylab="Drug Response")
lines(x = dose,y =  drugB, type="b",
      pch=17, lty=2, col="blue")
abline(h=c(30), lwd=1.5, lty=2, col="gray")
legend("topleft", inset=.05, title="Drug Type", c("A","B"),
       lty=c(1, 2), pch=c(15, 17), col=c("red", "blue"))

Q8

attach(mtcars)
## The following objects are masked from mtcars (pos = 3):
## 
##     am, carb, cyl, disp, drat, gear, hp, mpg, qsec, vs, wt
## The following object is masked from package:ggplot2:
## 
##     mpg
plot(wt,mpg)

plot(mpg,disp)

hist(mpg)

boxplot(wt)

2022

Q1

1a

*how can you trace a working directory in r ?

To trace the current working directory in R, you can use the getwd() function. This function returns the current working directory as a character string.

To use this function, simply type getwd() into the R console and press enter. The output will be the current working directory.

Alternatively, you can also use the dir() function to list the files and directories in the current working directory. This function takes an optional argument that specifies the directory to list, so you can use dir(getwd()) to list the contents of the current working directory.

You can also change the current working directory using the setwd() function. This function takes a character string argument specifying the path to the new working directory. For example, to change the working directory to a folder named “data” located in the current working directory, you could use setwd(“data”).

It’s important to note that when working with file paths in R, you should use forward slashes (/) instead of backslashes () on Windows systems. For example, to specify a file path in the “data” folder located in the current working directory on a Windows system, you would use “./data” instead of “.”.

1b

What is necessity of changing working directory in r?

Changing the working directory in R is important when you are working with files or data that are located in a different directory than the one where R is currently running.

When you are working with files in R, you often need to specify the path to the file. If the file is located in a different directory than the one where R is currently running, you need to provide the full path to the file. Changing the working directory to the directory where the file is located allows you to simply use the file name instead of the full path, which can save time and reduce the chance of errors.

In addition, changing the working directory can make it easier to organize your files and keep your code organized. For example, you might keep your data files in a separate directory from your R scripts. By changing the working directory to the data directory, you can more easily access and work with the data files in your R scripts.

Finally, some R packages, such as shiny, require that the working directory be set correctly in order to work properly. For example, if you are building a shiny app that reads in data from a file, you need to set the working directory to the location of the file in order for the app to work correctly.

Overall, changing the working directory in R can make it easier to work with files, keep your code organized, and ensure that your code works correctly with R packages that require the working directory to be set properly.

Q2

2a
data("rivers")
rivers
##   [1]  735  320  325  392  524  450 1459  135  465  600  330  336  280  315  870
##  [16]  906  202  329  290 1000  600  505 1450  840 1243  890  350  407  286  280
##  [31]  525  720  390  250  327  230  265  850  210  630  260  230  360  730  600
##  [46]  306  390  420  291  710  340  217  281  352  259  250  470  680  570  350
##  [61]  300  560  900  625  332 2348 1171 3710 2315 2533  780  280  410  460  260
##  [76]  255  431  350  760  618  338  981 1306  500  696  605  250  411 1054  735
##  [91]  233  435  490  310  460  383  375 1270  545  445 1885  380  300  380  377
## [106]  425  276  210  800  420  350  360  538 1100 1205  314  237  610  360  540
## [121] 1038  424  310  300  444  301  268  620  215  652  900  525  246  360  529
## [136]  500  720  270  430  671 1770
summary(rivers)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     135     310     425     591     680    3710
2b
hist(rivers)

boxplot(rivers)

Q3

3a
data(stackloss)
stackloss
##    Air.Flow Water.Temp Acid.Conc. stack.loss
## 1        80         27         89         42
## 2        80         27         88         37
## 3        75         25         90         37
## 4        62         24         87         28
## 5        62         22         87         18
## 6        62         23         87         18
## 7        62         24         93         19
## 8        62         24         93         20
## 9        58         23         87         15
## 10       58         18         80         14
## 11       58         18         89         14
## 12       58         17         88         13
## 13       58         18         82         11
## 14       58         19         93         12
## 15       50         18         89          8
## 16       50         18         86          7
## 17       50         19         72          8
## 18       50         19         79          8
## 19       50         20         80          9
## 20       56         20         82         15
## 21       70         20         91         15
plot(stackloss$Air.Flow,stackloss$stack.loss)
abline(lm(stackloss$stack.loss~stackloss$Air.Flow))

plot(stackloss$Water.Temp,stackloss$stack.loss)
abline(lm(stackloss$stack.loss~stackloss$Water.Temp))

plot(stackloss$Acid.Conc.,stackloss$stack.loss)
abline(lm(stackloss$stack.loss~stackloss$Acid.Conc.))

3b
cor(stackloss$Air.Flow,stackloss$stack.loss)
## [1] 0.92
cor(stackloss$Water.Temp,stackloss$stack.loss)
## [1] 0.88
cor(stackloss$Acid.Conc.,stackloss$stack.loss)
## [1] 0.4
3c
lm(data = stackloss,stack.loss~Air.Flow+Water.Temp+Acid.Conc.)
## 
## Call:
## lm(formula = stack.loss ~ Air.Flow + Water.Temp + Acid.Conc., 
##     data = stackloss)
## 
## Coefficients:
## (Intercept)     Air.Flow   Water.Temp   Acid.Conc.  
##     -39.920        0.716        1.295       -0.152

Q4

x<-c(1,2,3,4,5,6,7,8,9,10,11,12)
a<-c(73.3,  75.9,  89.2,  88.3,  90.0, 100.0,  85.4, 103.0,  91.2,  65.7,  63.3,  75.4)
b<-c(67.3,  59.5,  74.7,  58.3,  72.0,  48.3,  66.0,  75.6,  61.3,  50.6,  59.7,  61.0)
c<-c(104.0, 142.5,  80.1,  51.0,  70.1,  83.3, 109.8, 126.3, 104.4, 103.6, 132.2, 102.3)
plot(x = x,y = a,main="sunpot",type = "b",col="red" )
lines(x=x,y=b,col="blue")
lines(x=x,y=c)

Q5

data(attitude)
attitude
##    rating complaints privileges learning raises critical advance
## 1      43         51         30       39     61       92      45
## 2      63         64         51       54     63       73      47
## 3      71         70         68       69     76       86      48
## 4      61         63         45       47     54       84      35
## 5      81         78         56       66     71       83      47
## 6      43         55         49       44     54       49      34
## 7      58         67         42       56     66       68      35
## 8      71         75         50       55     70       66      41
## 9      72         82         72       67     71       83      31
## 10     67         61         45       47     62       80      41
## 11     64         53         53       58     58       67      34
## 12     67         60         47       39     59       74      41
## 13     69         62         57       42     55       63      25
## 14     68         83         83       45     59       77      35
## 15     77         77         54       72     79       77      46
## 16     81         90         50       72     60       54      36
## 17     74         85         64       69     79       79      63
## 18     65         60         65       75     55       80      60
## 19     65         70         46       57     75       85      46
## 20     50         58         68       54     64       78      52
## 21     50         40         33       34     43       64      33
## 22     64         61         52       62     66       80      41
## 23     53         66         52       50     63       80      37
## 24     40         37         42       58     50       57      49
## 25     63         54         42       48     66       75      33
## 26     66         77         66       63     88       76      72
## 27     78         75         58       74     80       78      49
## 28     48         57         44       45     51       83      38
## 29     85         85         71       71     77       74      55
## 30     82         82         39       59     64       78      39
#a
plot(attitude$rating ,attitude$complaints )

#b
abline(lm(attitude$complaints~attitude$rating))

5c
attitude$learning
##  [1] 39 54 69 47 66 44 56 55 67 47 58 39 42 45 72 72 69 75 57 54 34 62 50 58 48
## [26] 63 74 45 71 59
w<-cut(x = attitude$learning,breaks = c(30,45,60,75))
w
##  [1] (30,45] (45,60] (60,75] (45,60] (60,75] (30,45] (45,60] (45,60] (60,75]
## [10] (45,60] (45,60] (30,45] (30,45] (30,45] (60,75] (60,75] (60,75] (60,75]
## [19] (45,60] (45,60] (30,45] (60,75] (45,60] (45,60] (45,60] (60,75] (60,75]
## [28] (30,45] (60,75] (45,60]
## Levels: (30,45] (45,60] (60,75]
table(w)
## w
## (30,45] (45,60] (60,75] 
##       7      12      11

2nd incourse

2019

Question no. 01

The ls() code lists all of the objects in your workspace.

The rm() code removes objects in your workspace. You can begin your code with the rm() function to clear all of the objects from your workspace to start with a clean environment. This way the workspace is empty and everything you create is clearly visible.

rm(list=ls())

attach(trees)
par(mfrow = c(2,2))
plot(Girth, Height)
plot(Girth, Volume)
hist(Volume)
boxplot(Volume)

detach(trees)

Question no. 02

The swiss data set is built in R as a built in resource.

Recode the variable Infant.Mortality into four categories- the values upto first quartile as Good, first quartile until median as Normal, median until third quartile as Weaker and above third quartile as Poor.

a
#rm(list = ls())
#library(tidyverse) # needed for tibble & ggplot
#library(knitr) # needed for kable function

#tibble(swiss) # original data set
swiss
##              Fertility Agriculture Examination Education Catholic
## Courtelary          80        17.0          15        12     10.0
## Delemont            83        45.1           6         9     84.8
## Franches-Mnt        92        39.7           5         5     93.4
## Moutier             86        36.5          12         7     33.8
## Neuveville          77        43.5          17        15      5.2
## Porrentruy          76        35.3           9         7     90.6
## Broye               84        70.2          16         7     92.8
## Glane               92        67.8          14         8     97.2
## Gruyere             82        53.3          12         7     97.7
## Sarine              83        45.2          16        13     91.4
## Veveyse             87        64.5          14         6     98.6
## Aigle               64        62.0          21        12      8.5
## Aubonne             67        67.5          14         7      2.3
## Avenches            69        60.7          19        12      4.4
## Cossonay            62        69.3          22         5      2.8
## Echallens           68        72.6          18         2     24.2
## Grandson            72        34.0          17         8      3.3
## Lausanne            56        19.4          26        28     12.1
## La Vallee           54        15.2          31        20      2.1
## Lavaux              65        73.0          19         9      2.8
## Morges              66        59.8          22        10      5.2
## Moudon              65        55.1          14         3      4.5
## Nyone               57        50.9          22        12     15.1
## Orbe                57        54.1          20         6      4.2
## Oron                72        71.2          12         1      2.4
## Payerne             74        58.1          14         8      5.2
## Paysd'enhaut        72        63.5           6         3      2.6
## Rolle               60        60.8          16        10      7.7
## Vevey               58        26.8          25        19     18.5
## Yverdon             65        49.5          15         8      6.1
## Conthey             76        85.9           3         2     99.7
## Entremont           69        84.9           7         6     99.7
## Herens              77        89.7           5         2    100.0
## Martigwy            70        78.2          12         6     99.0
## Monthey             79        64.9           7         3     98.2
## St Maurice          65        75.9           9         9     99.1
## Sierre              92        84.6           3         3     99.5
## Sion                79        63.1          13        13     96.8
## Boudry              70        38.4          26        12      5.6
## La Chauxdfnd        66         7.7          29        11     13.8
## Le Locle            73        16.7          22        13     11.2
## Neuchatel           64        17.6          35        32     16.9
## Val de Ruz          78        37.6          15         7      5.0
## ValdeTravers        68        18.7          25         7      8.6
## V. De Geneve        35         1.2          37        53     42.3
## Rive Droite         45        46.6          16        29     50.4
## Rive Gauche         43        27.7          22        29     58.3
##              Infant.Mortality
## Courtelary                 22
## Delemont                   22
## Franches-Mnt               20
## Moutier                    20
## Neuveville                 21
## Porrentruy                 27
## Broye                      24
## Glane                      25
## Gruyere                    21
## Sarine                     24
## Veveyse                    24
## Aigle                      16
## Aubonne                    19
## Avenches                   23
## Cossonay                   19
## Echallens                  21
## Grandson                   20
## Lausanne                   20
## La Vallee                  11
## Lavaux                     20
## Morges                     18
## Moudon                     22
## Nyone                      17
## Orbe                       15
## Oron                       21
## Payerne                    24
## Paysd'enhaut               18
## Rolle                      16
## Vevey                      21
## Yverdon                    22
## Conthey                    15
## Entremont                  20
## Herens                     18
## Martigwy                   19
## Monthey                    20
## St Maurice                 18
## Sierre                     16
## Sion                       18
## Boudry                     20
## La Chauxdfnd               20
## Le Locle                   19
## Neuchatel                  23
## Val de Ruz                 20
## ValdeTravers               20
## V. De Geneve               18
## Rive Droite                18
## Rive Gauche                19
# ans
quantile(swiss$Infant.Mortality)
##   0%  25%  50%  75% 100% 
##   11   18   20   22   27
swiss <- within(data = swiss, expr = {
  status <- NA
  status[Infant.Mortality <= 18.15] <- "Good"
  status[Infant.Mortality > 18.15 & Infant.Mortality <= 20] <- "Normal"
  status[Infant.Mortality > 20 & Infant.Mortality <= 21.70] <- "Weaker"
  status[Infant.Mortality > 21.70] <- "Poor"
})
swiss
##              Fertility Agriculture Examination Education Catholic
## Courtelary          80        17.0          15        12     10.0
## Delemont            83        45.1           6         9     84.8
## Franches-Mnt        92        39.7           5         5     93.4
## Moutier             86        36.5          12         7     33.8
## Neuveville          77        43.5          17        15      5.2
## Porrentruy          76        35.3           9         7     90.6
## Broye               84        70.2          16         7     92.8
## Glane               92        67.8          14         8     97.2
## Gruyere             82        53.3          12         7     97.7
## Sarine              83        45.2          16        13     91.4
## Veveyse             87        64.5          14         6     98.6
## Aigle               64        62.0          21        12      8.5
## Aubonne             67        67.5          14         7      2.3
## Avenches            69        60.7          19        12      4.4
## Cossonay            62        69.3          22         5      2.8
## Echallens           68        72.6          18         2     24.2
## Grandson            72        34.0          17         8      3.3
## Lausanne            56        19.4          26        28     12.1
## La Vallee           54        15.2          31        20      2.1
## Lavaux              65        73.0          19         9      2.8
## Morges              66        59.8          22        10      5.2
## Moudon              65        55.1          14         3      4.5
## Nyone               57        50.9          22        12     15.1
## Orbe                57        54.1          20         6      4.2
## Oron                72        71.2          12         1      2.4
## Payerne             74        58.1          14         8      5.2
## Paysd'enhaut        72        63.5           6         3      2.6
## Rolle               60        60.8          16        10      7.7
## Vevey               58        26.8          25        19     18.5
## Yverdon             65        49.5          15         8      6.1
## Conthey             76        85.9           3         2     99.7
## Entremont           69        84.9           7         6     99.7
## Herens              77        89.7           5         2    100.0
## Martigwy            70        78.2          12         6     99.0
## Monthey             79        64.9           7         3     98.2
## St Maurice          65        75.9           9         9     99.1
## Sierre              92        84.6           3         3     99.5
## Sion                79        63.1          13        13     96.8
## Boudry              70        38.4          26        12      5.6
## La Chauxdfnd        66         7.7          29        11     13.8
## Le Locle            73        16.7          22        13     11.2
## Neuchatel           64        17.6          35        32     16.9
## Val de Ruz          78        37.6          15         7      5.0
## ValdeTravers        68        18.7          25         7      8.6
## V. De Geneve        35         1.2          37        53     42.3
## Rive Droite         45        46.6          16        29     50.4
## Rive Gauche         43        27.7          22        29     58.3
##              Infant.Mortality status
## Courtelary                 22   Poor
## Delemont                   22   Poor
## Franches-Mnt               20 Weaker
## Moutier                    20 Weaker
## Neuveville                 21 Weaker
## Porrentruy                 27   Poor
## Broye                      24   Poor
## Glane                      25   Poor
## Gruyere                    21 Weaker
## Sarine                     24   Poor
## Veveyse                    24   Poor
## Aigle                      16   Good
## Aubonne                    19 Normal
## Avenches                   23   Poor
## Cossonay                   19 Normal
## Echallens                  21 Weaker
## Grandson                   20 Normal
## Lausanne                   20 Weaker
## La Vallee                  11   Good
## Lavaux                     20 Normal
## Morges                     18   Good
## Moudon                     22   Poor
## Nyone                      17   Good
## Orbe                       15   Good
## Oron                       21 Weaker
## Payerne                    24   Poor
## Paysd'enhaut               18   Good
## Rolle                      16   Good
## Vevey                      21 Weaker
## Yverdon                    22   Poor
## Conthey                    15   Good
## Entremont                  20 Normal
## Herens                     18 Normal
## Martigwy                   19 Normal
## Monthey                    20 Weaker
## St Maurice                 18   Good
## Sierre                     16   Good
## Sion                       18   Good
## Boudry                     20 Weaker
## La Chauxdfnd               20 Weaker
## Le Locle                   19 Normal
## Neuchatel                  23   Poor
## Val de Ruz                 20 Normal
## ValdeTravers               20 Normal
## V. De Geneve               18   Good
## Rive Droite                18 Normal
## Rive Gauche                19 Normal
b
library(tidyverse) # needed for tibble & ggplot
## Warning: package 'tidyverse' was built under R version 4.2.2
## Warning: package 'tidyr' was built under R version 4.2.2
## Warning: package 'readr' was built under R version 4.2.2
## Warning: package 'purrr' was built under R version 4.2.2
## Warning: package 'dplyr' was built under R version 4.2.2
## Warning: package 'stringr' was built under R version 4.2.2
## Warning: package 'forcats' was built under R version 4.2.2
## Warning: package 'lubridate' was built under R version 4.2.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.0     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ lubridate 1.9.2     ✔ tibble    3.1.8
## ✔ purrr     1.0.1     ✔ tidyr     1.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ tidyr::expand()    masks reshape::expand()
## ✖ dplyr::filter()    masks stats::filter()
## ✖ dplyr::lag()       masks stats::lag()
## ✖ dplyr::rename()    masks reshape::rename()
## ✖ dplyr::select()    masks MASS::select()
## ✖ dplyr::src()       masks Hmisc::src()
## ✖ lubridate::stamp() masks reshape::stamp()
## ✖ dplyr::summarize() masks Hmisc::summarize()
## ℹ Use the ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors
ggplot(data = swiss)+
  geom_bar(mapping = aes(x = status), fill = "blue")

c
#Draw a random sample of size 10 from the swiss data set and calculate summary statistics for the variables Fertility and Education

set.seed(30)
new_swiss <- swiss[sample(x = nrow(swiss),size =  10),] 
# don't forget that comma at the end!!
new_swiss
##              Fertility Agriculture Examination Education Catholic
## Sarine              83          45          16        13     91.4
## Rive Droite         45          47          16        29     50.4
## Aubonne             67          68          14         7      2.3
## Aigle               64          62          21        12      8.5
## Rive Gauche         43          28          22        29     58.3
## Val de Ruz          78          38          15         7      5.0
## Vevey               58          27          25        19     18.5
## Moutier             86          36          12         7     33.8
## Franches-Mnt        92          40           5         5     93.4
## Entremont           69          85           7         6     99.7
##              Infant.Mortality status
## Sarine                     24   Poor
## Rive Droite                18 Normal
## Aubonne                    19 Normal
## Aigle                      16   Good
## Rive Gauche                19 Normal
## Val de Ruz                 20 Normal
## Vevey                      21 Weaker
## Moutier                    20 Weaker
## Franches-Mnt               20 Weaker
## Entremont                  20 Normal
summary(new_swiss$Fertility)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##      43      60      68      68      82      92
summary(new_swiss$Education)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     5.0     7.0     9.5    13.4    17.5    29.0

Question no. 03

Distinguish between FOR loop and WHILE loop by giving R executable examples.

For loop executes a statement repetitively till the variable’s value is contained in the sequence.The syntax is for(variable in sequence) statement Example:

for (i in 1:5) print("Hello world!")
## [1] "Hello world!"
## [1] "Hello world!"
## [1] "Hello world!"
## [1] "Hello world!"
## [1] "Hello world!"

While loop executes a statement repetitively till the condition is true The syntax is while(condition) statement Example:

i <-5
while(i > 0){
  print("Hello world!");
  i <- i-1
}
## [1] "Hello world!"
## [1] "Hello world!"
## [1] "Hello world!"
## [1] "Hello world!"
## [1] "Hello world!"

Question no. 04

Aggregates warpbreaks data for breaks by the variables wool and tension. Ignore the warnings and find the mean number of breaks corresponding to wool=A and tension=M

#rm(list = ls())
aggregate(warpbreaks, by = list(wool=warpbreaks$wool,tension=warpbreaks$tension), FUN=mean)
## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA

## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA

## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA

## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA

## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA

## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA

## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA

## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA

## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA

## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA

## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA

## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA
##   wool tension breaks wool tension
## 1    A       L     45   NA      NA
## 2    B       L     28   NA      NA
## 3    A       M     24   NA      NA
## 4    B       M     29   NA      NA
## 5    A       H     25   NA      NA
## 6    B       H     19   NA      NA
aggregate(warpbreaks,by=list(warpbreaks$wool,warpbreaks$tension),mean)
## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA

## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA

## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA

## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA

## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA

## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA

## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA

## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA

## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA

## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA

## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA

## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA
##   Group.1 Group.2 breaks wool tension
## 1       A       L     45   NA      NA
## 2       B       L     28   NA      NA
## 3       A       M     24   NA      NA
## 4       B       M     29   NA      NA
## 5       A       H     25   NA      NA
## 6       B       H     19   NA      NA
library(reshape)
m<-melt(data = warpbreaks,id= c('wool' ,'tension'))
m
##    wool tension variable value
## 1     A       L   breaks    26
## 2     A       L   breaks    30
## 3     A       L   breaks    54
## 4     A       L   breaks    25
## 5     A       L   breaks    70
## 6     A       L   breaks    52
## 7     A       L   breaks    51
## 8     A       L   breaks    26
## 9     A       L   breaks    67
## 10    A       M   breaks    18
## 11    A       M   breaks    21
## 12    A       M   breaks    29
## 13    A       M   breaks    17
## 14    A       M   breaks    12
## 15    A       M   breaks    18
## 16    A       M   breaks    35
## 17    A       M   breaks    30
## 18    A       M   breaks    36
## 19    A       H   breaks    36
## 20    A       H   breaks    21
## 21    A       H   breaks    24
## 22    A       H   breaks    18
## 23    A       H   breaks    10
## 24    A       H   breaks    43
## 25    A       H   breaks    28
## 26    A       H   breaks    15
## 27    A       H   breaks    26
## 28    B       L   breaks    27
## 29    B       L   breaks    14
## 30    B       L   breaks    29
## 31    B       L   breaks    19
## 32    B       L   breaks    29
## 33    B       L   breaks    31
## 34    B       L   breaks    41
## 35    B       L   breaks    20
## 36    B       L   breaks    44
## 37    B       M   breaks    42
## 38    B       M   breaks    26
## 39    B       M   breaks    19
## 40    B       M   breaks    16
## 41    B       M   breaks    39
## 42    B       M   breaks    28
## 43    B       M   breaks    21
## 44    B       M   breaks    39
## 45    B       M   breaks    29
## 46    B       H   breaks    20
## 47    B       H   breaks    21
## 48    B       H   breaks    24
## 49    B       H   breaks    17
## 50    B       H   breaks    13
## 51    B       H   breaks    15
## 52    B       H   breaks    15
## 53    B       H   breaks    16
## 54    B       H   breaks    28
cast(data = m,wool+tension ~ variable,mean)
##   wool tension breaks
## 1    A       L     45
## 2    A       M     24
## 3    A       H     25
## 4    B       L     28
## 5    B       M     29
## 6    B       H     19

Question no. 05

rm(list = ls())
p_weibull <- function(x, a, lambda){
    if (! x >= 0){
        0
    } else {
        if (! a > 0){
            "a must be greater than zero"
        } else {
            if (! lambda > 0){
                "lambda must be greater than zero" 
            } else {
                1-exp((-1*(lambda*x))**a)
            }
        }
    }
}

p_weibull(5,3,1) # function we created
## [1] 1
pweibull(5,3,1) # fuction built in 'stats' package
## [1] 1
p_weibull(1.0,3,2)
## [1] 1
pweibull(1.0,3,.5)
## [1] 1
p_weibull(1.5,3,5)
## [1] 1
pweibull(1.5,3,5^-1)
## [1] 1
p_weibull(-5,3,1)
## [1] 0
pweibull(-5,3,1)
## [1] 0
p_weibull(5,-3,1)
## [1] "a must be greater than zero"
pweibull(5,-3,1)
## Warning in pweibull(5, -3, 1): NaNs produced
## [1] NaN
p_weibull(5,3,-1)
## [1] "lambda must be greater than zero"
pweibull(5,3,-1)
## Warning in pweibull(5, 3, -1): NaNs produced
## [1] NaN

2020

Question no. 01

rm(list = ls())
breaks<-c(26,   30, 54, 47, 26, 48, 58, 76, 21, 56, 96, 30, 14, 45, 65, 32, 14, 85, 45)
wool<-c('A','A','A',    'B',    'B',    'B',    'C',    'C',    'C',    'D',    'D',    'D',    'D',    'D',    'D',    'E',    'E',    'E',    'E')
tension<-c('L','L', 'L',    'L',    'L',    'M',    'M',    'M',    'M',    'M',    'M',    'M',    'M',    'N',    'N',    'N','N',    'N',    'N')
data<-data.frame(breaks,wool,tension)
data
##    breaks wool tension
## 1      26    A       L
## 2      30    A       L
## 3      54    A       L
## 4      47    B       L
## 5      26    B       L
## 6      48    B       M
## 7      58    C       M
## 8      76    C       M
## 9      21    C       M
## 10     56    D       M
## 11     96    D       M
## 12     30    D       M
## 13     14    D       M
## 14     45    D       N
## 15     65    D       N
## 16     32    E       N
## 17     14    E       N
## 18     85    E       N
## 19     45    E       N
1a
quantile(breaks)
##   0%  25%  50%  75% 100% 
##   14   28   45   57   96
status<-NA
status[breaks<=28]<-"Good"
status[breaks>28 & breaks <=45 ]<-"Normal"
status[breaks>45 & breaks <=57 ]<-"Weaker"
status[breaks>57 ]<-"Poor"
status
##  [1] "Good"   "Normal" "Weaker" "Weaker" "Good"   "Weaker" "Poor"   "Poor"  
##  [9] "Good"   "Weaker" "Poor"   "Normal" "Good"   "Normal" "Poor"   "Normal"
## [17] "Good"   "Poor"   "Normal"
data$status<-status
data
##    breaks wool tension status
## 1      26    A       L   Good
## 2      30    A       L Normal
## 3      54    A       L Weaker
## 4      47    B       L Weaker
## 5      26    B       L   Good
## 6      48    B       M Weaker
## 7      58    C       M   Poor
## 8      76    C       M   Poor
## 9      21    C       M   Good
## 10     56    D       M Weaker
## 11     96    D       M   Poor
## 12     30    D       M Normal
## 13     14    D       M   Good
## 14     45    D       N Normal
## 15     65    D       N   Poor
## 16     32    E       N Normal
## 17     14    E       N   Good
## 18     85    E       N   Poor
## 19     45    E       N Normal
alternative
data<- within(data = data,expr = {
  quantile(breaks)
status1<-NULL
status1[breaks<=28]<-"Good"
status1[breaks>28 & breaks <=45 ]<-"Normal"
status1[breaks>45 & breaks <=57 ]<-"Weaker"
status1[breaks>57 ]<-"Poor"

status1
})
data
##    breaks wool tension status status1
## 1      26    A       L   Good    Good
## 2      30    A       L Normal  Normal
## 3      54    A       L Weaker  Weaker
## 4      47    B       L Weaker  Weaker
## 5      26    B       L   Good    Good
## 6      48    B       M Weaker  Weaker
## 7      58    C       M   Poor    Poor
## 8      76    C       M   Poor    Poor
## 9      21    C       M   Good    Good
## 10     56    D       M Weaker  Weaker
## 11     96    D       M   Poor    Poor
## 12     30    D       M Normal  Normal
## 13     14    D       M   Good    Good
## 14     45    D       N Normal  Normal
## 15     65    D       N   Poor    Poor
## 16     32    E       N Normal  Normal
## 17     14    E       N   Good    Good
## 18     85    E       N   Poor    Poor
## 19     45    E       N Normal  Normal
1b
library(tidyverse)

ggplot(data = data)+
  geom_bar(aes(status))

1c
set.seed(13)
sample<-data[sample(x = nrow(data),size = 5),]
sample
##    breaks wool tension status status1
## 3      54    A       L Weaker  Weaker
## 5      26    B       L   Good    Good
## 10     56    D       M Weaker  Weaker
## 13     14    D       M   Good    Good
## 6      48    B       M Weaker  Weaker
summary(wool)
##    Length     Class      Mode 
##        19 character character
summary(tension)
##    Length     Class      Mode 
##        19 character character

Question no. 03

3a

Detect the errors from the following codes & explain why they appear to be errors. (I) qplot(x1, x2, data=(), color=x3) data argument should be assigned to a valid data frame in which object x1, x2, x3 must exist.

  1. boxplot(nitrogen=Month, airquality, xlab=Month, ylab=Nitrogen) Nitrogen object does not exist in airquality data frame. We should replace it with Ozone. Also the formula is incorrect it should be written with ~ sign. Also x- and y-axis annotations should be characters not object.
3b

Write down the executable R codes to generate 30 random numbers from the following distributions:

#rm(list = ls())
set.seed(30)

# log normal
rlnorm(30)
##  [1] 0.28 0.71 0.59 3.57 6.20 0.22 1.12 0.47 0.51 1.32 0.36 0.16 0.51 0.94 2.41
## [16] 1.31 0.98 0.59 0.24 0.16 0.85 2.13 0.40 2.23 4.44 0.33 0.59 0.24 0.29 1.26
# geometric
rgeom(30, .4)
##  [1] 2 1 1 4 0 0 0 0 2 0 0 5 2 0 4 0 0 7 1 2 0 1 1 0 0 1 0 2 0 0
# gamma
round(rgamma(30, shape = 5), 2)
##  [1] 8.7 7.2 3.1 4.7 2.5 5.6 3.1 2.6 4.1 8.8 4.3 3.6 5.7 6.6 7.6 6.9 2.5 5.8 4.0
## [20] 6.8 2.7 5.1 4.7 5.1 5.0 2.0 3.5 6.0 3.4 6.4

Question no. 04

Interpret the following R codes (a) apply(data5, 2, median, trim=.12)

Calculate trimmed (based on middle 76 percent) column medians of the data object data5

  1. marks< − rnorm(50, 30, 50)

Create a numeric vector named marks by generating 50 random numbers that follow normal disrtibution with mean 30 and stadanrd deviation 50.

** results< − ifelse(marks>= 50, ”Passed”, ”Failed”)

Create a character vector named results of size 50, which include “Passed” if the marks obtained is 50 or more, and include “Failed” if marks obtained in less than 50.

  1. boxplot(oxygen∼year, weather, xlab=”year”, ylab=”oxygen”)

Create a boxplot by splitting oxygen (which must be a numeric vector) into year from the data object weather putting oxygen data in the y-axis and year in the x-axis.

Question no. 05

Write a fuction to calculate 1−e−λx where x≥0, λ is the rate parameter

#rm(list = ls())
p_exp <- function(x, lambda){
  if (! x >= 0){
    0
  } else {
    1-exp(-1*lambda*x)
  }
}
p_exp(1,4) # function we created 
## [1] 0.98
pexp(1,4) # function built in 'stats' package
## [1] 0.98
p_exp(-1,4) 
## [1] 0
pexp(-1,4)
## [1] 0
d_exp <- function(x, lambda){
  if (! x >= 0){
    0
  } else {
    exp(-1*lambda*x)*lambda
  }
}
d_exp(1,4) # function we created 
## [1] 0.073
dexp(x = 1,4) # function built in 'stats' package
## [1] 0.073
d_exp(-1,4) 
## [1] 0
dexp(-1,4)
## [1] 0

2021 (same 19)

Q1

Base plotting system, lattice system, and ggplot2 are all R packages that can be used for data visualization. Each has its own advantages and disadvantages, and the choice of which to use may depend on the specific needs of the user.

Base Plotting System: Advantages:

Base plotting system is the default plotting system in R, so it requires no additional packages or installation. It provides a lot of control over the appearance of plots, allowing users to customize almost every aspect of the plot. It has a very shallow learning curve, and is easy to use for simple plots. Disadvantages:

It can be difficult to use for more complex plots. The code required to produce even simple plots can be quite verbose and difficult to read. It produces static plots that cannot be easily updated or modified. Lattice System: Advantages:

The lattice system provides a wide range of statistical graphics with many advanced features, such as multi-panel displays and conditioning plots. It provides a simple syntax for producing complex graphics. It is well-suited for creating plots that show relationships between multiple variables. Disadvantages:

It can be difficult to customize plots to the degree that the base plotting system allows. It can be slower than other plotting systems when creating large numbers of plots. ggplot2: Advantages:

ggplot2 is a powerful and flexible plotting system, capable of creating complex and beautiful visualizations. It has a more intuitive syntax than the base plotting system, making it easier to use for complex plots. It is highly customizable, and allows users to create plots that are tailored to their needs. Disadvantages:

ggplot2 can have a steep learning curve for users unfamiliar with its syntax and structure. It may require additional packages to produce certain types of plots, adding to the complexity of the process. It can be slower than other plotting systems when creating large numbers of plots. In summary, the base plotting system is easy to use and highly customizable, but can be difficult to use for complex plots. The lattice system is well-suited for creating plots with multiple variables, but can be difficult to customize. ggplot2 is highly flexible and customizable, but may require additional packages and can have a steep learning curve. The choice of which to use may depend on the specific needs of the user, and the type of plot they are looking to create.

2022

Q1

1a

library(Hmisc) world95 summary(world95\(babymort) cor(world95\)lifeexpm,world95\(literacy, na.rm= T) abline(lm(world95\)literacy~world95$lifeexpm),)

Q3

3a
library(reshape)
ChickWeight
## Grouped Data: weight ~ Time | Chick
##     weight Time Chick Diet
## 1       42    0     1    1
## 2       51    2     1    1
## 3       59    4     1    1
## 4       64    6     1    1
## 5       76    8     1    1
## 6       93   10     1    1
## 7      106   12     1    1
## 8      125   14     1    1
## 9      149   16     1    1
## 10     171   18     1    1
## 11     199   20     1    1
## 12     205   21     1    1
## 13      40    0     2    1
## 14      49    2     2    1
## 15      58    4     2    1
## 16      72    6     2    1
## 17      84    8     2    1
## 18     103   10     2    1
## 19     122   12     2    1
## 20     138   14     2    1
## 21     162   16     2    1
## 22     187   18     2    1
## 23     209   20     2    1
## 24     215   21     2    1
## 25      43    0     3    1
## 26      39    2     3    1
## 27      55    4     3    1
## 28      67    6     3    1
## 29      84    8     3    1
## 30      99   10     3    1
## 31     115   12     3    1
## 32     138   14     3    1
## 33     163   16     3    1
## 34     187   18     3    1
## 35     198   20     3    1
## 36     202   21     3    1
## 37      42    0     4    1
## 38      49    2     4    1
## 39      56    4     4    1
## 40      67    6     4    1
## 41      74    8     4    1
## 42      87   10     4    1
## 43     102   12     4    1
## 44     108   14     4    1
## 45     136   16     4    1
## 46     154   18     4    1
## 47     160   20     4    1
## 48     157   21     4    1
## 49      41    0     5    1
## 50      42    2     5    1
## 51      48    4     5    1
## 52      60    6     5    1
## 53      79    8     5    1
## 54     106   10     5    1
## 55     141   12     5    1
## 56     164   14     5    1
## 57     197   16     5    1
## 58     199   18     5    1
## 59     220   20     5    1
## 60     223   21     5    1
## 61      41    0     6    1
## 62      49    2     6    1
## 63      59    4     6    1
## 64      74    6     6    1
## 65      97    8     6    1
## 66     124   10     6    1
## 67     141   12     6    1
## 68     148   14     6    1
## 69     155   16     6    1
## 70     160   18     6    1
## 71     160   20     6    1
## 72     157   21     6    1
## 73      41    0     7    1
## 74      49    2     7    1
## 75      57    4     7    1
## 76      71    6     7    1
## 77      89    8     7    1
## 78     112   10     7    1
## 79     146   12     7    1
## 80     174   14     7    1
## 81     218   16     7    1
## 82     250   18     7    1
## 83     288   20     7    1
## 84     305   21     7    1
## 85      42    0     8    1
## 86      50    2     8    1
## 87      61    4     8    1
## 88      71    6     8    1
## 89      84    8     8    1
## 90      93   10     8    1
## 91     110   12     8    1
## 92     116   14     8    1
## 93     126   16     8    1
## 94     134   18     8    1
## 95     125   20     8    1
## 96      42    0     9    1
## 97      51    2     9    1
## 98      59    4     9    1
## 99      68    6     9    1
## 100     85    8     9    1
## 101     96   10     9    1
## 102     90   12     9    1
## 103     92   14     9    1
## 104     93   16     9    1
## 105    100   18     9    1
## 106    100   20     9    1
## 107     98   21     9    1
## 108     41    0    10    1
## 109     44    2    10    1
## 110     52    4    10    1
## 111     63    6    10    1
## 112     74    8    10    1
## 113     81   10    10    1
## 114     89   12    10    1
## 115     96   14    10    1
## 116    101   16    10    1
## 117    112   18    10    1
## 118    120   20    10    1
## 119    124   21    10    1
## 120     43    0    11    1
## 121     51    2    11    1
## 122     63    4    11    1
## 123     84    6    11    1
## 124    112    8    11    1
## 125    139   10    11    1
## 126    168   12    11    1
## 127    177   14    11    1
## 128    182   16    11    1
## 129    184   18    11    1
## 130    181   20    11    1
## 131    175   21    11    1
## 132     41    0    12    1
## 133     49    2    12    1
## 134     56    4    12    1
## 135     62    6    12    1
## 136     72    8    12    1
## 137     88   10    12    1
## 138    119   12    12    1
## 139    135   14    12    1
## 140    162   16    12    1
## 141    185   18    12    1
## 142    195   20    12    1
## 143    205   21    12    1
## 144     41    0    13    1
## 145     48    2    13    1
## 146     53    4    13    1
## 147     60    6    13    1
## 148     65    8    13    1
## 149     67   10    13    1
## 150     71   12    13    1
## 151     70   14    13    1
## 152     71   16    13    1
## 153     81   18    13    1
## 154     91   20    13    1
## 155     96   21    13    1
## 156     41    0    14    1
## 157     49    2    14    1
## 158     62    4    14    1
## 159     79    6    14    1
## 160    101    8    14    1
## 161    128   10    14    1
## 162    164   12    14    1
## 163    192   14    14    1
## 164    227   16    14    1
## 165    248   18    14    1
## 166    259   20    14    1
## 167    266   21    14    1
## 168     41    0    15    1
## 169     49    2    15    1
## 170     56    4    15    1
## 171     64    6    15    1
## 172     68    8    15    1
## 173     68   10    15    1
## 174     67   12    15    1
## 175     68   14    15    1
## 176     41    0    16    1
## 177     45    2    16    1
## 178     49    4    16    1
## 179     51    6    16    1
## 180     57    8    16    1
## 181     51   10    16    1
## 182     54   12    16    1
## 183     42    0    17    1
## 184     51    2    17    1
## 185     61    4    17    1
## 186     72    6    17    1
## 187     83    8    17    1
## 188     89   10    17    1
## 189     98   12    17    1
## 190    103   14    17    1
## 191    113   16    17    1
## 192    123   18    17    1
## 193    133   20    17    1
## 194    142   21    17    1
## 195     39    0    18    1
## 196     35    2    18    1
## 197     43    0    19    1
## 198     48    2    19    1
## 199     55    4    19    1
## 200     62    6    19    1
## 201     65    8    19    1
## 202     71   10    19    1
## 203     82   12    19    1
## 204     88   14    19    1
## 205    106   16    19    1
## 206    120   18    19    1
## 207    144   20    19    1
## 208    157   21    19    1
## 209     41    0    20    1
## 210     47    2    20    1
## 211     54    4    20    1
## 212     58    6    20    1
## 213     65    8    20    1
## 214     73   10    20    1
## 215     77   12    20    1
## 216     89   14    20    1
## 217     98   16    20    1
## 218    107   18    20    1
## 219    115   20    20    1
## 220    117   21    20    1
## 221     40    0    21    2
## 222     50    2    21    2
## 223     62    4    21    2
## 224     86    6    21    2
## 225    125    8    21    2
## 226    163   10    21    2
## 227    217   12    21    2
## 228    240   14    21    2
## 229    275   16    21    2
## 230    307   18    21    2
## 231    318   20    21    2
## 232    331   21    21    2
## 233     41    0    22    2
## 234     55    2    22    2
## 235     64    4    22    2
## 236     77    6    22    2
## 237     90    8    22    2
## 238     95   10    22    2
## 239    108   12    22    2
## 240    111   14    22    2
## 241    131   16    22    2
## 242    148   18    22    2
## 243    164   20    22    2
## 244    167   21    22    2
## 245     43    0    23    2
## 246     52    2    23    2
## 247     61    4    23    2
## 248     73    6    23    2
## 249     90    8    23    2
## 250    103   10    23    2
## 251    127   12    23    2
## 252    135   14    23    2
## 253    145   16    23    2
## 254    163   18    23    2
## 255    170   20    23    2
## 256    175   21    23    2
## 257     42    0    24    2
## 258     52    2    24    2
## 259     58    4    24    2
## 260     74    6    24    2
## 261     66    8    24    2
## 262     68   10    24    2
## 263     70   12    24    2
## 264     71   14    24    2
## 265     72   16    24    2
## 266     72   18    24    2
## 267     76   20    24    2
## 268     74   21    24    2
## 269     40    0    25    2
## 270     49    2    25    2
## 271     62    4    25    2
## 272     78    6    25    2
## 273    102    8    25    2
## 274    124   10    25    2
## 275    146   12    25    2
## 276    164   14    25    2
## 277    197   16    25    2
## 278    231   18    25    2
## 279    259   20    25    2
## 280    265   21    25    2
## 281     42    0    26    2
## 282     48    2    26    2
## 283     57    4    26    2
## 284     74    6    26    2
## 285     93    8    26    2
## 286    114   10    26    2
## 287    136   12    26    2
## 288    147   14    26    2
## 289    169   16    26    2
## 290    205   18    26    2
## 291    236   20    26    2
## 292    251   21    26    2
## 293     39    0    27    2
## 294     46    2    27    2
## 295     58    4    27    2
## 296     73    6    27    2
## 297     87    8    27    2
## 298    100   10    27    2
## 299    115   12    27    2
## 300    123   14    27    2
## 301    144   16    27    2
## 302    163   18    27    2
## 303    185   20    27    2
## 304    192   21    27    2
## 305     39    0    28    2
## 306     46    2    28    2
## 307     58    4    28    2
## 308     73    6    28    2
## 309     92    8    28    2
## 310    114   10    28    2
## 311    145   12    28    2
## 312    156   14    28    2
## 313    184   16    28    2
## 314    207   18    28    2
## 315    212   20    28    2
## 316    233   21    28    2
## 317     39    0    29    2
## 318     48    2    29    2
## 319     59    4    29    2
## 320     74    6    29    2
## 321     87    8    29    2
## 322    106   10    29    2
## 323    134   12    29    2
## 324    150   14    29    2
## 325    187   16    29    2
## 326    230   18    29    2
## 327    279   20    29    2
## 328    309   21    29    2
## 329     42    0    30    2
## 330     48    2    30    2
## 331     59    4    30    2
## 332     72    6    30    2
## 333     85    8    30    2
## 334     98   10    30    2
## 335    115   12    30    2
## 336    122   14    30    2
## 337    143   16    30    2
## 338    151   18    30    2
## 339    157   20    30    2
## 340    150   21    30    2
## 341     42    0    31    3
## 342     53    2    31    3
## 343     62    4    31    3
## 344     73    6    31    3
## 345     85    8    31    3
## 346    102   10    31    3
## 347    123   12    31    3
## 348    138   14    31    3
## 349    170   16    31    3
## 350    204   18    31    3
## 351    235   20    31    3
## 352    256   21    31    3
## 353     41    0    32    3
## 354     49    2    32    3
## 355     65    4    32    3
## 356     82    6    32    3
## 357    107    8    32    3
## 358    129   10    32    3
## 359    159   12    32    3
## 360    179   14    32    3
## 361    221   16    32    3
## 362    263   18    32    3
## 363    291   20    32    3
## 364    305   21    32    3
## 365     39    0    33    3
## 366     50    2    33    3
## 367     63    4    33    3
## 368     77    6    33    3
## 369     96    8    33    3
## 370    111   10    33    3
## 371    137   12    33    3
## 372    144   14    33    3
## 373    151   16    33    3
## 374    146   18    33    3
## 375    156   20    33    3
## 376    147   21    33    3
## 377     41    0    34    3
## 378     49    2    34    3
## 379     63    4    34    3
## 380     85    6    34    3
## 381    107    8    34    3
## 382    134   10    34    3
## 383    164   12    34    3
## 384    186   14    34    3
## 385    235   16    34    3
## 386    294   18    34    3
## 387    327   20    34    3
## 388    341   21    34    3
## 389     41    0    35    3
## 390     53    2    35    3
## 391     64    4    35    3
## 392     87    6    35    3
## 393    123    8    35    3
## 394    158   10    35    3
## 395    201   12    35    3
## 396    238   14    35    3
## 397    287   16    35    3
## 398    332   18    35    3
## 399    361   20    35    3
## 400    373   21    35    3
## 401     39    0    36    3
## 402     48    2    36    3
## 403     61    4    36    3
## 404     76    6    36    3
## 405     98    8    36    3
## 406    116   10    36    3
## 407    145   12    36    3
## 408    166   14    36    3
## 409    198   16    36    3
## 410    227   18    36    3
## 411    225   20    36    3
## 412    220   21    36    3
## 413     41    0    37    3
## 414     48    2    37    3
## 415     56    4    37    3
## 416     68    6    37    3
## 417     80    8    37    3
## 418     83   10    37    3
## 419    103   12    37    3
## 420    112   14    37    3
## 421    135   16    37    3
## 422    157   18    37    3
## 423    169   20    37    3
## 424    178   21    37    3
## 425     41    0    38    3
## 426     49    2    38    3
## 427     61    4    38    3
## 428     74    6    38    3
## 429     98    8    38    3
## 430    109   10    38    3
## 431    128   12    38    3
## 432    154   14    38    3
## 433    192   16    38    3
## 434    232   18    38    3
## 435    280   20    38    3
## 436    290   21    38    3
## 437     42    0    39    3
## 438     50    2    39    3
## 439     61    4    39    3
## 440     78    6    39    3
## 441     89    8    39    3
## 442    109   10    39    3
## 443    130   12    39    3
## 444    146   14    39    3
## 445    170   16    39    3
## 446    214   18    39    3
## 447    250   20    39    3
## 448    272   21    39    3
## 449     41    0    40    3
## 450     55    2    40    3
## 451     66    4    40    3
## 452     79    6    40    3
## 453    101    8    40    3
## 454    120   10    40    3
## 455    154   12    40    3
## 456    182   14    40    3
## 457    215   16    40    3
## 458    262   18    40    3
## 459    295   20    40    3
## 460    321   21    40    3
## 461     42    0    41    4
## 462     51    2    41    4
## 463     66    4    41    4
## 464     85    6    41    4
## 465    103    8    41    4
## 466    124   10    41    4
## 467    155   12    41    4
## 468    153   14    41    4
## 469    175   16    41    4
## 470    184   18    41    4
## 471    199   20    41    4
## 472    204   21    41    4
## 473     42    0    42    4
## 474     49    2    42    4
## 475     63    4    42    4
## 476     84    6    42    4
## 477    103    8    42    4
## 478    126   10    42    4
## 479    160   12    42    4
## 480    174   14    42    4
## 481    204   16    42    4
## 482    234   18    42    4
## 483    269   20    42    4
## 484    281   21    42    4
## 485     42    0    43    4
## 486     55    2    43    4
## 487     69    4    43    4
## 488     96    6    43    4
## 489    131    8    43    4
## 490    157   10    43    4
## 491    184   12    43    4
## 492    188   14    43    4
## 493    197   16    43    4
## 494    198   18    43    4
## 495    199   20    43    4
## 496    200   21    43    4
## 497     42    0    44    4
## 498     51    2    44    4
## 499     65    4    44    4
## 500     86    6    44    4
## 501    103    8    44    4
## 502    118   10    44    4
## 503    127   12    44    4
## 504    138   14    44    4
## 505    145   16    44    4
## 506    146   18    44    4
## 507     41    0    45    4
## 508     50    2    45    4
## 509     61    4    45    4
## 510     78    6    45    4
## 511     98    8    45    4
## 512    117   10    45    4
## 513    135   12    45    4
## 514    141   14    45    4
## 515    147   16    45    4
## 516    174   18    45    4
## 517    197   20    45    4
## 518    196   21    45    4
## 519     40    0    46    4
## 520     52    2    46    4
## 521     62    4    46    4
## 522     82    6    46    4
## 523    101    8    46    4
## 524    120   10    46    4
## 525    144   12    46    4
## 526    156   14    46    4
## 527    173   16    46    4
## 528    210   18    46    4
## 529    231   20    46    4
## 530    238   21    46    4
## 531     41    0    47    4
## 532     53    2    47    4
## 533     66    4    47    4
## 534     79    6    47    4
## 535    100    8    47    4
## 536    123   10    47    4
## 537    148   12    47    4
## 538    157   14    47    4
## 539    168   16    47    4
## 540    185   18    47    4
## 541    210   20    47    4
## 542    205   21    47    4
## 543     39    0    48    4
## 544     50    2    48    4
## 545     62    4    48    4
## 546     80    6    48    4
## 547    104    8    48    4
## 548    125   10    48    4
## 549    154   12    48    4
## 550    170   14    48    4
## 551    222   16    48    4
## 552    261   18    48    4
## 553    303   20    48    4
## 554    322   21    48    4
## 555     40    0    49    4
## 556     53    2    49    4
## 557     64    4    49    4
## 558     85    6    49    4
## 559    108    8    49    4
## 560    128   10    49    4
## 561    152   12    49    4
## 562    166   14    49    4
## 563    184   16    49    4
## 564    203   18    49    4
## 565    233   20    49    4
## 566    237   21    49    4
## 567     41    0    50    4
## 568     54    2    50    4
## 569     67    4    50    4
## 570     84    6    50    4
## 571    105    8    50    4
## 572    122   10    50    4
## 573    155   12    50    4
## 574    175   14    50    4
## 575    205   16    50    4
## 576    234   18    50    4
## 577    264   20    50    4
## 578    264   21    50    4
x<-melt(data =ChickWeight,id=c("Diet"))
x
##      Diet variable value
## 1       1   weight    42
## 2       1   weight    51
## 3       1   weight    59
## 4       1   weight    64
## 5       1   weight    76
## 6       1   weight    93
## 7       1   weight   106
## 8       1   weight   125
## 9       1   weight   149
## 10      1   weight   171
## 11      1   weight   199
## 12      1   weight   205
## 13      1   weight    40
## 14      1   weight    49
## 15      1   weight    58
## 16      1   weight    72
## 17      1   weight    84
## 18      1   weight   103
## 19      1   weight   122
## 20      1   weight   138
## 21      1   weight   162
## 22      1   weight   187
## 23      1   weight   209
## 24      1   weight   215
## 25      1   weight    43
## 26      1   weight    39
## 27      1   weight    55
## 28      1   weight    67
## 29      1   weight    84
## 30      1   weight    99
## 31      1   weight   115
## 32      1   weight   138
## 33      1   weight   163
## 34      1   weight   187
## 35      1   weight   198
## 36      1   weight   202
## 37      1   weight    42
## 38      1   weight    49
## 39      1   weight    56
## 40      1   weight    67
## 41      1   weight    74
## 42      1   weight    87
## 43      1   weight   102
## 44      1   weight   108
## 45      1   weight   136
## 46      1   weight   154
## 47      1   weight   160
## 48      1   weight   157
## 49      1   weight    41
## 50      1   weight    42
## 51      1   weight    48
## 52      1   weight    60
## 53      1   weight    79
## 54      1   weight   106
## 55      1   weight   141
## 56      1   weight   164
## 57      1   weight   197
## 58      1   weight   199
## 59      1   weight   220
## 60      1   weight   223
## 61      1   weight    41
## 62      1   weight    49
## 63      1   weight    59
## 64      1   weight    74
## 65      1   weight    97
## 66      1   weight   124
## 67      1   weight   141
## 68      1   weight   148
## 69      1   weight   155
## 70      1   weight   160
## 71      1   weight   160
## 72      1   weight   157
## 73      1   weight    41
## 74      1   weight    49
## 75      1   weight    57
## 76      1   weight    71
## 77      1   weight    89
## 78      1   weight   112
## 79      1   weight   146
## 80      1   weight   174
## 81      1   weight   218
## 82      1   weight   250
## 83      1   weight   288
## 84      1   weight   305
## 85      1   weight    42
## 86      1   weight    50
## 87      1   weight    61
## 88      1   weight    71
## 89      1   weight    84
## 90      1   weight    93
## 91      1   weight   110
## 92      1   weight   116
## 93      1   weight   126
## 94      1   weight   134
## 95      1   weight   125
## 96      1   weight    42
## 97      1   weight    51
## 98      1   weight    59
## 99      1   weight    68
## 100     1   weight    85
## 101     1   weight    96
## 102     1   weight    90
## 103     1   weight    92
## 104     1   weight    93
## 105     1   weight   100
## 106     1   weight   100
## 107     1   weight    98
## 108     1   weight    41
## 109     1   weight    44
## 110     1   weight    52
## 111     1   weight    63
## 112     1   weight    74
## 113     1   weight    81
## 114     1   weight    89
## 115     1   weight    96
## 116     1   weight   101
## 117     1   weight   112
## 118     1   weight   120
## 119     1   weight   124
## 120     1   weight    43
## 121     1   weight    51
## 122     1   weight    63
## 123     1   weight    84
## 124     1   weight   112
## 125     1   weight   139
## 126     1   weight   168
## 127     1   weight   177
## 128     1   weight   182
## 129     1   weight   184
## 130     1   weight   181
## 131     1   weight   175
## 132     1   weight    41
## 133     1   weight    49
## 134     1   weight    56
## 135     1   weight    62
## 136     1   weight    72
## 137     1   weight    88
## 138     1   weight   119
## 139     1   weight   135
## 140     1   weight   162
## 141     1   weight   185
## 142     1   weight   195
## 143     1   weight   205
## 144     1   weight    41
## 145     1   weight    48
## 146     1   weight    53
## 147     1   weight    60
## 148     1   weight    65
## 149     1   weight    67
## 150     1   weight    71
## 151     1   weight    70
## 152     1   weight    71
## 153     1   weight    81
## 154     1   weight    91
## 155     1   weight    96
## 156     1   weight    41
## 157     1   weight    49
## 158     1   weight    62
## 159     1   weight    79
## 160     1   weight   101
## 161     1   weight   128
## 162     1   weight   164
## 163     1   weight   192
## 164     1   weight   227
## 165     1   weight   248
## 166     1   weight   259
## 167     1   weight   266
## 168     1   weight    41
## 169     1   weight    49
## 170     1   weight    56
## 171     1   weight    64
## 172     1   weight    68
## 173     1   weight    68
## 174     1   weight    67
## 175     1   weight    68
## 176     1   weight    41
## 177     1   weight    45
## 178     1   weight    49
## 179     1   weight    51
## 180     1   weight    57
## 181     1   weight    51
## 182     1   weight    54
## 183     1   weight    42
## 184     1   weight    51
## 185     1   weight    61
## 186     1   weight    72
## 187     1   weight    83
## 188     1   weight    89
## 189     1   weight    98
## 190     1   weight   103
## 191     1   weight   113
## 192     1   weight   123
## 193     1   weight   133
## 194     1   weight   142
## 195     1   weight    39
## 196     1   weight    35
## 197     1   weight    43
## 198     1   weight    48
## 199     1   weight    55
## 200     1   weight    62
## 201     1   weight    65
## 202     1   weight    71
## 203     1   weight    82
## 204     1   weight    88
## 205     1   weight   106
## 206     1   weight   120
## 207     1   weight   144
## 208     1   weight   157
## 209     1   weight    41
## 210     1   weight    47
## 211     1   weight    54
## 212     1   weight    58
## 213     1   weight    65
## 214     1   weight    73
## 215     1   weight    77
## 216     1   weight    89
## 217     1   weight    98
## 218     1   weight   107
## 219     1   weight   115
## 220     1   weight   117
## 221     2   weight    40
## 222     2   weight    50
## 223     2   weight    62
## 224     2   weight    86
## 225     2   weight   125
## 226     2   weight   163
## 227     2   weight   217
## 228     2   weight   240
## 229     2   weight   275
## 230     2   weight   307
## 231     2   weight   318
## 232     2   weight   331
## 233     2   weight    41
## 234     2   weight    55
## 235     2   weight    64
## 236     2   weight    77
## 237     2   weight    90
## 238     2   weight    95
## 239     2   weight   108
## 240     2   weight   111
## 241     2   weight   131
## 242     2   weight   148
## 243     2   weight   164
## 244     2   weight   167
## 245     2   weight    43
## 246     2   weight    52
## 247     2   weight    61
## 248     2   weight    73
## 249     2   weight    90
## 250     2   weight   103
## 251     2   weight   127
## 252     2   weight   135
## 253     2   weight   145
## 254     2   weight   163
## 255     2   weight   170
## 256     2   weight   175
## 257     2   weight    42
## 258     2   weight    52
## 259     2   weight    58
## 260     2   weight    74
## 261     2   weight    66
## 262     2   weight    68
## 263     2   weight    70
## 264     2   weight    71
## 265     2   weight    72
## 266     2   weight    72
## 267     2   weight    76
## 268     2   weight    74
## 269     2   weight    40
## 270     2   weight    49
## 271     2   weight    62
## 272     2   weight    78
## 273     2   weight   102
## 274     2   weight   124
## 275     2   weight   146
## 276     2   weight   164
## 277     2   weight   197
## 278     2   weight   231
## 279     2   weight   259
## 280     2   weight   265
## 281     2   weight    42
## 282     2   weight    48
## 283     2   weight    57
## 284     2   weight    74
## 285     2   weight    93
## 286     2   weight   114
## 287     2   weight   136
## 288     2   weight   147
## 289     2   weight   169
## 290     2   weight   205
## 291     2   weight   236
## 292     2   weight   251
## 293     2   weight    39
## 294     2   weight    46
## 295     2   weight    58
## 296     2   weight    73
## 297     2   weight    87
## 298     2   weight   100
## 299     2   weight   115
## 300     2   weight   123
## 301     2   weight   144
## 302     2   weight   163
## 303     2   weight   185
## 304     2   weight   192
## 305     2   weight    39
## 306     2   weight    46
## 307     2   weight    58
## 308     2   weight    73
## 309     2   weight    92
## 310     2   weight   114
## 311     2   weight   145
## 312     2   weight   156
## 313     2   weight   184
## 314     2   weight   207
## 315     2   weight   212
## 316     2   weight   233
## 317     2   weight    39
## 318     2   weight    48
## 319     2   weight    59
## 320     2   weight    74
## 321     2   weight    87
## 322     2   weight   106
## 323     2   weight   134
## 324     2   weight   150
## 325     2   weight   187
## 326     2   weight   230
## 327     2   weight   279
## 328     2   weight   309
## 329     2   weight    42
## 330     2   weight    48
## 331     2   weight    59
## 332     2   weight    72
## 333     2   weight    85
## 334     2   weight    98
## 335     2   weight   115
## 336     2   weight   122
## 337     2   weight   143
## 338     2   weight   151
## 339     2   weight   157
## 340     2   weight   150
## 341     3   weight    42
## 342     3   weight    53
## 343     3   weight    62
## 344     3   weight    73
## 345     3   weight    85
## 346     3   weight   102
## 347     3   weight   123
## 348     3   weight   138
## 349     3   weight   170
## 350     3   weight   204
## 351     3   weight   235
## 352     3   weight   256
## 353     3   weight    41
## 354     3   weight    49
## 355     3   weight    65
## 356     3   weight    82
## 357     3   weight   107
## 358     3   weight   129
## 359     3   weight   159
## 360     3   weight   179
## 361     3   weight   221
## 362     3   weight   263
## 363     3   weight   291
## 364     3   weight   305
## 365     3   weight    39
## 366     3   weight    50
## 367     3   weight    63
## 368     3   weight    77
## 369     3   weight    96
## 370     3   weight   111
## 371     3   weight   137
## 372     3   weight   144
## 373     3   weight   151
## 374     3   weight   146
## 375     3   weight   156
## 376     3   weight   147
## 377     3   weight    41
## 378     3   weight    49
## 379     3   weight    63
## 380     3   weight    85
## 381     3   weight   107
## 382     3   weight   134
## 383     3   weight   164
## 384     3   weight   186
## 385     3   weight   235
## 386     3   weight   294
## 387     3   weight   327
## 388     3   weight   341
## 389     3   weight    41
## 390     3   weight    53
## 391     3   weight    64
## 392     3   weight    87
## 393     3   weight   123
## 394     3   weight   158
## 395     3   weight   201
## 396     3   weight   238
## 397     3   weight   287
## 398     3   weight   332
## 399     3   weight   361
## 400     3   weight   373
## 401     3   weight    39
## 402     3   weight    48
## 403     3   weight    61
## 404     3   weight    76
## 405     3   weight    98
## 406     3   weight   116
## 407     3   weight   145
## 408     3   weight   166
## 409     3   weight   198
## 410     3   weight   227
## 411     3   weight   225
## 412     3   weight   220
## 413     3   weight    41
## 414     3   weight    48
## 415     3   weight    56
## 416     3   weight    68
## 417     3   weight    80
## 418     3   weight    83
## 419     3   weight   103
## 420     3   weight   112
## 421     3   weight   135
## 422     3   weight   157
## 423     3   weight   169
## 424     3   weight   178
## 425     3   weight    41
## 426     3   weight    49
## 427     3   weight    61
## 428     3   weight    74
## 429     3   weight    98
## 430     3   weight   109
## 431     3   weight   128
## 432     3   weight   154
## 433     3   weight   192
## 434     3   weight   232
## 435     3   weight   280
## 436     3   weight   290
## 437     3   weight    42
## 438     3   weight    50
## 439     3   weight    61
## 440     3   weight    78
## 441     3   weight    89
## 442     3   weight   109
## 443     3   weight   130
## 444     3   weight   146
## 445     3   weight   170
## 446     3   weight   214
## 447     3   weight   250
## 448     3   weight   272
## 449     3   weight    41
## 450     3   weight    55
## 451     3   weight    66
## 452     3   weight    79
## 453     3   weight   101
## 454     3   weight   120
## 455     3   weight   154
## 456     3   weight   182
## 457     3   weight   215
## 458     3   weight   262
## 459     3   weight   295
## 460     3   weight   321
## 461     4   weight    42
## 462     4   weight    51
## 463     4   weight    66
## 464     4   weight    85
## 465     4   weight   103
## 466     4   weight   124
## 467     4   weight   155
## 468     4   weight   153
## 469     4   weight   175
## 470     4   weight   184
## 471     4   weight   199
## 472     4   weight   204
## 473     4   weight    42
## 474     4   weight    49
## 475     4   weight    63
## 476     4   weight    84
## 477     4   weight   103
## 478     4   weight   126
## 479     4   weight   160
## 480     4   weight   174
## 481     4   weight   204
## 482     4   weight   234
## 483     4   weight   269
## 484     4   weight   281
## 485     4   weight    42
## 486     4   weight    55
## 487     4   weight    69
## 488     4   weight    96
## 489     4   weight   131
## 490     4   weight   157
## 491     4   weight   184
## 492     4   weight   188
## 493     4   weight   197
## 494     4   weight   198
## 495     4   weight   199
## 496     4   weight   200
## 497     4   weight    42
## 498     4   weight    51
## 499     4   weight    65
## 500     4   weight    86
## 501     4   weight   103
## 502     4   weight   118
## 503     4   weight   127
## 504     4   weight   138
## 505     4   weight   145
## 506     4   weight   146
## 507     4   weight    41
## 508     4   weight    50
## 509     4   weight    61
## 510     4   weight    78
## 511     4   weight    98
## 512     4   weight   117
## 513     4   weight   135
## 514     4   weight   141
## 515     4   weight   147
## 516     4   weight   174
## 517     4   weight   197
## 518     4   weight   196
## 519     4   weight    40
## 520     4   weight    52
## 521     4   weight    62
## 522     4   weight    82
## 523     4   weight   101
## 524     4   weight   120
## 525     4   weight   144
## 526     4   weight   156
## 527     4   weight   173
## 528     4   weight   210
## 529     4   weight   231
## 530     4   weight   238
## 531     4   weight    41
## 532     4   weight    53
## 533     4   weight    66
## 534     4   weight    79
## 535     4   weight   100
## 536     4   weight   123
## 537     4   weight   148
## 538     4   weight   157
## 539     4   weight   168
## 540     4   weight   185
## 541     4   weight   210
## 542     4   weight   205
## 543     4   weight    39
## 544     4   weight    50
## 545     4   weight    62
## 546     4   weight    80
## 547     4   weight   104
## 548     4   weight   125
## 549     4   weight   154
## 550     4   weight   170
## 551     4   weight   222
## 552     4   weight   261
## 553     4   weight   303
## 554     4   weight   322
## 555     4   weight    40
## 556     4   weight    53
## 557     4   weight    64
## 558     4   weight    85
## 559     4   weight   108
## 560     4   weight   128
## 561     4   weight   152
## 562     4   weight   166
## 563     4   weight   184
## 564     4   weight   203
## 565     4   weight   233
## 566     4   weight   237
## 567     4   weight    41
## 568     4   weight    54
## 569     4   weight    67
## 570     4   weight    84
## 571     4   weight   105
## 572     4   weight   122
## 573     4   weight   155
## 574     4   weight   175
## 575     4   weight   205
## 576     4   weight   234
## 577     4   weight   264
## 578     4   weight   264
## 579     1     Time     0
## 580     1     Time     2
## 581     1     Time     4
## 582     1     Time     6
## 583     1     Time     8
## 584     1     Time    10
## 585     1     Time    12
## 586     1     Time    14
## 587     1     Time    16
## 588     1     Time    18
## 589     1     Time    20
## 590     1     Time    21
## 591     1     Time     0
## 592     1     Time     2
## 593     1     Time     4
## 594     1     Time     6
## 595     1     Time     8
## 596     1     Time    10
## 597     1     Time    12
## 598     1     Time    14
## 599     1     Time    16
## 600     1     Time    18
## 601     1     Time    20
## 602     1     Time    21
## 603     1     Time     0
## 604     1     Time     2
## 605     1     Time     4
## 606     1     Time     6
## 607     1     Time     8
## 608     1     Time    10
## 609     1     Time    12
## 610     1     Time    14
## 611     1     Time    16
## 612     1     Time    18
## 613     1     Time    20
## 614     1     Time    21
## 615     1     Time     0
## 616     1     Time     2
## 617     1     Time     4
## 618     1     Time     6
## 619     1     Time     8
## 620     1     Time    10
## 621     1     Time    12
## 622     1     Time    14
## 623     1     Time    16
## 624     1     Time    18
## 625     1     Time    20
## 626     1     Time    21
## 627     1     Time     0
## 628     1     Time     2
## 629     1     Time     4
## 630     1     Time     6
## 631     1     Time     8
## 632     1     Time    10
## 633     1     Time    12
## 634     1     Time    14
## 635     1     Time    16
## 636     1     Time    18
## 637     1     Time    20
## 638     1     Time    21
## 639     1     Time     0
## 640     1     Time     2
## 641     1     Time     4
## 642     1     Time     6
## 643     1     Time     8
## 644     1     Time    10
## 645     1     Time    12
## 646     1     Time    14
## 647     1     Time    16
## 648     1     Time    18
## 649     1     Time    20
## 650     1     Time    21
## 651     1     Time     0
## 652     1     Time     2
## 653     1     Time     4
## 654     1     Time     6
## 655     1     Time     8
## 656     1     Time    10
## 657     1     Time    12
## 658     1     Time    14
## 659     1     Time    16
## 660     1     Time    18
## 661     1     Time    20
## 662     1     Time    21
## 663     1     Time     0
## 664     1     Time     2
## 665     1     Time     4
## 666     1     Time     6
## 667     1     Time     8
## 668     1     Time    10
## 669     1     Time    12
## 670     1     Time    14
## 671     1     Time    16
## 672     1     Time    18
## 673     1     Time    20
## 674     1     Time     0
## 675     1     Time     2
## 676     1     Time     4
## 677     1     Time     6
## 678     1     Time     8
## 679     1     Time    10
## 680     1     Time    12
## 681     1     Time    14
## 682     1     Time    16
## 683     1     Time    18
## 684     1     Time    20
## 685     1     Time    21
## 686     1     Time     0
## 687     1     Time     2
## 688     1     Time     4
## 689     1     Time     6
## 690     1     Time     8
## 691     1     Time    10
## 692     1     Time    12
## 693     1     Time    14
## 694     1     Time    16
## 695     1     Time    18
## 696     1     Time    20
## 697     1     Time    21
## 698     1     Time     0
## 699     1     Time     2
## 700     1     Time     4
## 701     1     Time     6
## 702     1     Time     8
## 703     1     Time    10
## 704     1     Time    12
## 705     1     Time    14
## 706     1     Time    16
## 707     1     Time    18
## 708     1     Time    20
## 709     1     Time    21
## 710     1     Time     0
## 711     1     Time     2
## 712     1     Time     4
## 713     1     Time     6
## 714     1     Time     8
## 715     1     Time    10
## 716     1     Time    12
## 717     1     Time    14
## 718     1     Time    16
## 719     1     Time    18
## 720     1     Time    20
## 721     1     Time    21
## 722     1     Time     0
## 723     1     Time     2
## 724     1     Time     4
## 725     1     Time     6
## 726     1     Time     8
## 727     1     Time    10
## 728     1     Time    12
## 729     1     Time    14
## 730     1     Time    16
## 731     1     Time    18
## 732     1     Time    20
## 733     1     Time    21
## 734     1     Time     0
## 735     1     Time     2
## 736     1     Time     4
## 737     1     Time     6
## 738     1     Time     8
## 739     1     Time    10
## 740     1     Time    12
## 741     1     Time    14
## 742     1     Time    16
## 743     1     Time    18
## 744     1     Time    20
## 745     1     Time    21
## 746     1     Time     0
## 747     1     Time     2
## 748     1     Time     4
## 749     1     Time     6
## 750     1     Time     8
## 751     1     Time    10
## 752     1     Time    12
## 753     1     Time    14
## 754     1     Time     0
## 755     1     Time     2
## 756     1     Time     4
## 757     1     Time     6
## 758     1     Time     8
## 759     1     Time    10
## 760     1     Time    12
## 761     1     Time     0
## 762     1     Time     2
## 763     1     Time     4
## 764     1     Time     6
## 765     1     Time     8
## 766     1     Time    10
## 767     1     Time    12
## 768     1     Time    14
## 769     1     Time    16
## 770     1     Time    18
## 771     1     Time    20
## 772     1     Time    21
## 773     1     Time     0
## 774     1     Time     2
## 775     1     Time     0
## 776     1     Time     2
## 777     1     Time     4
## 778     1     Time     6
## 779     1     Time     8
## 780     1     Time    10
## 781     1     Time    12
## 782     1     Time    14
## 783     1     Time    16
## 784     1     Time    18
## 785     1     Time    20
## 786     1     Time    21
## 787     1     Time     0
## 788     1     Time     2
## 789     1     Time     4
## 790     1     Time     6
## 791     1     Time     8
## 792     1     Time    10
## 793     1     Time    12
## 794     1     Time    14
## 795     1     Time    16
## 796     1     Time    18
## 797     1     Time    20
## 798     1     Time    21
## 799     2     Time     0
## 800     2     Time     2
## 801     2     Time     4
## 802     2     Time     6
## 803     2     Time     8
## 804     2     Time    10
## 805     2     Time    12
## 806     2     Time    14
## 807     2     Time    16
## 808     2     Time    18
## 809     2     Time    20
## 810     2     Time    21
## 811     2     Time     0
## 812     2     Time     2
## 813     2     Time     4
## 814     2     Time     6
## 815     2     Time     8
## 816     2     Time    10
## 817     2     Time    12
## 818     2     Time    14
## 819     2     Time    16
## 820     2     Time    18
## 821     2     Time    20
## 822     2     Time    21
## 823     2     Time     0
## 824     2     Time     2
## 825     2     Time     4
## 826     2     Time     6
## 827     2     Time     8
## 828     2     Time    10
## 829     2     Time    12
## 830     2     Time    14
## 831     2     Time    16
## 832     2     Time    18
## 833     2     Time    20
## 834     2     Time    21
## 835     2     Time     0
## 836     2     Time     2
## 837     2     Time     4
## 838     2     Time     6
## 839     2     Time     8
## 840     2     Time    10
## 841     2     Time    12
## 842     2     Time    14
## 843     2     Time    16
## 844     2     Time    18
## 845     2     Time    20
## 846     2     Time    21
## 847     2     Time     0
## 848     2     Time     2
## 849     2     Time     4
## 850     2     Time     6
## 851     2     Time     8
## 852     2     Time    10
## 853     2     Time    12
## 854     2     Time    14
## 855     2     Time    16
## 856     2     Time    18
## 857     2     Time    20
## 858     2     Time    21
## 859     2     Time     0
## 860     2     Time     2
## 861     2     Time     4
## 862     2     Time     6
## 863     2     Time     8
## 864     2     Time    10
## 865     2     Time    12
## 866     2     Time    14
## 867     2     Time    16
## 868     2     Time    18
## 869     2     Time    20
## 870     2     Time    21
## 871     2     Time     0
## 872     2     Time     2
## 873     2     Time     4
## 874     2     Time     6
## 875     2     Time     8
## 876     2     Time    10
## 877     2     Time    12
## 878     2     Time    14
## 879     2     Time    16
## 880     2     Time    18
## 881     2     Time    20
## 882     2     Time    21
## 883     2     Time     0
## 884     2     Time     2
## 885     2     Time     4
## 886     2     Time     6
## 887     2     Time     8
## 888     2     Time    10
## 889     2     Time    12
## 890     2     Time    14
## 891     2     Time    16
## 892     2     Time    18
## 893     2     Time    20
## 894     2     Time    21
## 895     2     Time     0
## 896     2     Time     2
## 897     2     Time     4
## 898     2     Time     6
## 899     2     Time     8
## 900     2     Time    10
## 901     2     Time    12
## 902     2     Time    14
## 903     2     Time    16
## 904     2     Time    18
## 905     2     Time    20
## 906     2     Time    21
## 907     2     Time     0
## 908     2     Time     2
## 909     2     Time     4
## 910     2     Time     6
## 911     2     Time     8
## 912     2     Time    10
## 913     2     Time    12
## 914     2     Time    14
## 915     2     Time    16
## 916     2     Time    18
## 917     2     Time    20
## 918     2     Time    21
## 919     3     Time     0
## 920     3     Time     2
## 921     3     Time     4
## 922     3     Time     6
## 923     3     Time     8
## 924     3     Time    10
## 925     3     Time    12
## 926     3     Time    14
## 927     3     Time    16
## 928     3     Time    18
## 929     3     Time    20
## 930     3     Time    21
## 931     3     Time     0
## 932     3     Time     2
## 933     3     Time     4
## 934     3     Time     6
## 935     3     Time     8
## 936     3     Time    10
## 937     3     Time    12
## 938     3     Time    14
## 939     3     Time    16
## 940     3     Time    18
## 941     3     Time    20
## 942     3     Time    21
## 943     3     Time     0
## 944     3     Time     2
## 945     3     Time     4
## 946     3     Time     6
## 947     3     Time     8
## 948     3     Time    10
## 949     3     Time    12
## 950     3     Time    14
## 951     3     Time    16
## 952     3     Time    18
## 953     3     Time    20
## 954     3     Time    21
## 955     3     Time     0
## 956     3     Time     2
## 957     3     Time     4
## 958     3     Time     6
## 959     3     Time     8
## 960     3     Time    10
## 961     3     Time    12
## 962     3     Time    14
## 963     3     Time    16
## 964     3     Time    18
## 965     3     Time    20
## 966     3     Time    21
## 967     3     Time     0
## 968     3     Time     2
## 969     3     Time     4
## 970     3     Time     6
## 971     3     Time     8
## 972     3     Time    10
## 973     3     Time    12
## 974     3     Time    14
## 975     3     Time    16
## 976     3     Time    18
## 977     3     Time    20
## 978     3     Time    21
## 979     3     Time     0
## 980     3     Time     2
## 981     3     Time     4
## 982     3     Time     6
## 983     3     Time     8
## 984     3     Time    10
## 985     3     Time    12
## 986     3     Time    14
## 987     3     Time    16
## 988     3     Time    18
## 989     3     Time    20
## 990     3     Time    21
## 991     3     Time     0
## 992     3     Time     2
## 993     3     Time     4
## 994     3     Time     6
## 995     3     Time     8
## 996     3     Time    10
## 997     3     Time    12
## 998     3     Time    14
## 999     3     Time    16
## 1000    3     Time    18
## 1001    3     Time    20
## 1002    3     Time    21
## 1003    3     Time     0
## 1004    3     Time     2
## 1005    3     Time     4
## 1006    3     Time     6
## 1007    3     Time     8
## 1008    3     Time    10
## 1009    3     Time    12
## 1010    3     Time    14
## 1011    3     Time    16
## 1012    3     Time    18
## 1013    3     Time    20
## 1014    3     Time    21
## 1015    3     Time     0
## 1016    3     Time     2
## 1017    3     Time     4
## 1018    3     Time     6
## 1019    3     Time     8
## 1020    3     Time    10
## 1021    3     Time    12
## 1022    3     Time    14
## 1023    3     Time    16
## 1024    3     Time    18
## 1025    3     Time    20
## 1026    3     Time    21
## 1027    3     Time     0
## 1028    3     Time     2
## 1029    3     Time     4
## 1030    3     Time     6
## 1031    3     Time     8
## 1032    3     Time    10
## 1033    3     Time    12
## 1034    3     Time    14
## 1035    3     Time    16
## 1036    3     Time    18
## 1037    3     Time    20
## 1038    3     Time    21
## 1039    4     Time     0
## 1040    4     Time     2
## 1041    4     Time     4
## 1042    4     Time     6
## 1043    4     Time     8
## 1044    4     Time    10
## 1045    4     Time    12
## 1046    4     Time    14
## 1047    4     Time    16
## 1048    4     Time    18
## 1049    4     Time    20
## 1050    4     Time    21
## 1051    4     Time     0
## 1052    4     Time     2
## 1053    4     Time     4
## 1054    4     Time     6
## 1055    4     Time     8
## 1056    4     Time    10
## 1057    4     Time    12
## 1058    4     Time    14
## 1059    4     Time    16
## 1060    4     Time    18
## 1061    4     Time    20
## 1062    4     Time    21
## 1063    4     Time     0
## 1064    4     Time     2
## 1065    4     Time     4
## 1066    4     Time     6
## 1067    4     Time     8
## 1068    4     Time    10
## 1069    4     Time    12
## 1070    4     Time    14
## 1071    4     Time    16
## 1072    4     Time    18
## 1073    4     Time    20
## 1074    4     Time    21
## 1075    4     Time     0
## 1076    4     Time     2
## 1077    4     Time     4
## 1078    4     Time     6
## 1079    4     Time     8
## 1080    4     Time    10
## 1081    4     Time    12
## 1082    4     Time    14
## 1083    4     Time    16
## 1084    4     Time    18
## 1085    4     Time     0
## 1086    4     Time     2
## 1087    4     Time     4
## 1088    4     Time     6
## 1089    4     Time     8
## 1090    4     Time    10
## 1091    4     Time    12
## 1092    4     Time    14
## 1093    4     Time    16
## 1094    4     Time    18
## 1095    4     Time    20
## 1096    4     Time    21
## 1097    4     Time     0
## 1098    4     Time     2
## 1099    4     Time     4
## 1100    4     Time     6
## 1101    4     Time     8
## 1102    4     Time    10
## 1103    4     Time    12
## 1104    4     Time    14
## 1105    4     Time    16
## 1106    4     Time    18
## 1107    4     Time    20
## 1108    4     Time    21
## 1109    4     Time     0
## 1110    4     Time     2
## 1111    4     Time     4
## 1112    4     Time     6
## 1113    4     Time     8
## 1114    4     Time    10
## 1115    4     Time    12
## 1116    4     Time    14
## 1117    4     Time    16
## 1118    4     Time    18
## 1119    4     Time    20
## 1120    4     Time    21
## 1121    4     Time     0
## 1122    4     Time     2
## 1123    4     Time     4
## 1124    4     Time     6
## 1125    4     Time     8
## 1126    4     Time    10
## 1127    4     Time    12
## 1128    4     Time    14
## 1129    4     Time    16
## 1130    4     Time    18
## 1131    4     Time    20
## 1132    4     Time    21
## 1133    4     Time     0
## 1134    4     Time     2
## 1135    4     Time     4
## 1136    4     Time     6
## 1137    4     Time     8
## 1138    4     Time    10
## 1139    4     Time    12
## 1140    4     Time    14
## 1141    4     Time    16
## 1142    4     Time    18
## 1143    4     Time    20
## 1144    4     Time    21
## 1145    4     Time     0
## 1146    4     Time     2
## 1147    4     Time     4
## 1148    4     Time     6
## 1149    4     Time     8
## 1150    4     Time    10
## 1151    4     Time    12
## 1152    4     Time    14
## 1153    4     Time    16
## 1154    4     Time    18
## 1155    4     Time    20
## 1156    4     Time    21
## 1157    1    Chick     1
## 1158    1    Chick     1
## 1159    1    Chick     1
## 1160    1    Chick     1
## 1161    1    Chick     1
## 1162    1    Chick     1
## 1163    1    Chick     1
## 1164    1    Chick     1
## 1165    1    Chick     1
## 1166    1    Chick     1
## 1167    1    Chick     1
## 1168    1    Chick     1
## 1169    1    Chick     2
## 1170    1    Chick     2
## 1171    1    Chick     2
## 1172    1    Chick     2
## 1173    1    Chick     2
## 1174    1    Chick     2
## 1175    1    Chick     2
## 1176    1    Chick     2
## 1177    1    Chick     2
## 1178    1    Chick     2
## 1179    1    Chick     2
## 1180    1    Chick     2
## 1181    1    Chick     3
## 1182    1    Chick     3
## 1183    1    Chick     3
## 1184    1    Chick     3
## 1185    1    Chick     3
## 1186    1    Chick     3
## 1187    1    Chick     3
## 1188    1    Chick     3
## 1189    1    Chick     3
## 1190    1    Chick     3
## 1191    1    Chick     3
## 1192    1    Chick     3
## 1193    1    Chick     4
## 1194    1    Chick     4
## 1195    1    Chick     4
## 1196    1    Chick     4
## 1197    1    Chick     4
## 1198    1    Chick     4
## 1199    1    Chick     4
## 1200    1    Chick     4
## 1201    1    Chick     4
## 1202    1    Chick     4
## 1203    1    Chick     4
## 1204    1    Chick     4
## 1205    1    Chick     5
## 1206    1    Chick     5
## 1207    1    Chick     5
## 1208    1    Chick     5
## 1209    1    Chick     5
## 1210    1    Chick     5
## 1211    1    Chick     5
## 1212    1    Chick     5
## 1213    1    Chick     5
## 1214    1    Chick     5
## 1215    1    Chick     5
## 1216    1    Chick     5
## 1217    1    Chick     6
## 1218    1    Chick     6
## 1219    1    Chick     6
## 1220    1    Chick     6
## 1221    1    Chick     6
## 1222    1    Chick     6
## 1223    1    Chick     6
## 1224    1    Chick     6
## 1225    1    Chick     6
## 1226    1    Chick     6
## 1227    1    Chick     6
## 1228    1    Chick     6
## 1229    1    Chick     7
## 1230    1    Chick     7
## 1231    1    Chick     7
## 1232    1    Chick     7
## 1233    1    Chick     7
## 1234    1    Chick     7
## 1235    1    Chick     7
## 1236    1    Chick     7
## 1237    1    Chick     7
## 1238    1    Chick     7
## 1239    1    Chick     7
## 1240    1    Chick     7
## 1241    1    Chick     8
## 1242    1    Chick     8
## 1243    1    Chick     8
## 1244    1    Chick     8
## 1245    1    Chick     8
## 1246    1    Chick     8
## 1247    1    Chick     8
## 1248    1    Chick     8
## 1249    1    Chick     8
## 1250    1    Chick     8
## 1251    1    Chick     8
## 1252    1    Chick     9
## 1253    1    Chick     9
## 1254    1    Chick     9
## 1255    1    Chick     9
## 1256    1    Chick     9
## 1257    1    Chick     9
## 1258    1    Chick     9
## 1259    1    Chick     9
## 1260    1    Chick     9
## 1261    1    Chick     9
## 1262    1    Chick     9
## 1263    1    Chick     9
## 1264    1    Chick    10
## 1265    1    Chick    10
## 1266    1    Chick    10
## 1267    1    Chick    10
## 1268    1    Chick    10
## 1269    1    Chick    10
## 1270    1    Chick    10
## 1271    1    Chick    10
## 1272    1    Chick    10
## 1273    1    Chick    10
## 1274    1    Chick    10
## 1275    1    Chick    10
## 1276    1    Chick    11
## 1277    1    Chick    11
## 1278    1    Chick    11
## 1279    1    Chick    11
## 1280    1    Chick    11
## 1281    1    Chick    11
## 1282    1    Chick    11
## 1283    1    Chick    11
## 1284    1    Chick    11
## 1285    1    Chick    11
## 1286    1    Chick    11
## 1287    1    Chick    11
## 1288    1    Chick    12
## 1289    1    Chick    12
## 1290    1    Chick    12
## 1291    1    Chick    12
## 1292    1    Chick    12
## 1293    1    Chick    12
## 1294    1    Chick    12
## 1295    1    Chick    12
## 1296    1    Chick    12
## 1297    1    Chick    12
## 1298    1    Chick    12
## 1299    1    Chick    12
## 1300    1    Chick    13
## 1301    1    Chick    13
## 1302    1    Chick    13
## 1303    1    Chick    13
## 1304    1    Chick    13
## 1305    1    Chick    13
## 1306    1    Chick    13
## 1307    1    Chick    13
## 1308    1    Chick    13
## 1309    1    Chick    13
## 1310    1    Chick    13
## 1311    1    Chick    13
## 1312    1    Chick    14
## 1313    1    Chick    14
## 1314    1    Chick    14
## 1315    1    Chick    14
## 1316    1    Chick    14
## 1317    1    Chick    14
## 1318    1    Chick    14
## 1319    1    Chick    14
## 1320    1    Chick    14
## 1321    1    Chick    14
## 1322    1    Chick    14
## 1323    1    Chick    14
## 1324    1    Chick    15
## 1325    1    Chick    15
## 1326    1    Chick    15
## 1327    1    Chick    15
## 1328    1    Chick    15
## 1329    1    Chick    15
## 1330    1    Chick    15
## 1331    1    Chick    15
## 1332    1    Chick    16
## 1333    1    Chick    16
## 1334    1    Chick    16
## 1335    1    Chick    16
## 1336    1    Chick    16
## 1337    1    Chick    16
## 1338    1    Chick    16
## 1339    1    Chick    17
## 1340    1    Chick    17
## 1341    1    Chick    17
## 1342    1    Chick    17
## 1343    1    Chick    17
## 1344    1    Chick    17
## 1345    1    Chick    17
## 1346    1    Chick    17
## 1347    1    Chick    17
## 1348    1    Chick    17
## 1349    1    Chick    17
## 1350    1    Chick    17
## 1351    1    Chick    18
## 1352    1    Chick    18
## 1353    1    Chick    19
## 1354    1    Chick    19
## 1355    1    Chick    19
## 1356    1    Chick    19
## 1357    1    Chick    19
## 1358    1    Chick    19
## 1359    1    Chick    19
## 1360    1    Chick    19
## 1361    1    Chick    19
## 1362    1    Chick    19
## 1363    1    Chick    19
## 1364    1    Chick    19
## 1365    1    Chick    20
## 1366    1    Chick    20
## 1367    1    Chick    20
## 1368    1    Chick    20
## 1369    1    Chick    20
## 1370    1    Chick    20
## 1371    1    Chick    20
## 1372    1    Chick    20
## 1373    1    Chick    20
## 1374    1    Chick    20
## 1375    1    Chick    20
## 1376    1    Chick    20
## 1377    2    Chick    21
## 1378    2    Chick    21
## 1379    2    Chick    21
## 1380    2    Chick    21
## 1381    2    Chick    21
## 1382    2    Chick    21
## 1383    2    Chick    21
## 1384    2    Chick    21
## 1385    2    Chick    21
## 1386    2    Chick    21
## 1387    2    Chick    21
## 1388    2    Chick    21
## 1389    2    Chick    22
## 1390    2    Chick    22
## 1391    2    Chick    22
## 1392    2    Chick    22
## 1393    2    Chick    22
## 1394    2    Chick    22
## 1395    2    Chick    22
## 1396    2    Chick    22
## 1397    2    Chick    22
## 1398    2    Chick    22
## 1399    2    Chick    22
## 1400    2    Chick    22
## 1401    2    Chick    23
## 1402    2    Chick    23
## 1403    2    Chick    23
## 1404    2    Chick    23
## 1405    2    Chick    23
## 1406    2    Chick    23
## 1407    2    Chick    23
## 1408    2    Chick    23
## 1409    2    Chick    23
## 1410    2    Chick    23
## 1411    2    Chick    23
## 1412    2    Chick    23
## 1413    2    Chick    24
## 1414    2    Chick    24
## 1415    2    Chick    24
## 1416    2    Chick    24
## 1417    2    Chick    24
## 1418    2    Chick    24
## 1419    2    Chick    24
## 1420    2    Chick    24
## 1421    2    Chick    24
## 1422    2    Chick    24
## 1423    2    Chick    24
## 1424    2    Chick    24
## 1425    2    Chick    25
## 1426    2    Chick    25
## 1427    2    Chick    25
## 1428    2    Chick    25
## 1429    2    Chick    25
## 1430    2    Chick    25
## 1431    2    Chick    25
## 1432    2    Chick    25
## 1433    2    Chick    25
## 1434    2    Chick    25
## 1435    2    Chick    25
## 1436    2    Chick    25
## 1437    2    Chick    26
## 1438    2    Chick    26
## 1439    2    Chick    26
## 1440    2    Chick    26
## 1441    2    Chick    26
## 1442    2    Chick    26
## 1443    2    Chick    26
## 1444    2    Chick    26
## 1445    2    Chick    26
## 1446    2    Chick    26
## 1447    2    Chick    26
## 1448    2    Chick    26
## 1449    2    Chick    27
## 1450    2    Chick    27
## 1451    2    Chick    27
## 1452    2    Chick    27
## 1453    2    Chick    27
## 1454    2    Chick    27
## 1455    2    Chick    27
## 1456    2    Chick    27
## 1457    2    Chick    27
## 1458    2    Chick    27
## 1459    2    Chick    27
## 1460    2    Chick    27
## 1461    2    Chick    28
## 1462    2    Chick    28
## 1463    2    Chick    28
## 1464    2    Chick    28
## 1465    2    Chick    28
## 1466    2    Chick    28
## 1467    2    Chick    28
## 1468    2    Chick    28
## 1469    2    Chick    28
## 1470    2    Chick    28
## 1471    2    Chick    28
## 1472    2    Chick    28
## 1473    2    Chick    29
## 1474    2    Chick    29
## 1475    2    Chick    29
## 1476    2    Chick    29
## 1477    2    Chick    29
## 1478    2    Chick    29
## 1479    2    Chick    29
## 1480    2    Chick    29
## 1481    2    Chick    29
## 1482    2    Chick    29
## 1483    2    Chick    29
## 1484    2    Chick    29
## 1485    2    Chick    30
## 1486    2    Chick    30
## 1487    2    Chick    30
## 1488    2    Chick    30
## 1489    2    Chick    30
## 1490    2    Chick    30
## 1491    2    Chick    30
## 1492    2    Chick    30
## 1493    2    Chick    30
## 1494    2    Chick    30
## 1495    2    Chick    30
## 1496    2    Chick    30
## 1497    3    Chick    31
## 1498    3    Chick    31
## 1499    3    Chick    31
## 1500    3    Chick    31
## 1501    3    Chick    31
## 1502    3    Chick    31
## 1503    3    Chick    31
## 1504    3    Chick    31
## 1505    3    Chick    31
## 1506    3    Chick    31
## 1507    3    Chick    31
## 1508    3    Chick    31
## 1509    3    Chick    32
## 1510    3    Chick    32
## 1511    3    Chick    32
## 1512    3    Chick    32
## 1513    3    Chick    32
## 1514    3    Chick    32
## 1515    3    Chick    32
## 1516    3    Chick    32
## 1517    3    Chick    32
## 1518    3    Chick    32
## 1519    3    Chick    32
## 1520    3    Chick    32
## 1521    3    Chick    33
## 1522    3    Chick    33
## 1523    3    Chick    33
## 1524    3    Chick    33
## 1525    3    Chick    33
## 1526    3    Chick    33
## 1527    3    Chick    33
## 1528    3    Chick    33
## 1529    3    Chick    33
## 1530    3    Chick    33
## 1531    3    Chick    33
## 1532    3    Chick    33
## 1533    3    Chick    34
## 1534    3    Chick    34
## 1535    3    Chick    34
## 1536    3    Chick    34
## 1537    3    Chick    34
## 1538    3    Chick    34
## 1539    3    Chick    34
## 1540    3    Chick    34
## 1541    3    Chick    34
## 1542    3    Chick    34
## 1543    3    Chick    34
## 1544    3    Chick    34
## 1545    3    Chick    35
## 1546    3    Chick    35
## 1547    3    Chick    35
## 1548    3    Chick    35
## 1549    3    Chick    35
## 1550    3    Chick    35
## 1551    3    Chick    35
## 1552    3    Chick    35
## 1553    3    Chick    35
## 1554    3    Chick    35
## 1555    3    Chick    35
## 1556    3    Chick    35
## 1557    3    Chick    36
## 1558    3    Chick    36
## 1559    3    Chick    36
## 1560    3    Chick    36
## 1561    3    Chick    36
## 1562    3    Chick    36
## 1563    3    Chick    36
## 1564    3    Chick    36
## 1565    3    Chick    36
## 1566    3    Chick    36
## 1567    3    Chick    36
## 1568    3    Chick    36
## 1569    3    Chick    37
## 1570    3    Chick    37
## 1571    3    Chick    37
## 1572    3    Chick    37
## 1573    3    Chick    37
## 1574    3    Chick    37
## 1575    3    Chick    37
## 1576    3    Chick    37
## 1577    3    Chick    37
## 1578    3    Chick    37
## 1579    3    Chick    37
## 1580    3    Chick    37
## 1581    3    Chick    38
## 1582    3    Chick    38
## 1583    3    Chick    38
## 1584    3    Chick    38
## 1585    3    Chick    38
## 1586    3    Chick    38
## 1587    3    Chick    38
## 1588    3    Chick    38
## 1589    3    Chick    38
## 1590    3    Chick    38
## 1591    3    Chick    38
## 1592    3    Chick    38
## 1593    3    Chick    39
## 1594    3    Chick    39
## 1595    3    Chick    39
## 1596    3    Chick    39
## 1597    3    Chick    39
## 1598    3    Chick    39
## 1599    3    Chick    39
## 1600    3    Chick    39
## 1601    3    Chick    39
## 1602    3    Chick    39
## 1603    3    Chick    39
## 1604    3    Chick    39
## 1605    3    Chick    40
## 1606    3    Chick    40
## 1607    3    Chick    40
## 1608    3    Chick    40
## 1609    3    Chick    40
## 1610    3    Chick    40
## 1611    3    Chick    40
## 1612    3    Chick    40
## 1613    3    Chick    40
## 1614    3    Chick    40
## 1615    3    Chick    40
## 1616    3    Chick    40
## 1617    4    Chick    41
## 1618    4    Chick    41
## 1619    4    Chick    41
## 1620    4    Chick    41
## 1621    4    Chick    41
## 1622    4    Chick    41
## 1623    4    Chick    41
## 1624    4    Chick    41
## 1625    4    Chick    41
## 1626    4    Chick    41
## 1627    4    Chick    41
## 1628    4    Chick    41
## 1629    4    Chick    42
## 1630    4    Chick    42
## 1631    4    Chick    42
## 1632    4    Chick    42
## 1633    4    Chick    42
## 1634    4    Chick    42
## 1635    4    Chick    42
## 1636    4    Chick    42
## 1637    4    Chick    42
## 1638    4    Chick    42
## 1639    4    Chick    42
## 1640    4    Chick    42
## 1641    4    Chick    43
## 1642    4    Chick    43
## 1643    4    Chick    43
## 1644    4    Chick    43
## 1645    4    Chick    43
## 1646    4    Chick    43
## 1647    4    Chick    43
## 1648    4    Chick    43
## 1649    4    Chick    43
## 1650    4    Chick    43
## 1651    4    Chick    43
## 1652    4    Chick    43
## 1653    4    Chick    44
## 1654    4    Chick    44
## 1655    4    Chick    44
## 1656    4    Chick    44
## 1657    4    Chick    44
## 1658    4    Chick    44
## 1659    4    Chick    44
## 1660    4    Chick    44
## 1661    4    Chick    44
## 1662    4    Chick    44
## 1663    4    Chick    45
## 1664    4    Chick    45
## 1665    4    Chick    45
## 1666    4    Chick    45
## 1667    4    Chick    45
## 1668    4    Chick    45
## 1669    4    Chick    45
## 1670    4    Chick    45
## 1671    4    Chick    45
## 1672    4    Chick    45
## 1673    4    Chick    45
## 1674    4    Chick    45
## 1675    4    Chick    46
## 1676    4    Chick    46
## 1677    4    Chick    46
## 1678    4    Chick    46
## 1679    4    Chick    46
## 1680    4    Chick    46
## 1681    4    Chick    46
## 1682    4    Chick    46
## 1683    4    Chick    46
## 1684    4    Chick    46
## 1685    4    Chick    46
## 1686    4    Chick    46
## 1687    4    Chick    47
## 1688    4    Chick    47
## 1689    4    Chick    47
## 1690    4    Chick    47
## 1691    4    Chick    47
## 1692    4    Chick    47
## 1693    4    Chick    47
## 1694    4    Chick    47
## 1695    4    Chick    47
## 1696    4    Chick    47
## 1697    4    Chick    47
## 1698    4    Chick    47
## 1699    4    Chick    48
## 1700    4    Chick    48
## 1701    4    Chick    48
## 1702    4    Chick    48
## 1703    4    Chick    48
## 1704    4    Chick    48
## 1705    4    Chick    48
## 1706    4    Chick    48
## 1707    4    Chick    48
## 1708    4    Chick    48
## 1709    4    Chick    48
## 1710    4    Chick    48
## 1711    4    Chick    49
## 1712    4    Chick    49
## 1713    4    Chick    49
## 1714    4    Chick    49
## 1715    4    Chick    49
## 1716    4    Chick    49
## 1717    4    Chick    49
## 1718    4    Chick    49
## 1719    4    Chick    49
## 1720    4    Chick    49
## 1721    4    Chick    49
## 1722    4    Chick    49
## 1723    4    Chick    50
## 1724    4    Chick    50
## 1725    4    Chick    50
## 1726    4    Chick    50
## 1727    4    Chick    50
## 1728    4    Chick    50
## 1729    4    Chick    50
## 1730    4    Chick    50
## 1731    4    Chick    50
## 1732    4    Chick    50
## 1733    4    Chick    50
## 1734    4    Chick    50
#cast(data = x,Diet~variable,mean)
cast(x, Diet~variable, mean)
## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA

## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA

## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA

## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA

## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA

## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA

## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA

## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA

## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA

## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA

## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA

## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA
##   Diet weight Time Chick
## 1    1     NA   NA    NA
## 2    2     NA   NA    NA
## 3    3     NA   NA    NA
## 4    4     NA   NA    NA
alternative 1 (eassiest)
# Load the ChickWeight dataset
data(ChickWeight)

# Load the reshape2 package
library(reshape2)
## Warning: package 'reshape2' was built under R version 4.2.2
## 
## Attaching package: 'reshape2'
## The following object is masked from 'package:tidyr':
## 
##     smiths
## The following objects are masked from 'package:reshape':
## 
##     colsplit, melt, recast
# Cast the data to calculate the mean weight for each diet type
avg_weight <- cast(ChickWeight, Diet ~ ., mean, value = "weight")

# Print the average weight for each diet type
print(avg_weight)
##   Diet (all)
## 1    1   103
## 2    2   123
## 3    3   143
## 4    4   135
alternative 2
# Load the ChickWeight dataset
data(ChickWeight)

# Group the data by diet type and calculate the mean weight for each group
avg_weight <- aggregate(weight ~ Diet, data = ChickWeight, mean)

# Print the average weight for each diet type
print(avg_weight)
##   Diet weight
## 1    1    103
## 2    2    123
## 3    3    143
## 4    4    135
3b
cast(data = ChickWeight,Time~.,value = "weight")
## Aggregation requires fun.aggregate: length used as default
##    Time (all)
## 1     0    50
## 2     2    50
## 3     4    49
## 4     6    49
## 5     8    49
## 6    10    49
## 7    12    49
## 8    14    48
## 9    16    47
## 10   18    47
## 11   20    46
## 12   21    45
3c
boxplot(weight~ Diet,ChickWeight)

Q4

cdf<-function(x){
  m<-function(x){exp(-x)}
  return(integrate(f = m,lower = 0,upper = x))
  #return(exp(-x))
}
cdf(6)
## 1 with absolute error < 1.1e-14

Final Exam

2019

1

1(a) Minimum

rm(list=ls())
dataState = as.data.frame(state.x77)
index = apply(dataState, 2, which.min)
index
## Population     Income Illiteracy   Life Exp     Murder    HS Grad      Frost 
##          2         24         15         40         34         40         11 
##       Area 
##         39
minimum = row.names(dataState)[index]
minimum
## [1] "Alaska"         "Mississippi"    "Iowa"           "South Carolina"
## [5] "North Dakota"   "South Carolina" "Hawaii"         "Rhode Island"

1(b) Maximum

x<-apply(X = state.x77,MARGIN = 2,FUN = which.max)
x
## Population     Income Illiteracy   Life Exp     Murder    HS Grad      Frost 
##          5          2         18         11          1         44         28 
##       Area 
##          2
row.names(state.x77)[x]
## [1] "California" "Alaska"     "Louisiana"  "Hawaii"     "Alabama"   
## [6] "Utah"       "Nevada"     "Alaska"

1c(easy)

x<-state.x77[sample(x = nrow(state.x77),size = 10,replace = F),]
x
##                Population Income Illiteracy Life Exp Murder HS Grad Frost
## Texas               12237   4188        2.2       71   12.2      47    35
## California          21198   5114        1.1       72   10.3      63    20
## Virginia             4981   4701        1.4       70    9.5      48    85
## New Hampshire         812   4281        0.7       71    3.3      58   174
## Mississippi          2341   3098        2.4       68   12.5      41    50
## Kentucky             3387   3712        1.6       70   10.6      38    95
## North Carolina       5441   3875        1.8       69   11.1      38    80
## New York            18076   4903        1.4       71   10.9      53    82
## Ohio                10735   4561        0.8       71    7.4      53   124
## Oklahoma             2715   3983        1.1       71    6.4      52    82
##                  Area
## Texas          262134
## California     156361
## Virginia        39780
## New Hampshire    9027
## Mississippi     47296
## Kentucky        39650
## North Carolina  48798
## New York        47831
## Ohio            40975
## Oklahoma        68782
summary(x)
##    Population        Income       Illiteracy      Life Exp      Murder    
##  Min.   :  812   Min.   :3098   Min.   :0.70   Min.   :68   Min.   : 3.3  
##  1st Qu.: 2883   1st Qu.:3902   1st Qu.:1.10   1st Qu.:70   1st Qu.: 7.9  
##  Median : 5211   Median :4234   Median :1.40   Median :71   Median :10.4  
##  Mean   : 8192   Mean   :4242   Mean   :1.45   Mean   :70   Mean   : 9.4  
##  3rd Qu.:11862   3rd Qu.:4666   3rd Qu.:1.75   3rd Qu.:71   3rd Qu.:11.1  
##  Max.   :21198   Max.   :5114   Max.   :2.40   Max.   :72   Max.   :12.5  
##     HS Grad       Frost          Area       
##  Min.   :38   Min.   : 20   Min.   :  9027  
##  1st Qu.:43   1st Qu.: 58   1st Qu.: 40079  
##  Median :50   Median : 82   Median : 47564  
##  Mean   :49   Mean   : 83   Mean   : 76063  
##  3rd Qu.:53   3rd Qu.: 92   3rd Qu.: 63786  
##  Max.   :63   Max.   :174   Max.   :262134
alternative
rand_num = ceiling(runif(10, 1, length(dataState$Population)))
sampState = dataState[rand_num,]
summary(sampState)
##    Population        Income       Illiteracy      Life Exp      Murder    
##  Min.   :  365   Min.   :3983   Min.   :0.50   Min.   :69   Min.   : 3.1  
##  1st Qu.: 1162   1st Qu.:4476   1st Qu.:0.80   1st Qu.:70   1st Qu.: 6.6  
##  Median : 2908   Median :4802   Median :1.10   Median :71   Median : 8.7  
##  Mean   : 5936   Mean   :4878   Mean   :1.13   Mean   :71   Mean   : 8.1  
##  3rd Qu.: 5230   3rd Qu.:5140   3rd Qu.:1.40   3rd Qu.:71   3rd Qu.:10.8  
##  Max.   :21198   Max.   :6315   Max.   :1.80   Max.   :72   Max.   :11.5  
##     HS Grad       Frost          Area       
##  Min.   :48   Min.   : 15   Min.   :  4862  
##  1st Qu.:53   1st Qu.: 82   1st Qu.: 37018  
##  Median :57   Median :104   Median : 58306  
##  Mean   :57   Mean   :106   Mean   :115248  
##  3rd Qu.:61   3rd Qu.:149   3rd Qu.:112535  
##  Max.   :67   Max.   :188   Max.   :566432
dataState 
##                Population Income Illiteracy Life Exp Murder HS Grad Frost
## Alabama              3615   3624        2.1       69   15.1      41    20
## Alaska                365   6315        1.5       69   11.3      67   152
## Arizona              2212   4530        1.8       71    7.8      58    15
## Arkansas             2110   3378        1.9       71   10.1      40    65
## California          21198   5114        1.1       72   10.3      63    20
## Colorado             2541   4884        0.7       72    6.8      64   166
## Connecticut          3100   5348        1.1       72    3.1      56   139
## Delaware              579   4809        0.9       70    6.2      55   103
## Florida              8277   4815        1.3       71   10.7      53    11
## Georgia              4931   4091        2.0       69   13.9      41    60
## Hawaii                868   4963        1.9       74    6.2      62     0
## Idaho                 813   4119        0.6       72    5.3      60   126
## Illinois            11197   5107        0.9       70   10.3      53   127
## Indiana              5313   4458        0.7       71    7.1      53   122
## Iowa                 2861   4628        0.5       73    2.3      59   140
## Kansas               2280   4669        0.6       73    4.5      60   114
## Kentucky             3387   3712        1.6       70   10.6      38    95
## Louisiana            3806   3545        2.8       69   13.2      42    12
## Maine                1058   3694        0.7       70    2.7      55   161
## Maryland             4122   5299        0.9       70    8.5      52   101
## Massachusetts        5814   4755        1.1       72    3.3      58   103
## Michigan             9111   4751        0.9       71   11.1      53   125
## Minnesota            3921   4675        0.6       73    2.3      58   160
## Mississippi          2341   3098        2.4       68   12.5      41    50
## Missouri             4767   4254        0.8       71    9.3      49   108
## Montana               746   4347        0.6       71    5.0      59   155
## Nebraska             1544   4508        0.6       73    2.9      59   139
## Nevada                590   5149        0.5       69   11.5      65   188
## New Hampshire         812   4281        0.7       71    3.3      58   174
## New Jersey           7333   5237        1.1       71    5.2      52   115
## New Mexico           1144   3601        2.2       70    9.7      55   120
## New York            18076   4903        1.4       71   10.9      53    82
## North Carolina       5441   3875        1.8       69   11.1      38    80
## North Dakota          637   5087        0.8       73    1.4      50   186
## Ohio                10735   4561        0.8       71    7.4      53   124
## Oklahoma             2715   3983        1.1       71    6.4      52    82
## Oregon               2284   4660        0.6       72    4.2      60    44
## Pennsylvania        11860   4449        1.0       70    6.1      50   126
## Rhode Island          931   4558        1.3       72    2.4      46   127
## South Carolina       2816   3635        2.3       68   11.6      38    65
## South Dakota          681   4167        0.5       72    1.7      53   172
## Tennessee            4173   3821        1.7       70   11.0      42    70
## Texas               12237   4188        2.2       71   12.2      47    35
## Utah                 1203   4022        0.6       73    4.5      67   137
## Vermont               472   3907        0.6       72    5.5      57   168
## Virginia             4981   4701        1.4       70    9.5      48    85
## Washington           3559   4864        0.6       72    4.3      64    32
## West Virginia        1799   3617        1.4       69    6.7      42   100
## Wisconsin            4589   4468        0.7       72    3.0      54   149
## Wyoming               376   4566        0.6       70    6.9      63   173
##                  Area
## Alabama         50708
## Alaska         566432
## Arizona        113417
## Arkansas        51945
## California     156361
## Colorado       103766
## Connecticut      4862
## Delaware         1982
## Florida         54090
## Georgia         58073
## Hawaii           6425
## Idaho           82677
## Illinois        55748
## Indiana         36097
## Iowa            55941
## Kansas          81787
## Kentucky        39650
## Louisiana       44930
## Maine           30920
## Maryland         9891
## Massachusetts    7826
## Michigan        56817
## Minnesota       79289
## Mississippi     47296
## Missouri        68995
## Montana        145587
## Nebraska        76483
## Nevada         109889
## New Hampshire    9027
## New Jersey       7521
## New Mexico     121412
## New York        47831
## North Carolina  48798
## North Dakota    69273
## Ohio            40975
## Oklahoma        68782
## Oregon          96184
## Pennsylvania    44966
## Rhode Island     1049
## South Carolina  30225
## South Dakota    75955
## Tennessee       41328
## Texas          262134
## Utah            82096
## Vermont          9267
## Virginia        39780
## Washington      66570
## West Virginia   24070
## Wisconsin       54464
## Wyoming         97203
cor(dataState$Murder,dataState$Illiteracy )
## [1] 0.7

1d

quantile(x = dataState$Area,.33)
##   33% 
## 41940
quantile(x = dataState$Area,.66)
##   66% 
## 69090
dataState$category[dataState$Area<41940.34 ]<-"small"
dataState$category[dataState$Area>= 41940.34 & dataState$Area<69089.52  ]<-"mediam"
dataState$category[dataState$Area>=69089.52  ]<-"big"

table(dataState$category)
## 
##    big mediam  small 
##     17     16     17

2

par(mfrow=c(2,2))
plot(trees$Volume,trees$Height)
plot(trees$Girth,trees$Volume)
hist(trees$Height)
boxplot(trees$Volume)

3

3a

rm(list = ls())
quantile(swiss$Infant.Mortality)
##   0%  25%  50%  75% 100% 
##   11   18   20   22   27
swiss$categories[swiss$Infant.Mortality<=18.15]<-"Good"
swiss$categories[swiss$Infant.Mortalit>18.15 &
                   swiss$Infant.Mortality<=20]<-"Normal"
swiss$categories[swiss$Infant.Mortality>20 &
                   swiss$Infant.Mortality<=21.70]<-"Weaker"
swiss$categories[swiss$Infant.Mortality>21.70 ]<-"Poor"
swiss
##              Fertility Agriculture Examination Education Catholic
## Courtelary          80        17.0          15        12     10.0
## Delemont            83        45.1           6         9     84.8
## Franches-Mnt        92        39.7           5         5     93.4
## Moutier             86        36.5          12         7     33.8
## Neuveville          77        43.5          17        15      5.2
## Porrentruy          76        35.3           9         7     90.6
## Broye               84        70.2          16         7     92.8
## Glane               92        67.8          14         8     97.2
## Gruyere             82        53.3          12         7     97.7
## Sarine              83        45.2          16        13     91.4
## Veveyse             87        64.5          14         6     98.6
## Aigle               64        62.0          21        12      8.5
## Aubonne             67        67.5          14         7      2.3
## Avenches            69        60.7          19        12      4.4
## Cossonay            62        69.3          22         5      2.8
## Echallens           68        72.6          18         2     24.2
## Grandson            72        34.0          17         8      3.3
## Lausanne            56        19.4          26        28     12.1
## La Vallee           54        15.2          31        20      2.1
## Lavaux              65        73.0          19         9      2.8
## Morges              66        59.8          22        10      5.2
## Moudon              65        55.1          14         3      4.5
## Nyone               57        50.9          22        12     15.1
## Orbe                57        54.1          20         6      4.2
## Oron                72        71.2          12         1      2.4
## Payerne             74        58.1          14         8      5.2
## Paysd'enhaut        72        63.5           6         3      2.6
## Rolle               60        60.8          16        10      7.7
## Vevey               58        26.8          25        19     18.5
## Yverdon             65        49.5          15         8      6.1
## Conthey             76        85.9           3         2     99.7
## Entremont           69        84.9           7         6     99.7
## Herens              77        89.7           5         2    100.0
## Martigwy            70        78.2          12         6     99.0
## Monthey             79        64.9           7         3     98.2
## St Maurice          65        75.9           9         9     99.1
## Sierre              92        84.6           3         3     99.5
## Sion                79        63.1          13        13     96.8
## Boudry              70        38.4          26        12      5.6
## La Chauxdfnd        66         7.7          29        11     13.8
## Le Locle            73        16.7          22        13     11.2
## Neuchatel           64        17.6          35        32     16.9
## Val de Ruz          78        37.6          15         7      5.0
## ValdeTravers        68        18.7          25         7      8.6
## V. De Geneve        35         1.2          37        53     42.3
## Rive Droite         45        46.6          16        29     50.4
## Rive Gauche         43        27.7          22        29     58.3
##              Infant.Mortality categories
## Courtelary                 22       Poor
## Delemont                   22       Poor
## Franches-Mnt               20     Weaker
## Moutier                    20     Weaker
## Neuveville                 21     Weaker
## Porrentruy                 27       Poor
## Broye                      24       Poor
## Glane                      25       Poor
## Gruyere                    21     Weaker
## Sarine                     24       Poor
## Veveyse                    24       Poor
## Aigle                      16       Good
## Aubonne                    19     Normal
## Avenches                   23       Poor
## Cossonay                   19     Normal
## Echallens                  21     Weaker
## Grandson                   20     Normal
## Lausanne                   20     Weaker
## La Vallee                  11       Good
## Lavaux                     20     Normal
## Morges                     18       Good
## Moudon                     22       Poor
## Nyone                      17       Good
## Orbe                       15       Good
## Oron                       21     Weaker
## Payerne                    24       Poor
## Paysd'enhaut               18       Good
## Rolle                      16       Good
## Vevey                      21     Weaker
## Yverdon                    22       Poor
## Conthey                    15       Good
## Entremont                  20     Normal
## Herens                     18     Normal
## Martigwy                   19     Normal
## Monthey                    20     Weaker
## St Maurice                 18       Good
## Sierre                     16       Good
## Sion                       18       Good
## Boudry                     20     Weaker
## La Chauxdfnd               20     Weaker
## Le Locle                   19     Normal
## Neuchatel                  23       Poor
## Val de Ruz                 20     Normal
## ValdeTravers               20     Normal
## V. De Geneve               18       Good
## Rive Droite                18     Normal
## Rive Gauche                19     Normal
table(swiss$categories)
## 
##   Good Normal   Poor Weaker 
##     12     12     12     11

3b

library(ggplot2)
ggplot(data = swiss)+
  geom_bar(mapping = aes(categories))

## alternative
barplot(table(swiss$categories))

3c

is.data.frame(swiss)
## [1] TRUE
swiss[sample(x = nrow(swiss),size = 15,replace = F),]
##              Fertility Agriculture Examination Education Catholic
## St Maurice          65          76           9         9     99.1
## Rive Droite         45          47          16        29     50.4
## Sarine              83          45          16        13     91.4
## Neuchatel           64          18          35        32     16.9
## Yverdon             65          50          15         8      6.1
## Aigle               64          62          21        12      8.5
## Paysd'enhaut        72          64           6         3      2.6
## Conthey             76          86           3         2     99.7
## Val de Ruz          78          38          15         7      5.0
## Aubonne             67          68          14         7      2.3
## Lavaux              65          73          19         9      2.8
## Martigwy            70          78          12         6     99.0
## Veveyse             87          64          14         6     98.6
## Rive Gauche         43          28          22        29     58.3
## Herens              77          90           5         2    100.0
##              Infant.Mortality categories
## St Maurice                 18       Good
## Rive Droite                18     Normal
## Sarine                     24       Poor
## Neuchatel                  23       Poor
## Yverdon                    22       Poor
## Aigle                      16       Good
## Paysd'enhaut               18       Good
## Conthey                    15       Good
## Val de Ruz                 20     Normal
## Aubonne                    19     Normal
## Lavaux                     20     Normal
## Martigwy                   19     Normal
## Veveyse                    24       Poor
## Rive Gauche                19     Normal
## Herens                     18     Normal
summary(swiss)
##    Fertility   Agriculture  Examination   Education     Catholic  
##  Min.   :35   Min.   : 1   Min.   : 3   Min.   : 1   Min.   :  2  
##  1st Qu.:65   1st Qu.:36   1st Qu.:12   1st Qu.: 6   1st Qu.:  5  
##  Median :70   Median :54   Median :16   Median : 8   Median : 15  
##  Mean   :70   Mean   :51   Mean   :16   Mean   :11   Mean   : 41  
##  3rd Qu.:78   3rd Qu.:68   3rd Qu.:22   3rd Qu.:12   3rd Qu.: 93  
##  Max.   :92   Max.   :90   Max.   :37   Max.   :53   Max.   :100  
##  Infant.Mortality  categories       
##  Min.   :10.8     Length:47         
##  1st Qu.:18.1     Class :character  
##  Median :20.0     Mode  :character  
##  Mean   :19.9                       
##  3rd Qu.:21.7                       
##  Max.   :26.6
summary(swiss$Agriculture)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##       1      36      54      51      68      90
summary(swiss$Education)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##       1       6       8      11      12      53

4

#library(reshape)

#aggregate(x =  mtcars,by = list(cyl)  ,FUN = "mean")
#aggregate(x =  mtcars,by = list(gear)  ,FUN = "median")

alternative

#cast(data = mtcars,cyl~.,mean)
#cast(data = mtcars,gear~.,mean)

5

aggregate(x = warpbreaks$breaks,by = list(warpbreaks$wool ,warpbreaks$tension),FUN = "mean")
##   Group.1 Group.2  x
## 1       A       L 45
## 2       B       L 28
## 3       A       M 24
## 4       B       M 29
## 5       A       H 25
## 6       B       H 19

6

library(dplyr)
library(MASS)

7

7a

x<-c(1:1:100)
sum(x)
## [1] 5050

8

pdf<-function(a,x){
  return(a*exp(-a*x))
}
cdf<-function(a,x){
  return(1-exp(-a*x))
}
x<-c(1:20)

par(mfrow=c(2,2))
plot(x = x,y = pdf(1,x))
plot(x = x,y = cdf(1,x))

plot(pdf(2,x))
plot(cdf(2,x))

2020

Q1

1a

#What is meant by wrangling? Illustrate with example.

Data wrangling, also known as data munging, is the process of cleaning, transforming, and preparing data for analysis. It involves working with data that may be incomplete, inconsistent, or in a format that is not suitable for analysis. The goal of data wrangling is to transform the data into a format that is usable and appropriate for analysis, so that insights and value can be extracted from it.

The data wrangling process typically includes tasks such as:

  1. Handling missing or incomplete data by imputing, deleting, or estimating missing values.

  2. Removing duplicates or irrelevant data

  3. Correcting formatting errors

  4. Normalizing data by converting it into a consistent format

  5. Combining data from multiple sources into a single dataset

  6. Aggregating and summarizing data Overall, data wrangling is an essential step in the data analysis process, as it ensures that the data is accurate, complete, and usable for analysis.

alternative

I can give you an example to help illustrate what is meant by data wrangling.

Let’s say you have a dataset containing information about a group of people, including their names, ages, and the city they live in. However, the dataset is messy and contains a lot of errors and inconsistencies. Here’s an example of how you might use data wrangling techniques to clean up the data:

  1. Removing duplicate records: You notice that the dataset contains multiple records for some individuals. For example, there are two records for a person named “John Smith”, with slightly different ages and addresses. To clean up the data, you decide to remove the duplicate records so that each person only appears once in the dataset.

  2. Fixing formatting errors: You notice that some of the city names are not consistently formatted. For example, some records list the city as “New York City”, while others list it as “NYC” or “New York”. To make the data consistent, you decide to standardize all city names to the full name “New York City”.

  3. Handling missing values: You notice that some records are missing information, such as an age or a city. To handle these missing values, you decide to either fill in the missing values with reasonable estimates (e.g. the average age of people in the dataset), or to remove the records entirely if they contain too many missing values.

These are just a few examples of the types of tasks that might be involved in data wrangling. The goal of data wrangling is to clean and transform the data so that it is ready for analysis, which can involve statistical modeling, machine learning, or other data science techniques.

1(b)

The ‘tidyverse’ is a collection of packages for data manipulation, exploration, and visualization in R. The core packages in the ‘tidyverse’ include:

  1. ggplot2: for creating high-quality visualizations
  2. dplyr: for data manipulation and transformation
  3. tidyr: for cleaning and tidying data
  4. readr: for reading in data in various formats
  5. purrr: for functional programming and iterating over data
  6. tibble: for creating and working with data frames
  7. stringr: for working with strings and text data
  8. forcats: for working with categorical data

These packages all follow a consistent set of design principles and syntax, making it easy to learn and use them together. The ‘tidyverse’ philosophy emphasizes the use of tidy data principles, which means that data should be structured in a consistent way to facilitate analysis and visualization.

1(c)

1.c.(i)

Question :

library(ggplot2) ggplot(data = diamonds,mapping = aes(x = cut,fill = “clarity” ))+ geom_bar(alpha = 1/5,position = “clarity”)

The code you provided uses the ggplot2 package in R to create a bar plot of the “cut” variable from the “diamonds” dataset. The plot uses the “fill” aesthetic to color the bars based on the “clarity” variable.

Here’s a breakdown of the code:

  1. library(ggplot2) loads the ggplot2 package.

  2. ggplot(data = diamonds, mapping = aes(x = cut, fill = “clarity”)) initializes a ggplot object, specifying that the data is the “diamonds” dataset and the x-axis variable is “cut”. The “fill” aesthetic is also set to “clarity”, which will be used to color the bars.

  3. geom_bar(alpha = 1/5, position = “clarity”) specifies that the plot should be a bar plot (geom_bar) with an alpha of 1/5 (for transparency) and the bars should be positioned based on the “clarity” variable.

The output of this code would be a bar plot with bars representing the frequency of each “cut” category, colored based on the “clarity” variable. However, there is an error in the code as the “fill” aesthetic should be mapped to the clarity variable and not a string (“clarity”).

Here’s the corrected code:

ggplot(data = diamonds, mapping = aes(x = cut, fill = clarity)) +
  geom_bar(alpha = 1/5, position = "stack")

#alternative
rm(list = ls())
library(tidyverse)

ggplot(data = diamonds, mapping = aes(x = cut, fill = clarity)) + 
  geom_bar(alpha = 1/5, position = 'identity')

This should produce a bar plot with bars representing the frequency of each “cut” category, colored based on the “clarity” variable, and stacked on top of each other.

1.c.(ii)

Question :

library(ggplot2) ggplot(data = diamonds,mapping = aes(x = cut,fill = “clarity” ))+ geom_bar(fill = NA ,position = “identity”)

The code you provided uses the ggplot2 package in R to create a bar plot of the “cut” variable from the “diamonds” dataset. The plot uses the “fill” aesthetic to color the bars based on a constant string (“clarity”).

Here’s a breakdown of the code:

  1. library(ggplot2) loads the ggplot2 package.

  2. ggplot(data = diamonds, mapping = aes(x = cut, fill = “clarity”)) initializes a ggplot object, specifying that the data is the “diamonds” dataset and the x-axis variable is “cut”. The “fill” aesthetic is also set to a constant string (“clarity”), which will be used to color the bars.

  3. geom_bar(fill = NA ,position = “identity”) specifies that the plot should be a bar plot (geom_bar) with no fill color (fill = NA) and the bars should be positioned with no stacking (position = “identity”).

The output of this code would be a bar plot with bars representing the frequency of each “cut” category, but with no fill color. The constant string used for the “fill” aesthetic doesn’t affect the plot since the fill = NA argument overrides it. The bars are also positioned side by side (position = “identity”), which is the default for a bar plot.

Overall, this code is not very useful for visualizing the data in the “diamonds” dataset since it doesn’t show any meaningful relationship between the variables.

library(ggplot2)
ggplot(data = diamonds, mapping = aes(x = cut, fill = "clarity")) +
  geom_bar( position = "identity")

1.b.(iii)

Question :

ggplot(data= diamonds)+ stat_summary(mapping = aes(x = cut,y = price),fun.ymin = min,fun.ymax = max,fun.y = median)

This code creates a plot of the “diamonds” dataset using ggplot2 in R. The stat_summary() function is used to add summary statistics to the plot. In particular, the fun.ymin, fun.ymax, and fun.y arguments are set to min, max, and median, respectively, which means that the plot will show the minimum, maximum, and median values of the “price” variable for each level of the “cut” variable.

The mapping argument specifies the aesthetics for the plot, with x set to “cut” and y set to “price”. The resulting plot will have “cut” on the x-axis and “price” on the y-axis, with boxes that show the median, minimum, and maximum values for each level of “cut”.

Note that the exact appearance of the plot will depend on additional specifications such as the theme, axis labels, and plot title.

2

2(a)

reg<-function(x,y){
  beta1<-sum((x-mean(x))*(y-mean(y)))/sum((x-mean(x))^2)
  beta0<-mean(y)-beta1*mean(x)
  
  return(c(b0=beta0,b1= beta1))
}
x = c(1,4,8,9,12,16,17,19,27)
y = c(23,59,50,25,29,45,24,75,23)
reg(x,y)
##    b0    b1 
## 39.85 -0.05
#by default function
lm(y~x)
## 
## Call:
## lm(formula = y ~ x)
## 
## Coefficients:
## (Intercept)            x  
##       39.85        -0.05

2(b)

correl<-function(x,y){
  a<-sum((x-mean(x))*(y-mean(y)))/sqrt(sum((x-mean(x))^2)*sum((y-mean(y))^2))
  
  
  return( a)
}
x = c(1,4,8,9,12,16,17,19,27)
y = c(23,59,50,25,29,45,24,75,23)
correl(x,y)
## [1] -0.021
#by default function
cor(x,y)
## [1] -0.021

3

3(i)

The melt() function is a part of the reshape2 package in R, and it is used to reshape data from a wide format to a long format. The long format is often more useful for analysis and visualization purposes.

The basic idea of melt() is to take columns of a data frame and “melt” them down into rows, while preserving other columns that are used as identifiers. Here’s an example:

library(reshape2)

# Create a sample data frame
df <- data.frame(id = 1:3, x1 = c(2, 4, 6), x2 = c(3, 5, 7))

# Melt the data frame
melted_df <- melt(df, id.vars = "id")

# Print the original and melted data frames
print(df)
##   id x1 x2
## 1  1  2  3
## 2  2  4  5
## 3  3  6  7
print(melted_df)
##   id variable value
## 1  1       x1     2
## 2  2       x1     4
## 3  3       x1     6
## 4  1       x2     3
## 5  2       x2     5
## 6  3       x2     7

3(ii)

The cast() function in R is used to reshape data from long format to wide format. It is the opposite of the melt() function. This function is part of the reshape2 package in R.

The basic idea of cast() is to take a data frame that has a single row for each observation, but multiple columns for different variables, and convert it to a data frame with a single row for each value of a particular variable. Here’s an example:

library(reshape)
# Create a sample data frame
df <- data.frame(
  id = rep(1:3, each = 2),
  variable = c("x1", "x2", "x1", "x2", "x1", "x2"),
  value = c(2, 3, 4, 5, 6, 7)
)

# Cast the data frame
casted_df <- cast(df, id ~ variable, value = "value")

# Print the original and casted data frames
print(df)
##   id variable value
## 1  1       x1     2
## 2  1       x2     3
## 3  2       x1     4
## 4  2       x2     5
## 5  3       x1     6
## 6  3       x2     7
print(casted_df)
##   id x1 x2
## 1  1  2  3
## 2  2  4  5
## 3  3  6  7

3(iii)

The grep function is used to search for a specified pattern in a vector or a text file and returns the indices or the values that match the pattern. It is useful for extracting specific information from text data.

Syntax: grep(pattern, x, …)

Where pattern is the regular expression to search for, x is the vector or text file to search in, and … are optional arguments such as ignore.case and value.

Example:

Suppose we have a vector of strings representing the names of fruits:

fruits <- c("apple", "banana", "orange", "kiwi", "pear", "mango")
grep("a", fruits)
## [1] 1 2 3 5 6

We can use grep to search for all the fruits that contain the letter “a” in their name:

Here, grep returns the indices of the elements in the fruits vector that contain the letter “a” in their name.

We can also use grep to extract the fruits that contain the letter “a” in their name:

grep("a", fruits, value = TRUE)
## [1] "apple"  "banana" "orange" "pear"   "mango"

Here, grep returns the values of the elements in the fruits vector that contain the letter “a” in their name.

Note that grep is case-sensitive by default, but this can be changed using the ignore.case argument.

3(iv)

The qnorm function is used to calculate the quantiles of a normal distribution given a probability value. It is the inverse of the cumulative distribution function pnorm.

Syntax: qnorm(p, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)

Where p is the probability value, mean and sd are the mean and standard deviation of the normal distribution, lower.tail is a logical value indicating whether to calculate the probability for the lower or upper tail of the distribution, and log.p is a logical value indicating whether p is given in log scale or not.

Example:

Suppose we want to find the quantile of a standard normal distribution for a given probability value 0.95:

qnorm(0.95)
## [1] 1.6
#We can also find the quantile of a normal distribution with a given mean and standard deviation for a probability value of 0.9:

qnorm(0.9, mean = 50, sd = 10)
## [1] 63

3(v)

ggplot is a powerful data visualization package in the R programming language. It is used to create beautiful and informative graphics, and provides a wide range of customization options.

The basic syntax for creating a ggplot object is as follows:

ggplot(data = ) + (mapping = aes()) Here, the data argument specifies the dataset to be plotted, and the argument specifies the type of plot to be created (e.g. geom_point() for a scatter plot, geom_line() for a line plot, etc.). The mapping argument specifies how the variables in the dataset should be mapped to the aesthetics of the plot (e.g. x-axis, y-axis, color, etc.).

Here’s an example of how to use ggplot to create a scatter plot:

library(ggplot2)

# create a dataset
data <- data.frame(x = rnorm(100), y = rnorm(100))

# create a ggplot object
p <- ggplot(data = data) + 
  geom_point(mapping = aes(x = x, y = y))

# display the plot
p

In this example, we first create a dataset with 100 random points in two dimensions. We then create a ggplot object with data = data and geom_point() as the geometric object. We use the mapping argument to specify that the x and y variables in the dataset should be mapped to the x-axis and y-axis of the plot, respectively. Finally, we display the plot using the p object.

This produces a scatter plot with the x-axis and y-axis labeled and scaled appropriately, and with each point represented as a dot. We can customize the plot further by adding additional geometric objects, changing the color or size of the points, adding titles or legends, etc. The flexibility and power of ggplot makes it a very useful tool for exploring and visualizing data.

4

4(a)

library(tidyverse)
AirPassengers
##      Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
## 1949 112 118 132 129 121 135 148 148 136 119 104 118
## 1950 115 126 141 135 125 149 170 170 158 133 114 140
## 1951 145 150 178 163 172 178 199 199 184 162 146 166
## 1952 171 180 193 181 183 218 230 242 209 191 172 194
## 1953 196 196 236 235 229 243 264 272 237 211 180 201
## 1954 204 188 235 227 234 264 302 293 259 229 203 229
## 1955 242 233 267 269 270 315 364 347 312 274 237 278
## 1956 284 277 317 313 318 374 413 405 355 306 271 306
## 1957 315 301 356 348 355 422 465 467 404 347 305 336
## 1958 340 318 362 348 363 435 491 505 404 359 310 337
## 1959 360 342 406 396 420 472 548 559 463 407 362 405
## 1960 417 391 419 461 472 535 622 606 508 461 390 432
data<-as.data.frame(AirPassengers)
month.abb
##  [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
data$year<-rep(c(1949:1960),each=12)
data$month<-rep(month.abb)
data
##       x year month
## 1   112 1949   Jan
## 2   118 1949   Feb
## 3   132 1949   Mar
## 4   129 1949   Apr
## 5   121 1949   May
## 6   135 1949   Jun
## 7   148 1949   Jul
## 8   148 1949   Aug
## 9   136 1949   Sep
## 10  119 1949   Oct
## 11  104 1949   Nov
## 12  118 1949   Dec
## 13  115 1950   Jan
## 14  126 1950   Feb
## 15  141 1950   Mar
## 16  135 1950   Apr
## 17  125 1950   May
## 18  149 1950   Jun
## 19  170 1950   Jul
## 20  170 1950   Aug
## 21  158 1950   Sep
## 22  133 1950   Oct
## 23  114 1950   Nov
## 24  140 1950   Dec
## 25  145 1951   Jan
## 26  150 1951   Feb
## 27  178 1951   Mar
## 28  163 1951   Apr
## 29  172 1951   May
## 30  178 1951   Jun
## 31  199 1951   Jul
## 32  199 1951   Aug
## 33  184 1951   Sep
## 34  162 1951   Oct
## 35  146 1951   Nov
## 36  166 1951   Dec
## 37  171 1952   Jan
## 38  180 1952   Feb
## 39  193 1952   Mar
## 40  181 1952   Apr
## 41  183 1952   May
## 42  218 1952   Jun
## 43  230 1952   Jul
## 44  242 1952   Aug
## 45  209 1952   Sep
## 46  191 1952   Oct
## 47  172 1952   Nov
## 48  194 1952   Dec
## 49  196 1953   Jan
## 50  196 1953   Feb
## 51  236 1953   Mar
## 52  235 1953   Apr
## 53  229 1953   May
## 54  243 1953   Jun
## 55  264 1953   Jul
## 56  272 1953   Aug
## 57  237 1953   Sep
## 58  211 1953   Oct
## 59  180 1953   Nov
## 60  201 1953   Dec
## 61  204 1954   Jan
## 62  188 1954   Feb
## 63  235 1954   Mar
## 64  227 1954   Apr
## 65  234 1954   May
## 66  264 1954   Jun
## 67  302 1954   Jul
## 68  293 1954   Aug
## 69  259 1954   Sep
## 70  229 1954   Oct
## 71  203 1954   Nov
## 72  229 1954   Dec
## 73  242 1955   Jan
## 74  233 1955   Feb
## 75  267 1955   Mar
## 76  269 1955   Apr
## 77  270 1955   May
## 78  315 1955   Jun
## 79  364 1955   Jul
## 80  347 1955   Aug
## 81  312 1955   Sep
## 82  274 1955   Oct
## 83  237 1955   Nov
## 84  278 1955   Dec
## 85  284 1956   Jan
## 86  277 1956   Feb
## 87  317 1956   Mar
## 88  313 1956   Apr
## 89  318 1956   May
## 90  374 1956   Jun
## 91  413 1956   Jul
## 92  405 1956   Aug
## 93  355 1956   Sep
## 94  306 1956   Oct
## 95  271 1956   Nov
## 96  306 1956   Dec
## 97  315 1957   Jan
## 98  301 1957   Feb
## 99  356 1957   Mar
## 100 348 1957   Apr
## 101 355 1957   May
## 102 422 1957   Jun
## 103 465 1957   Jul
## 104 467 1957   Aug
## 105 404 1957   Sep
## 106 347 1957   Oct
## 107 305 1957   Nov
## 108 336 1957   Dec
## 109 340 1958   Jan
## 110 318 1958   Feb
## 111 362 1958   Mar
## 112 348 1958   Apr
## 113 363 1958   May
## 114 435 1958   Jun
## 115 491 1958   Jul
## 116 505 1958   Aug
## 117 404 1958   Sep
## 118 359 1958   Oct
## 119 310 1958   Nov
## 120 337 1958   Dec
## 121 360 1959   Jan
## 122 342 1959   Feb
## 123 406 1959   Mar
## 124 396 1959   Apr
## 125 420 1959   May
## 126 472 1959   Jun
## 127 548 1959   Jul
## 128 559 1959   Aug
## 129 463 1959   Sep
## 130 407 1959   Oct
## 131 362 1959   Nov
## 132 405 1959   Dec
## 133 417 1960   Jan
## 134 391 1960   Feb
## 135 419 1960   Mar
## 136 461 1960   Apr
## 137 472 1960   May
## 138 535 1960   Jun
## 139 622 1960   Jul
## 140 606 1960   Aug
## 141 508 1960   Sep
## 142 461 1960   Oct
## 143 390 1960   Nov
## 144 432 1960   Dec
max(data$x)
## [1] 622
alternative
data("AirPassengers")
date_raw = time(AirPassengers)[which.max(AirPassengers)]
max_year = floor(date_raw)
max_year
## [1] 1960
month_raw = date_raw - max_year
month_raw
## [1] 0.5
month_index = 12*month_raw
month_index
## [1] 6
max_month = month.abb[month_index+1]
AirPassengers
##      Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
## 1949 112 118 132 129 121 135 148 148 136 119 104 118
## 1950 115 126 141 135 125 149 170 170 158 133 114 140
## 1951 145 150 178 163 172 178 199 199 184 162 146 166
## 1952 171 180 193 181 183 218 230 242 209 191 172 194
## 1953 196 196 236 235 229 243 264 272 237 211 180 201
## 1954 204 188 235 227 234 264 302 293 259 229 203 229
## 1955 242 233 267 269 270 315 364 347 312 274 237 278
## 1956 284 277 317 313 318 374 413 405 355 306 271 306
## 1957 315 301 356 348 355 422 465 467 404 347 305 336
## 1958 340 318 362 348 363 435 491 505 404 359 310 337
## 1959 360 342 406 396 420 472 548 559 463 407 362 405
## 1960 417 391 419 461 472 535 622 606 508 461 390 432
paste("Maximum passenger", max(AirPassengers), "in", max_month, max_year)
## [1] "Maximum passenger 622 in Jul 1960"

4b

plot(AirPassengers)

4c

library(ggplot2)

x<-factor(cycle(AirPassengers))
x
##   [1] 1  2  3  4  5  6  7  8  9  10 11 12 1  2  3  4  5  6  7  8  9  10 11 12 1 
##  [26] 2  3  4  5  6  7  8  9  10 11 12 1  2  3  4  5  6  7  8  9  10 11 12 1  2 
##  [51] 3  4  5  6  7  8  9  10 11 12 1  2  3  4  5  6  7  8  9  10 11 12 1  2  3 
##  [76] 4  5  6  7  8  9  10 11 12 1  2  3  4  5  6  7  8  9  10 11 12 1  2  3  4 
## [101] 5  6  7  8  9  10 11 12 1  2  3  4  5  6  7  8  9  10 11 12 1  2  3  4  5 
## [126] 6  7  8  9  10 11 12 1  2  3  4  5  6  7  8  9  10 11 12
## Levels: 1 2 3 4 5 6 7 8 9 10 11 12
ggplot()+
  geom_boxplot(mapping = aes(x = x,y = AirPassengers ))
## Don't know how to automatically pick scale for object of type <ts>. Defaulting
## to continuous.

alternative
library(tidyverse)
data<-as.data.frame(AirPassengers)
month.abb
##  [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
data$year<-rep(c(1949:1960),each=12)
data$month<-rep(month.abb)
data
##       x year month
## 1   112 1949   Jan
## 2   118 1949   Feb
## 3   132 1949   Mar
## 4   129 1949   Apr
## 5   121 1949   May
## 6   135 1949   Jun
## 7   148 1949   Jul
## 8   148 1949   Aug
## 9   136 1949   Sep
## 10  119 1949   Oct
## 11  104 1949   Nov
## 12  118 1949   Dec
## 13  115 1950   Jan
## 14  126 1950   Feb
## 15  141 1950   Mar
## 16  135 1950   Apr
## 17  125 1950   May
## 18  149 1950   Jun
## 19  170 1950   Jul
## 20  170 1950   Aug
## 21  158 1950   Sep
## 22  133 1950   Oct
## 23  114 1950   Nov
## 24  140 1950   Dec
## 25  145 1951   Jan
## 26  150 1951   Feb
## 27  178 1951   Mar
## 28  163 1951   Apr
## 29  172 1951   May
## 30  178 1951   Jun
## 31  199 1951   Jul
## 32  199 1951   Aug
## 33  184 1951   Sep
## 34  162 1951   Oct
## 35  146 1951   Nov
## 36  166 1951   Dec
## 37  171 1952   Jan
## 38  180 1952   Feb
## 39  193 1952   Mar
## 40  181 1952   Apr
## 41  183 1952   May
## 42  218 1952   Jun
## 43  230 1952   Jul
## 44  242 1952   Aug
## 45  209 1952   Sep
## 46  191 1952   Oct
## 47  172 1952   Nov
## 48  194 1952   Dec
## 49  196 1953   Jan
## 50  196 1953   Feb
## 51  236 1953   Mar
## 52  235 1953   Apr
## 53  229 1953   May
## 54  243 1953   Jun
## 55  264 1953   Jul
## 56  272 1953   Aug
## 57  237 1953   Sep
## 58  211 1953   Oct
## 59  180 1953   Nov
## 60  201 1953   Dec
## 61  204 1954   Jan
## 62  188 1954   Feb
## 63  235 1954   Mar
## 64  227 1954   Apr
## 65  234 1954   May
## 66  264 1954   Jun
## 67  302 1954   Jul
## 68  293 1954   Aug
## 69  259 1954   Sep
## 70  229 1954   Oct
## 71  203 1954   Nov
## 72  229 1954   Dec
## 73  242 1955   Jan
## 74  233 1955   Feb
## 75  267 1955   Mar
## 76  269 1955   Apr
## 77  270 1955   May
## 78  315 1955   Jun
## 79  364 1955   Jul
## 80  347 1955   Aug
## 81  312 1955   Sep
## 82  274 1955   Oct
## 83  237 1955   Nov
## 84  278 1955   Dec
## 85  284 1956   Jan
## 86  277 1956   Feb
## 87  317 1956   Mar
## 88  313 1956   Apr
## 89  318 1956   May
## 90  374 1956   Jun
## 91  413 1956   Jul
## 92  405 1956   Aug
## 93  355 1956   Sep
## 94  306 1956   Oct
## 95  271 1956   Nov
## 96  306 1956   Dec
## 97  315 1957   Jan
## 98  301 1957   Feb
## 99  356 1957   Mar
## 100 348 1957   Apr
## 101 355 1957   May
## 102 422 1957   Jun
## 103 465 1957   Jul
## 104 467 1957   Aug
## 105 404 1957   Sep
## 106 347 1957   Oct
## 107 305 1957   Nov
## 108 336 1957   Dec
## 109 340 1958   Jan
## 110 318 1958   Feb
## 111 362 1958   Mar
## 112 348 1958   Apr
## 113 363 1958   May
## 114 435 1958   Jun
## 115 491 1958   Jul
## 116 505 1958   Aug
## 117 404 1958   Sep
## 118 359 1958   Oct
## 119 310 1958   Nov
## 120 337 1958   Dec
## 121 360 1959   Jan
## 122 342 1959   Feb
## 123 406 1959   Mar
## 124 396 1959   Apr
## 125 420 1959   May
## 126 472 1959   Jun
## 127 548 1959   Jul
## 128 559 1959   Aug
## 129 463 1959   Sep
## 130 407 1959   Oct
## 131 362 1959   Nov
## 132 405 1959   Dec
## 133 417 1960   Jan
## 134 391 1960   Feb
## 135 419 1960   Mar
## 136 461 1960   Apr
## 137 472 1960   May
## 138 535 1960   Jun
## 139 622 1960   Jul
## 140 606 1960   Aug
## 141 508 1960   Sep
## 142 461 1960   Oct
## 143 390 1960   Nov
## 144 432 1960   Dec
## main part
ggplot()+
  geom_boxplot(mapping = aes(x = data$month,y = AirPassengers))
## Don't know how to automatically pick scale for object of type <ts>. Defaulting
## to continuous.

5

5a

rm(list=ls())
x<-read.csv(file = "D:/2nd Year/AST 230/avgpm25.csv")

summary(x$pm25)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     3.4     8.5    10.0     9.8    11.4    18.4
boxplot(x$pm25)

hist(x$pm25)

2021

Q2

2a

rm(list=ls())

data<- read.csv("D:/2nd Year/AST 230/Final excel/data1.csv")

summary(data)
##        ID         age        gender              height         weight  
##  Min.   :1   Min.   :27   Length:5           Min.   :1.58   Min.   :60  
##  1st Qu.:2   1st Qu.:29   Class :character   1st Qu.:1.63   1st Qu.:63  
##  Median :3   Median :35   Mode  :character   Median :1.64   Median :64  
##  Mean   :3   Mean   :44                      Mean   :1.67   Mean   :66  
##  3rd Qu.:4   3rd Qu.:56                      3rd Qu.:1.71   3rd Qu.:66  
##  Max.   :5   Max.   :74                      Max.   :1.78   Max.   :78

2b

library(haven)
## Warning: package 'haven' was built under R version 4.2.2
library(foreign)

breaks<-read.csv("D:/2nd Year/AST 230/Final excel/breaks.csv")
breaks
##    breaks wool tension
## 1      26    A       L
## 2      30    A       L
## 3      54    A       L
## 4      47    B       L
## 5      26    B       L
## 6      48    B       M
## 7      58    C       M
## 8      76    C       M
## 9      21    C       M
## 10     56    D       M
## 11     96    D       M
## 12     30    D       M
## 13     14    D       M
## 14     45    D       N
## 15     65    D       N
## 16     32    E       N
## 17     14    E       N
## 18     85    E       N
## 19     45    E       N
bpwide <- read_dta("D:/2nd Year/AST 230/Final excel/bpwide.dta")
bpwide
## # A tibble: 120 × 5
##    patient sex       agegrp    bp_before bp_after
##      <dbl> <dbl+lbl> <dbl+lbl>     <dbl>    <dbl>
##  1       1 0 [Male]  1 [30-45]       143      153
##  2       2 0 [Male]  1 [30-45]       163      170
##  3       3 0 [Male]  1 [30-45]       153      168
##  4       4 0 [Male]  1 [30-45]       153      142
##  5       5 0 [Male]  1 [30-45]       146      141
##  6       6 0 [Male]  1 [30-45]       150      147
##  7       7 0 [Male]  1 [30-45]       148      133
##  8       8 0 [Male]  1 [30-45]       153      141
##  9       9 0 [Male]  1 [30-45]       153      131
## 10      10 0 [Male]  1 [30-45]       158      125
## # … with 110 more rows
world95 <- read.spss("D:/2nd Year/AST 230/Final excel/world95.sav", to.data.frame = T)
## re-encoding from CP1252
## Warning in read.spss("D:/2nd Year/AST 230/Final excel/world95.sav",
## to.data.frame = T): Undeclared level(s) Animist, Buddhist, Catholic, Hindu,
## Jewish, Muslim, Orthodox, Protstnt, Taoist, Tribal added in variable: religion
## Warning in read.spss("D:/2nd Year/AST 230/Final excel/world95.sav",
## to.data.frame = T): Undeclared level(s) 4 added in variable: climate
world95
##          country populatn density urban religion lifeexpf lifeexpm literacy
## 1   Afghanistan     20500    25.0    18   Muslim       44       45       29
## 2   Argentina       33900    12.0    86 Catholic       75       68       95
## 3   Armenia          3700   126.0    68 Orthodox       75       68       98
## 4   Australia       17800     2.3    85 Protstnt       80       74      100
## 5   Austria          8000    94.0    58 Catholic       79       73       99
## 6   Azerbaijan       7400    86.0    54   Muslim       75       67       98
## 7   Bahrain           600   828.0    83   Muslim       74       71       77
## 8   Bangladesh     125000   800.0    16   Muslim       53       53       35
## 9   Barbados          256   605.0    45 Protstnt       78       73       99
## 10  Belarus         10300    50.0    65 Orthodox       76       66       99
## 11  Belgium         10100   329.0    96 Catholic       79       73       99
## 12  Bolivia          7900     6.9    51 Catholic       64       59       78
## 13  Bosnia           4600    87.0    36   Muslim       78       72       86
## 14  Botswana         1359     2.4    25   Tribal       66       60       72
## 15  Brazil         156600    18.0    75 Catholic       67       57       81
## 16  Bulgaria         8900    79.0    68 Orthodox       75       69       93
## 17  Burkina Faso    10000    36.0    15  Animist       50       47       18
## 18  Burundi          6000   216.0     5 Catholic       50       46       50
## 19  Cambodia        10000    55.0    12 Buddhist       52       50       35
## 20  Cameroon        13100    27.0    40  Animist       58       55       54
## 21  Canada          29100     2.8    77 Catholic       81       74       97
## 22  Cent. Afri.R     3300     5.0    47 Protstnt       44       41       27
## 23  Chile           14000    18.0    85 Catholic       78       71       93
## 24  China         1205200   124.0    26   Taoist       69       67       78
## 25  Colombia        35600    31.0    70 Catholic       75       69       87
## 26  Costa Rica       3300    64.0    47 Catholic       79       76       93
## 27  Croatia          4900    85.0    51 Catholic       77       70       97
## 28  Cuba            11100    99.0    74 Catholic       78       74       94
## 29  Czech Rep.      10400   132.0    NA Catholic       77       69       NA
## 30  Denmark          5200   120.0    85 Protstnt       79       73       99
## 31  Domincan R.      7800   159.0    60 Catholic       70       66       83
## 32  Ecuador         10700    39.0    56 Catholic       73       67       88
## 33  Egypt           60000    57.0    44   Muslim       63       60       48
## 34  El Salvador      5800   246.0    44 Catholic       69       64       73
## 35  Estonia          1600    36.0    72 Protstnt       76       67       99
## 36  Ethiopia        55200    47.0    12   Muslim       54       51       24
## 37  Finland          5100    39.0    60 Protstnt       80       72      100
## 38  France          58000   105.0    73 Catholic       82       74       99
## 39  Gabon            1300     4.2    46 Catholic       58       52       61
## 40  Gambia            959    86.0    23   Muslim       52       48       27
## 41  Georgia          5500    81.0    56 Orthodox       76       69       99
## 42  Germany         81200   227.0    85 Protstnt       79       73       99
## 43  Greece          10400    80.0    63 Orthodox       80       75       93
## 44  Guatemala       10300    97.0    39 Catholic       67       62       55
## 45  Haiti            6500   231.0    29 Catholic       47       43       53
## 46  Honduras         5600    46.0    44 Catholic       70       65       73
## 47  Hong Kong        5800  5494.0    94 Buddhist       80       75       77
## 48  Hungary         10500   111.0    64 Catholic       76       67       99
## 49  Iceland           263     2.5    91 Protstnt       81       76      100
## 50  India          911600   283.0    26    Hindu       59       58       52
## 51  Indonesia      199700   102.0    29   Muslim       65       61       77
## 52  Iran            65600    39.0    57   Muslim       67       65       54
## 53  Iraq            19900    44.0    72   Muslim       68       65       60
## 54  Ireland          3600    51.0    57 Catholic       78       73       98
## 55  Israel           5400   238.0    92   Jewish       80       76       92
## 56  Italy           58100   188.0    69 Catholic       81       74       97
## 57  Japan          125500   330.0    77 Buddhist       82       76       99
## 58  Jordan           3961    42.0    68   Muslim       74       70       80
## 59  Kenya           28200    49.0    24 Catholic       55       51       69
## 60  Kuwait           1800    97.0    96   Muslim       78       73       73
## 61  Latvia           2700    40.0    71 Protstnt       75       64       99
## 62  Lebanon          3620   343.0    84   Muslim       71       67       80
## 63  Liberia          2900    29.0    45  Animist       57       54       40
## 64  Libya            5500     2.8    82   Muslim       65       62       64
## 65  Lithuania        3800    58.0    69 Catholic       77       68       99
## 66  Malaysia        19500    58.0    43   Muslim       72       66       78
## 67  Mexico          91800    46.0    73 Catholic       77       69       87
## 68  Morocco         28600    63.0    46   Muslim       70       66       50
## 69  N. Korea        23100   189.0    60 Buddhist       73       67       99
## 70  Netherlands     15400   366.0    89 Catholic       81       75       99
## 71  New Zealand      3524    13.0    84 Protstnt       80       73       99
## 72  Nicaragua        4100    33.0    60 Catholic       67       61       57
## 73  Nigeria         98100   102.0    35   Muslim       57       54       51
## 74  Norway           4300    11.0    75 Protstnt       81       74       99
## 75  Oman             1900     7.8    11   Muslim       70       66       NA
## 76  Pakistan       128100   143.0    32   Muslim       58       57       35
## 77  Panama           2600    34.0    53 Catholic       78       71       88
## 78  Paraguay         5200    11.0    48 Catholic       75       72       90
## 79  Peru            23650    18.0    70 Catholic       67       63       85
## 80  Philippines     69800   221.0    43 Catholic       68       63       90
## 81  Poland          38600   123.0    62 Catholic       77       69       99
## 82  Portugal        10500   108.0    34 Catholic       78       71       85
## 83  Romania         23400    96.0    54 Orthodox       75       69       96
## 84  Russia         149200     8.8    74 Orthodox       74       64       99
## 85  Rwanda           8400   311.0     6 Catholic       46       43       50
## 86  S. Korea        45000   447.0    72 Protstnt       74       68       96
## 87  Saudi Arabia    18000     7.7    77   Muslim       70       66       62
## 88  Senegal          8700    43.0    40   Muslim       58       55       38
## 89  Singapore        2900  4456.0   100   Taoist       79       73       88
## 90  Somalia          6667    10.0    24   Muslim       55       54       24
## 91  South Africa    43900    35.0    49     <NA>       68       62       76
## 92  Spain           39200    77.0    78 Catholic       81       74       95
## 93  Sweden           8800    19.0    84 Protstnt       81       75       99
## 94  Switzerland      7000   170.0    62 Catholic       82       75       99
## 95  Syria           14900    74.0    50   Muslim       68       65       64
## 96  Taiwan          20944   582.0    71 Buddhist       78       72       91
## 97  Tanzania        29800    29.0    21  Animist       45       41       46
## 98  Thailand        59400   115.0    22 Buddhist       72       65       93
## 99  Turkey          62200    79.0    61   Muslim       73       69       81
## 100 U.Arab Em.       2800    32.0    81   Muslim       74       70       68
## 101 Uganda          19800    76.0    11 Catholic       43       41       48
## 102 UK              58400   237.0    89 Protstnt       80       74       99
## 103 Ukraine         51800    87.0    67 Orthodox       75       65       97
## 104 Uruguay          3200    18.0    89 Catholic       77       71       96
## 105 USA            260800    26.0    75 Protstnt       79       73       97
## 106 Uzbekistan      22600    50.0    41   Muslim       72       65       97
## 107 Venezuela       20600    22.0    91 Catholic       76       70       88
## 108 Vietnam         73100   218.0    20 Buddhist       68       63       88
## 109 Zambia           9100    11.0    42 Protstnt       45       44       73
##     pop_incr babymort gdp_cap       region calories   aids birth_rt death_rt
## 1       2.80    168.0     205 Pacific/Asia       NA      0       53     22.0
## 2       1.30     25.6    3408 Latn America     3113   3904       20      9.0
## 3       1.40     27.0    5000  Middle East       NA      2       23      6.0
## 4       1.38      7.3   16848         OECD     3216   4727       15      8.0
## 5       0.20      6.7   18396         OECD     3495   1150       12     11.0
## 6       1.40     35.0    3000  Middle East       NA     NA       23      7.0
## 7       2.40     25.0    7875  Middle East       NA     13       29      4.0
## 8       2.40    106.0     202 Pacific/Asia     2021      1       35     11.0
## 9       0.21     20.3    6950 Latn America       NA    418       16      8.4
## 10      0.32     19.0    6500  East Europe       NA     10       13     11.0
## 11      0.20      7.2   17912         OECD       NA   1603       12     11.0
## 12      2.70     75.0     730 Latn America     1916     87       34      9.0
## 13      0.70     12.7    3098  East Europe       NA     NA       14      6.4
## 14      2.70     39.3    2677       Africa     2375   1415       32      8.0
## 15      1.28     66.0    2354 Latn America     2751  49312       21      9.0
## 16     -0.20     12.0    3831  East Europe       NA     24       13     12.0
## 17      2.81    118.0     357       Africa     2288   4193       47     18.0
## 18      2.26    105.0     208       Africa     1932   7225       44     21.0
## 19      2.90    112.0     260 Pacific/Asia     2166      0       45     16.0
## 20      2.90     77.0     993       Africa     2217   3072       41     12.0
## 21      0.70      6.8   19904         OECD     3482   9511       14      8.0
## 22      2.40    137.0     457       Africa     2036   3730       44     21.0
## 23      1.70     14.6    2591 Latn America     2581    831       23      6.0
## 24      1.10     52.0     377 Pacific/Asia     2639     38       21      7.0
## 25      2.00     28.0    1538 Latn America     2598   4583       24      6.0
## 26      2.30     11.0    2031 Latn America     2808    587       26      4.0
## 27     -0.10      8.7    5487  East Europe       NA     56       11     11.0
## 28      0.95     10.2    1382 Latn America       NA    245       17      7.0
## 29      0.21      9.3    7311  East Europe     3632     48       13     11.1
## 30      0.10      6.6   18277         OECD     3628   1411       12     12.0
## 31      1.80     51.5    1034 Latn America     2359   2353       25      6.0
## 32      2.01     39.0    1085 Latn America     2531    381       26      6.0
## 33      1.95     76.4     748  Middle East     3336     91       29      9.0
## 34      2.04     41.0    1078 Latn America     2317    530       33      7.0
## 35      0.52     19.0    6000  East Europe       NA      3       14     12.0
## 36      3.10    110.0     122       Africa     1667  12958       45     14.0
## 37      0.30      5.3   15877         OECD     3253    158       13     10.0
## 38      0.47      6.7   18944         OECD     3465  30003       13      9.3
## 39      1.46     94.0    4283       Africa     2383    472       28     14.0
## 40      3.10    124.0     351       Africa       NA    277       46     16.0
## 41      0.80     23.0    4500  East Europe       NA      2       16      9.0
## 42      0.36      6.5   17539         OECD     3443  11179       11     11.0
## 43      0.84      8.2    8060         OECD     3825    916       10     10.0
## 44      2.58     57.0    1342 Latn America     2235    499       35      8.0
## 45      1.63    109.0     383 Latn America     2013   4987       40     19.0
## 46      2.73     45.0    1030 Latn America     2247   3473       35      6.0
## 47     -0.09      5.8   14641 Pacific/Asia       NA     99       13      6.0
## 48     -0.30     12.5    5249  East Europe     3644    149       12     13.0
## 49      1.10      4.0   17241         OECD       NA     31       16      7.0
## 50      1.90     79.0     275 Pacific/Asia     2229    713       29     10.0
## 51      1.60     68.0     681 Pacific/Asia     2750     49       24      9.0
## 52      3.46     60.0    1500  Middle East     3181     92       42      8.0
## 53      3.70     67.0    1955  Middle East     2887     26       44      7.0
## 54      0.30      7.4   12170         OECD     3778    392       14      9.0
## 55      2.22      8.6   13066  Middle East       NA    279       21      7.0
## 56      0.21      7.6   17500         OECD     3504  21770       11     10.0
## 57      0.30      4.4   19860 Pacific/Asia     2956    713       11      7.0
## 58      3.30     34.0    1157  Middle East     2634     31       39      5.0
## 59      3.07     74.0     323       Africa     2163  30126       42     11.0
## 60      5.24     12.5    6818  Middle East     3195     10       28      2.0
## 61      0.50     21.5    7400  East Europe       NA      8       14     12.0
## 62      2.00     39.5    1429  Middle East       NA     72       27      7.0
## 63      3.30    113.0     409       Africa     2382    191       43     12.0
## 64      3.70     63.0    5910  Middle East     3324     10       45      8.0
## 65      0.30     17.0    6710  East Europe       NA      5       15     10.0
## 66      2.30     25.6    2995 Pacific/Asia     2774    107       29      5.0
## 67      1.90     35.0    3604 Latn America     3052  18353       28      5.0
## 68      2.12     50.0    1062       Africa       NA    196       29      6.0
## 69      1.83     27.7    1000 Pacific/Asia       NA      0       24      5.5
## 70      0.58      6.3   17245         OECD     3151   3055       13      9.0
## 71      0.57      8.9   14381         OECD     3362    431       16      8.0
## 72      2.68     52.5     447 Latn America     2265     66       35      7.0
## 73      3.10     75.0     282       Africa     2312   1148       44     12.0
## 74      0.40      6.3   17755         OECD     3326    375       13     10.0
## 75      3.46     36.7    7467  Middle East       NA     33       40      5.0
## 76      2.80    101.0     406 Pacific/Asia       NA     41       42     10.0
## 77      1.94     16.5    2397 Latn America     2539    644       25      5.0
## 78      2.70     25.2    1500 Latn America     2757     77       33      4.5
## 79      2.00     54.0    1107 Latn America     2186   1068       26      7.0
## 80      1.92     51.0     867 Pacific/Asia     2375    136       27      7.0
## 81      0.30     13.8    4429  East Europe       NA    201       14     10.0
## 82      0.36      9.2    9000         OECD       NA   1811       12     10.0
## 83      0.06     20.3    2702  East Europe     3155   2736       14     10.0
## 84      0.20     27.0    6680  East Europe       NA    136       13     11.0
## 85      2.80    117.0     292       Africa     1971  10706       49     21.0
## 86      1.00     21.7    6627 Pacific/Asia       NA     19       16      6.0
## 87      3.20     52.0    6651  Middle East     2874     61       38      6.0
## 88      3.10     76.0     744       Africa     2369    911       43     12.0
## 89      1.20      5.7   14990 Pacific/Asia     3198     75       16      6.0
## 90      3.20    126.0    2126       Africa     1906     13       46     13.0
## 91      2.60     47.1    3128       Africa       NA   3210       34      8.0
## 92      0.25      6.9   13047         OECD     3572  24202       11      9.0
## 93      0.52      5.7   16900         OECD     2960   1001       14     11.0
## 94      0.70      6.2   22384         OECD     3562   3662       12      9.0
## 95      3.70     43.0    2436  Middle East       NA     26       44      6.0
## 96      0.92      5.1    7055 Pacific/Asia       NA     NA       16       NA
## 97      2.50    110.0     263       Africa     2206  38719       46     19.0
## 98      1.40     37.0    1800 Pacific/Asia     2316   5654       19      6.0
## 99      2.02     49.0    3721  Middle East     3236    130       26      6.0
## 100     4.80     22.0   14193  Middle East       NA      8       28      3.0
## 101     2.42    112.0     325       Africa     2153  43875       49     24.0
## 102     0.20      7.2   15974         OECD     3149   9025       13     11.0
## 103     0.05     20.7    2340  East Europe       NA     27       12     13.0
## 104     0.80     17.0    3131 Latn America     2653    469       17     10.0
## 105     0.99      8.1   23474         OECD     3671 411907       15      9.0
## 106     2.13     53.0    1350  Middle East       NA      2       30      7.0
## 107     2.16     28.0    2829 Latn America     2582   3511       26      5.0
## 108     1.78     46.0     230 Pacific/Asia     2233    107       27      8.0
## 109     2.80     85.0     573       Africa     2077  29734       46     18.0
##     aids_rt log_gdp lg_aidsr b_to_d fertilty log_pop cropgrow lit_male lit_fema
## 1   0.0e+00     2.3     0.00   2.41      6.9     4.3       12       44       14
## 2   1.2e+01     3.5     1.63   2.22      2.8     4.5        9       96       95
## 3   5.4e-02     3.7     0.56   3.83      3.2     3.6       17      100      100
## 4   2.7e+01     4.2     1.93   1.88      1.9     4.3        6      100      100
## 5   1.4e+01     4.3     1.70   1.09      1.5     3.9       17       NA       NA
## 6        NA     3.5       NA   3.29      2.8     3.9       18      100      100
## 7   2.2e+00     3.9     1.17   7.25      4.0     2.8        2       55       55
## 8   8.6e-04     2.3     0.24   3.18      4.7     5.1       67       47       22
## 9   1.4e+02     3.8     2.68   1.90      1.8     2.4       77       99       99
## 10  9.7e-02     3.8     0.63   1.18      1.9     4.0       29      100      100
## 11  1.6e+01     4.3     1.74   1.09      1.7     4.0       24       NA       NA
## 12  1.1e+00     2.9     1.02   3.78      4.2     3.9        3       85       71
## 13       NA     3.5       NA   2.19       NA     3.7       20       NA       NA
## 14  1.0e+02     3.4     2.52   4.00      5.1     3.1        2       32       16
## 15  3.1e+01     3.4     1.99   2.33      2.7     5.2        7       82       80
## 16  2.7e-01     3.6     0.77   1.08      1.8     3.9       34       NA       NA
## 17  4.2e+01     2.6     2.11   2.61      6.9     4.0       10       28        9
## 18  1.2e+02     2.3     2.61   2.10      6.8     3.8       43       61       40
## 19  0.0e+00     2.4     0.00   2.81      5.8     4.0       16       48       22
## 20  2.3e+01     3.0     1.88   3.42      5.7     4.1       13       66       45
## 21  3.3e+01     4.3     2.01   1.75      1.8     4.5        5       NA       NA
## 22  1.1e+02     2.7     2.57   2.10      5.4     3.5        3       33       15
## 23  5.9e+00     3.4     1.43   3.83      2.5     4.1        7       94       93
## 24  3.2e-03     2.6     0.32   3.00      1.8     6.1       10       87       68
## 25  1.3e+01     3.2     1.67   4.00      2.5     4.6        4       88       86
## 26  1.8e+01     3.3     1.78   6.50      3.1     3.5        6       93       93
## 27  1.1e+00     3.7     1.03   1.00      1.6     3.7       32       NA       NA
## 28  2.2e+00     3.1     1.17   2.43      1.9     4.0       23       95       93
## 29  4.6e-01     3.9     0.86   1.17      1.8     4.0       NA       NA       NA
## 30  2.7e+01     4.3     1.94   1.00      1.7     3.7       61       NA       NA
## 31  3.0e+01     3.0     1.98   4.17      2.8     3.9       23       85       82
## 32  3.4e+00     3.0     1.28   4.33      3.1     4.0        6       90       86
## 33  1.5e-01     2.9     0.69   3.22      3.8     4.8        3       63       34
## 34  9.6e+00     3.0     1.57   4.71      3.8     3.8       27       76       70
## 35  1.9e-01     3.8     0.72   1.17      2.0     3.2       22      100      100
## 36  2.3e+01     2.1     1.88   3.21      6.8     4.7       12       32       16
## 37  3.1e+00     4.2     1.25   1.30      1.8     3.7        8       NA       NA
## 38  5.2e+01     4.3     2.20   1.40      1.8     4.8       32       NA       NA
## 39  3.6e+01     3.6     2.05   2.00      4.0     3.1        1       74       48
## 40  2.5e+01     2.5     1.91   2.88      6.3     3.0       16       39       16
## 41  3.6e-02     3.7     0.52   1.78      2.2     3.7       NA      100      100
## 42  1.4e+01     4.2     1.69   1.00      1.5     4.9       34       NA       NA
## 43  8.8e+00     3.9     1.55   1.00      1.5     4.0       23       98       89
## 44  4.8e+00     3.1     1.37   4.38      4.8     4.0       12       63       47
## 45  7.1e+01     2.6     2.35   2.11      5.9     3.8       20       59       47
## 46  6.2e+01     3.0     2.28   5.83      4.9     3.7       14       76       71
## 47  1.7e+00     4.2     1.11   2.17      1.4     3.8        7       90       64
## 48  1.4e+00     3.7     1.07   0.92      1.8     4.0       51       99       98
## 49  1.0e+01     4.2     1.60   2.29      2.1     2.4        1       NA       NA
## 50  7.8e-02     2.4     0.60   2.90      4.5     6.0       55       64       39
## 51  2.5e-02     2.8     0.48   2.67      2.8     5.3        8       84       68
## 52  1.5e-01     3.2     0.68   5.25      6.3     4.8        8       64       43
## 53  1.3e-01     3.3     0.67   6.29      6.7     4.3       12       70       49
## 54  1.1e+01     4.1     1.61   1.56      2.0     3.6       14       NA       NA
## 55  5.2e+00     4.1     1.39   3.00      2.8     3.7       17       95       89
## 56  3.8e+01     4.2     2.07   1.10      1.3     4.8       32       98       96
## 57  5.7e-01     4.3     0.89   1.57      1.6     5.1       13       NA       NA
## 58  7.0e-01     3.1     0.93   7.80      5.6     3.6        4       89       70
## 59  1.1e+02     2.5     2.57   3.82      5.9     4.5        3       80       58
## 60  5.6e-01     3.8     0.89  14.00      4.0     3.3        0       77       67
## 61  3.0e-01     3.9     0.78   1.17      2.0     3.4       27      100      100
## 62  2.5e+00     3.2     1.20   3.86      3.4     3.6       21       88       73
## 63  6.6e+00     2.6     1.46   3.58      6.8     3.5        1       50       29
## 64  1.8e-01     3.8     0.71   5.62      6.4     3.7        2       75       50
## 65  1.3e-01     3.8     0.67   1.50      2.0     3.6       49       99       98
## 66  5.5e-01     3.5     0.89   5.80      3.5     4.3        3       86       70
## 67  2.0e+01     3.6     1.82   5.60      3.2     5.0       12       90       85
## 68  6.9e-01     3.0     0.93   4.83      3.8     4.5       18       61       38
## 69  0.0e+00     3.0     0.00   4.36      2.4     4.4       18       99       99
## 70  2.0e+01     4.2     1.82   1.44      1.6     4.2       26       NA       NA
## 71  1.2e+01     4.2     1.65   2.00      2.0     3.5        2       NA       NA
## 72  1.5e+00     2.7     1.09   5.00      4.3     3.6        9       57       57
## 73  9.6e-01     2.5     0.99   3.67      6.4     5.0       31       62       40
## 74  8.7e+00     4.2     1.54   1.30      2.0     3.6        3       NA       NA
## 75  1.7e+00     3.9     1.12   8.00      6.5     3.3        2       NA       NA
## 76  3.2e-02     2.6     0.50   4.20      6.4     5.1       26       47       21
## 77  2.5e+01     3.4     1.90   5.00      2.9     3.4        6       88       88
## 78  1.6e+00     3.2     1.10   7.33      4.3     3.7       20       92       88
## 79  4.7e+00     3.0     1.36   3.71      3.1     4.4        3       92       79
## 80  2.0e-01     2.9     0.72   3.86      3.4     4.8       26       90       90
## 81  5.2e-01     3.6     0.88   1.40      1.9     4.6       46       99       98
## 82  1.8e+01     4.0     1.79   1.20      1.5     4.0       32       89       82
## 83  1.2e+01     3.4     1.64   1.40      1.8     4.4       43       NA       NA
## 84  9.1e-02     3.8     0.62   1.18      1.8     5.2        8      100      100
## 85  1.4e+02     2.5     2.68   2.33      8.2     3.9       29       64       37
## 86  4.3e-02     3.8     0.53   2.67      1.6     4.7       21       99       99
## 87  3.4e-01     3.8     0.81   6.33      6.7     4.3        1       73       48
## 88  1.1e+01     2.9     1.62   3.58      6.1     3.9       27       52       25
## 89  2.6e+00     4.2     1.21   2.67      1.9     3.5        4       93       84
## 90  1.3e-01     3.3     0.67   3.54      7.2     3.8        2       36       14
## 91  7.8e+00     3.5     1.51   4.25      4.4     4.6       10       NA       NA
## 92  6.2e+01     4.1     2.28   1.22      1.4     4.6       31       97       93
## 93  1.1e+01     4.2     1.63   1.27      2.1     3.9        7       NA       NA
## 94  5.2e+01     4.3     2.21   1.33      1.6     3.8       10       NA       NA
## 95  1.9e-01     3.4     0.71   7.33      6.6     4.2       28       78       51
## 96       NA     3.8       NA     NA       NA     4.3       NA       NA       NA
## 97  1.3e+02     2.4     2.65   2.42      6.2     4.5        5       62       31
## 98  9.5e+00     3.3     1.57   3.17      2.1     4.8       34       96       90
## 99  2.1e-01     3.6     0.73   4.33      3.2     4.8       30       90       71
## 100 4.7e-01     4.2     0.86   9.33      4.5     3.4        0       70       63
## 101 2.2e+02     2.5     2.95   2.04      6.8     4.3       23       62       35
## 102 1.5e+01     4.2     1.73   1.18      1.8     4.8       29       NA       NA
## 103 5.2e-02     3.4     0.55   0.92      1.8     4.7       56      100      100
## 104 1.5e+01     3.5     1.71   1.70      2.4     3.5        8       97       96
## 105 1.6e+02     4.4     2.75   1.67      2.1     5.4       20       97       97
## 106 9.0e-03     3.1     0.39   4.29      3.7     4.4       10      100      100
## 107 1.6e+01     3.5     1.75   5.20      3.0     4.3        3       90       87
## 108 1.5e-01     2.4     0.68   3.38      3.3     4.9       22       93       83
## 109 3.3e+02     2.8     3.18   2.56      6.7     4.0        7       81       65
##           climate
## 1            arid
## 2       temperate
## 3            <NA>
## 4            arid
## 5       temperate
## 6            arid
## 7            arid
## 8        tropical
## 9        tropical
## 10      temperate
## 11      temperate
## 12              4
## 13      temperate
## 14              4
## 15       tropical
## 16      temperate
## 17       tropical
## 18      temperate
## 19       tropical
## 20              4
## 21  arctic / temp
## 22       tropical
## 23      temperate
## 24      temperate
## 25       tropical
## 26       tropical
## 27  mediterranean
## 28       tropical
## 29      temperate
## 30      temperate
## 31       tropical
## 32       tropical
## 33         desert
## 34       tropical
## 35       maritime
## 36       tropical
## 37  arctic / temp
## 38      temperate
## 39       tropical
## 40       tropical
## 41  mediterranean
## 42      temperate
## 43      temperate
## 44       tropical
## 45       tropical
## 46              4
## 47       tropical
## 48      temperate
## 49      temperate
## 50  mediterranean
## 51       tropical
## 52  arid / desert
## 53         desert
## 54      temperate
## 55      temperate
## 56  mediterranean
## 57  mediterranean
## 58  arid / desert
## 59              4
## 60  arid / desert
## 61       maritime
## 62  mediterranean
## 63       tropical
## 64  mediterranean
## 65       maritime
## 66       tropical
## 67           arid
## 68  mediterranean
## 69      temperate
## 70      temperate
## 71      temperate
## 72       tropical
## 73       tropical
## 74      temperate
## 75         desert
## 76      temperate
## 77       tropical
## 78  mediterranean
## 79           arid
## 80       tropical
## 81      temperate
## 82       maritime
## 83      temperate
## 84  arctic / temp
## 85      temperate
## 86      temperate
## 87         desert
## 88       tropical
## 89       tropical
## 90         desert
## 91  arid / desert
## 92      temperate
## 93  arctic / temp
## 94      temperate
## 95         desert
## 96           <NA>
## 97  mediterranean
## 98       tropical
## 99      temperate
## 100        desert
## 101      tropical
## 102     temperate
## 103     temperate
## 104     temperate
## 105     temperate
## 106 arid / desert
## 107      tropical
## 108      tropical
## 109      tropical
2b.i
summary(breaks$breaks)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##      14      28      45      46      57      96
2b.ii
cor(bpwide$bp_before,bpwide$bp_after)
## [1] 0.16
lm(bpwide$bp_before~bpwide$bp_after)
## 
## Call:
## lm(formula = bpwide$bp_before ~ bpwide$bp_after)
## 
## Coefficients:
##     (Intercept)  bpwide$bp_after  
##         137.102            0.128
alternative
# Fit a linear regression model of bp_before in terms of bp_after
model <- lm(bp_before ~ bp_after, data = bpwide)

# Print the model summary
summary(model)
## 
## Call:
## lm(formula = bp_before ~ bp_after, data = bpwide)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -18.50  -9.13  -1.49   7.62  30.00 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  137.102     11.099   12.35   <2e-16 ***
## bp_after       0.128      0.073    1.75    0.083 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 11 on 118 degrees of freedom
## Multiple R-squared:  0.0253, Adjusted R-squared:  0.0171 
## F-statistic: 3.07 on 1 and 118 DF,  p-value: 0.0826
2b.iii
max(world95$density)
## [1] 5494
min(world95$density)
## [1] 2.3
world95$country[world95$density==5494]
## [1] "Hong Kong   "
world95$country[world95$density==2.3]
## [1] "Australia   "

3

3a

swiss
##              Fertility Agriculture Examination Education Catholic
## Courtelary          80        17.0          15        12     10.0
## Delemont            83        45.1           6         9     84.8
## Franches-Mnt        92        39.7           5         5     93.4
## Moutier             86        36.5          12         7     33.8
## Neuveville          77        43.5          17        15      5.2
## Porrentruy          76        35.3           9         7     90.6
## Broye               84        70.2          16         7     92.8
## Glane               92        67.8          14         8     97.2
## Gruyere             82        53.3          12         7     97.7
## Sarine              83        45.2          16        13     91.4
## Veveyse             87        64.5          14         6     98.6
## Aigle               64        62.0          21        12      8.5
## Aubonne             67        67.5          14         7      2.3
## Avenches            69        60.7          19        12      4.4
## Cossonay            62        69.3          22         5      2.8
## Echallens           68        72.6          18         2     24.2
## Grandson            72        34.0          17         8      3.3
## Lausanne            56        19.4          26        28     12.1
## La Vallee           54        15.2          31        20      2.1
## Lavaux              65        73.0          19         9      2.8
## Morges              66        59.8          22        10      5.2
## Moudon              65        55.1          14         3      4.5
## Nyone               57        50.9          22        12     15.1
## Orbe                57        54.1          20         6      4.2
## Oron                72        71.2          12         1      2.4
## Payerne             74        58.1          14         8      5.2
## Paysd'enhaut        72        63.5           6         3      2.6
## Rolle               60        60.8          16        10      7.7
## Vevey               58        26.8          25        19     18.5
## Yverdon             65        49.5          15         8      6.1
## Conthey             76        85.9           3         2     99.7
## Entremont           69        84.9           7         6     99.7
## Herens              77        89.7           5         2    100.0
## Martigwy            70        78.2          12         6     99.0
## Monthey             79        64.9           7         3     98.2
## St Maurice          65        75.9           9         9     99.1
## Sierre              92        84.6           3         3     99.5
## Sion                79        63.1          13        13     96.8
## Boudry              70        38.4          26        12      5.6
## La Chauxdfnd        66         7.7          29        11     13.8
## Le Locle            73        16.7          22        13     11.2
## Neuchatel           64        17.6          35        32     16.9
## Val de Ruz          78        37.6          15         7      5.0
## ValdeTravers        68        18.7          25         7      8.6
## V. De Geneve        35         1.2          37        53     42.3
## Rive Droite         45        46.6          16        29     50.4
## Rive Gauche         43        27.7          22        29     58.3
##              Infant.Mortality
## Courtelary                 22
## Delemont                   22
## Franches-Mnt               20
## Moutier                    20
## Neuveville                 21
## Porrentruy                 27
## Broye                      24
## Glane                      25
## Gruyere                    21
## Sarine                     24
## Veveyse                    24
## Aigle                      16
## Aubonne                    19
## Avenches                   23
## Cossonay                   19
## Echallens                  21
## Grandson                   20
## Lausanne                   20
## La Vallee                  11
## Lavaux                     20
## Morges                     18
## Moudon                     22
## Nyone                      17
## Orbe                       15
## Oron                       21
## Payerne                    24
## Paysd'enhaut               18
## Rolle                      16
## Vevey                      21
## Yverdon                    22
## Conthey                    15
## Entremont                  20
## Herens                     18
## Martigwy                   19
## Monthey                    20
## St Maurice                 18
## Sierre                     16
## Sion                       18
## Boudry                     20
## La Chauxdfnd               20
## Le Locle                   19
## Neuchatel                  23
## Val de Ruz                 20
## ValdeTravers               20
## V. De Geneve               18
## Rive Droite                18
## Rive Gauche                19
boxplot(swiss$Education)

boxplot(swiss$Infant.Mortality)

3b

set.seed(235)
samp<-swiss[sample(x = ncol(swiss),size = 5,replace = F),]
samp
##              Fertility Agriculture Examination Education Catholic
## Porrentruy          76          35           9         7     90.6
## Neuveville          77          44          17        15      5.2
## Courtelary          80          17          15        12     10.0
## Delemont            83          45           6         9     84.8
## Franches-Mnt        92          40           5         5     93.4
##              Infant.Mortality
## Porrentruy                 27
## Neuveville                 21
## Courtelary                 22
## Delemont                   22
## Franches-Mnt               20

3c

sub1<-swiss[swiss$Education>=20 &swiss$Agriculture<40,]
sub1
##              Fertility Agriculture Examination Education Catholic
## Lausanne            56        19.4          26        28     12.1
## La Vallee           54        15.2          31        20      2.1
## Neuchatel           64        17.6          35        32     16.9
## V. De Geneve        35         1.2          37        53     42.3
## Rive Gauche         43        27.7          22        29     58.3
##              Infant.Mortality
## Lausanne                   20
## La Vallee                  11
## Neuchatel                  23
## V. De Geneve               18
## Rive Gauche                19

3d

4

4a

4b

pdf<-function(x,p){
  return((1-p)^(x-1)*p)
}

x<-c(3,5,7)
pdf(x,0.4)
## [1] 0.144 0.052 0.019
cdf<-function(x,p){
  return(1-(1-p)^x)
}
cdf(5,.4)
## [1] 0.92

5

5a

# Load airquality dataset
data(airquality)

# Create boxplots for Temp against Month
boxplot(Temp ~ Month, data = airquality, 
        xlab = "Month", ylab = "Temperature",
        main = "Temperature vs Month Boxplot")

alternative
airquality
##     Ozone Solar.R Wind Temp Month Day
## 1      41     190  7.4   67     5   1
## 2      36     118  8.0   72     5   2
## 3      12     149 12.6   74     5   3
## 4      18     313 11.5   62     5   4
## 5      NA      NA 14.3   56     5   5
## 6      28      NA 14.9   66     5   6
## 7      23     299  8.6   65     5   7
## 8      19      99 13.8   59     5   8
## 9       8      19 20.1   61     5   9
## 10     NA     194  8.6   69     5  10
## 11      7      NA  6.9   74     5  11
## 12     16     256  9.7   69     5  12
## 13     11     290  9.2   66     5  13
## 14     14     274 10.9   68     5  14
## 15     18      65 13.2   58     5  15
## 16     14     334 11.5   64     5  16
## 17     34     307 12.0   66     5  17
## 18      6      78 18.4   57     5  18
## 19     30     322 11.5   68     5  19
## 20     11      44  9.7   62     5  20
## 21      1       8  9.7   59     5  21
## 22     11     320 16.6   73     5  22
## 23      4      25  9.7   61     5  23
## 24     32      92 12.0   61     5  24
## 25     NA      66 16.6   57     5  25
## 26     NA     266 14.9   58     5  26
## 27     NA      NA  8.0   57     5  27
## 28     23      13 12.0   67     5  28
## 29     45     252 14.9   81     5  29
## 30    115     223  5.7   79     5  30
## 31     37     279  7.4   76     5  31
## 32     NA     286  8.6   78     6   1
## 33     NA     287  9.7   74     6   2
## 34     NA     242 16.1   67     6   3
## 35     NA     186  9.2   84     6   4
## 36     NA     220  8.6   85     6   5
## 37     NA     264 14.3   79     6   6
## 38     29     127  9.7   82     6   7
## 39     NA     273  6.9   87     6   8
## 40     71     291 13.8   90     6   9
## 41     39     323 11.5   87     6  10
## 42     NA     259 10.9   93     6  11
## 43     NA     250  9.2   92     6  12
## 44     23     148  8.0   82     6  13
## 45     NA     332 13.8   80     6  14
## 46     NA     322 11.5   79     6  15
## 47     21     191 14.9   77     6  16
## 48     37     284 20.7   72     6  17
## 49     20      37  9.2   65     6  18
## 50     12     120 11.5   73     6  19
## 51     13     137 10.3   76     6  20
## 52     NA     150  6.3   77     6  21
## 53     NA      59  1.7   76     6  22
## 54     NA      91  4.6   76     6  23
## 55     NA     250  6.3   76     6  24
## 56     NA     135  8.0   75     6  25
## 57     NA     127  8.0   78     6  26
## 58     NA      47 10.3   73     6  27
## 59     NA      98 11.5   80     6  28
## 60     NA      31 14.9   77     6  29
## 61     NA     138  8.0   83     6  30
## 62    135     269  4.1   84     7   1
## 63     49     248  9.2   85     7   2
## 64     32     236  9.2   81     7   3
## 65     NA     101 10.9   84     7   4
## 66     64     175  4.6   83     7   5
## 67     40     314 10.9   83     7   6
## 68     77     276  5.1   88     7   7
## 69     97     267  6.3   92     7   8
## 70     97     272  5.7   92     7   9
## 71     85     175  7.4   89     7  10
## 72     NA     139  8.6   82     7  11
## 73     10     264 14.3   73     7  12
## 74     27     175 14.9   81     7  13
## 75     NA     291 14.9   91     7  14
## 76      7      48 14.3   80     7  15
## 77     48     260  6.9   81     7  16
## 78     35     274 10.3   82     7  17
## 79     61     285  6.3   84     7  18
## 80     79     187  5.1   87     7  19
## 81     63     220 11.5   85     7  20
## 82     16       7  6.9   74     7  21
## 83     NA     258  9.7   81     7  22
## 84     NA     295 11.5   82     7  23
## 85     80     294  8.6   86     7  24
## 86    108     223  8.0   85     7  25
## 87     20      81  8.6   82     7  26
## 88     52      82 12.0   86     7  27
## 89     82     213  7.4   88     7  28
## 90     50     275  7.4   86     7  29
## 91     64     253  7.4   83     7  30
## 92     59     254  9.2   81     7  31
## 93     39      83  6.9   81     8   1
## 94      9      24 13.8   81     8   2
## 95     16      77  7.4   82     8   3
## 96     78      NA  6.9   86     8   4
## 97     35      NA  7.4   85     8   5
## 98     66      NA  4.6   87     8   6
## 99    122     255  4.0   89     8   7
## 100    89     229 10.3   90     8   8
## 101   110     207  8.0   90     8   9
## 102    NA     222  8.6   92     8  10
## 103    NA     137 11.5   86     8  11
## 104    44     192 11.5   86     8  12
## 105    28     273 11.5   82     8  13
## 106    65     157  9.7   80     8  14
## 107    NA      64 11.5   79     8  15
## 108    22      71 10.3   77     8  16
## 109    59      51  6.3   79     8  17
## 110    23     115  7.4   76     8  18
## 111    31     244 10.9   78     8  19
## 112    44     190 10.3   78     8  20
## 113    21     259 15.5   77     8  21
## 114     9      36 14.3   72     8  22
## 115    NA     255 12.6   75     8  23
## 116    45     212  9.7   79     8  24
## 117   168     238  3.4   81     8  25
## 118    73     215  8.0   86     8  26
## 119    NA     153  5.7   88     8  27
## 120    76     203  9.7   97     8  28
## 121   118     225  2.3   94     8  29
## 122    84     237  6.3   96     8  30
## 123    85     188  6.3   94     8  31
## 124    96     167  6.9   91     9   1
## 125    78     197  5.1   92     9   2
## 126    73     183  2.8   93     9   3
## 127    91     189  4.6   93     9   4
## 128    47      95  7.4   87     9   5
## 129    32      92 15.5   84     9   6
## 130    20     252 10.9   80     9   7
## 131    23     220 10.3   78     9   8
## 132    21     230 10.9   75     9   9
## 133    24     259  9.7   73     9  10
## 134    44     236 14.9   81     9  11
## 135    21     259 15.5   76     9  12
## 136    28     238  6.3   77     9  13
## 137     9      24 10.9   71     9  14
## 138    13     112 11.5   71     9  15
## 139    46     237  6.9   78     9  16
## 140    18     224 13.8   67     9  17
## 141    13      27 10.3   76     9  18
## 142    24     238 10.3   68     9  19
## 143    16     201  8.0   82     9  20
## 144    13     238 12.6   64     9  21
## 145    23      14  9.2   71     9  22
## 146    36     139 10.3   81     9  23
## 147     7      49 10.3   69     9  24
## 148    14      20 16.6   63     9  25
## 149    30     193  6.9   70     9  26
## 150    NA     145 13.2   77     9  27
## 151    14     191 14.3   75     9  28
## 152    18     131  8.0   76     9  29
## 153    20     223 11.5   68     9  30
library(ggplot2)
ggplot()+
  geom_boxplot(mapping = aes(x = factor(Month),y = Temp),data = airquality,)

5b

1st way
morley
##     Expt Run Speed
## 001    1   1   850
## 002    1   2   740
## 003    1   3   900
## 004    1   4  1070
## 005    1   5   930
## 006    1   6   850
## 007    1   7   950
## 008    1   8   980
## 009    1   9   980
## 010    1  10   880
## 011    1  11  1000
## 012    1  12   980
## 013    1  13   930
## 014    1  14   650
## 015    1  15   760
## 016    1  16   810
## 017    1  17  1000
## 018    1  18  1000
## 019    1  19   960
## 020    1  20   960
## 021    2   1   960
## 022    2   2   940
## 023    2   3   960
## 024    2   4   940
## 025    2   5   880
## 026    2   6   800
## 027    2   7   850
## 028    2   8   880
## 029    2   9   900
## 030    2  10   840
## 031    2  11   830
## 032    2  12   790
## 033    2  13   810
## 034    2  14   880
## 035    2  15   880
## 036    2  16   830
## 037    2  17   800
## 038    2  18   790
## 039    2  19   760
## 040    2  20   800
## 041    3   1   880
## 042    3   2   880
## 043    3   3   880
## 044    3   4   860
## 045    3   5   720
## 046    3   6   720
## 047    3   7   620
## 048    3   8   860
## 049    3   9   970
## 050    3  10   950
## 051    3  11   880
## 052    3  12   910
## 053    3  13   850
## 054    3  14   870
## 055    3  15   840
## 056    3  16   840
## 057    3  17   850
## 058    3  18   840
## 059    3  19   840
## 060    3  20   840
## 061    4   1   890
## 062    4   2   810
## 063    4   3   810
## 064    4   4   820
## 065    4   5   800
## 066    4   6   770
## 067    4   7   760
## 068    4   8   740
## 069    4   9   750
## 070    4  10   760
## 071    4  11   910
## 072    4  12   920
## 073    4  13   890
## 074    4  14   860
## 075    4  15   880
## 076    4  16   720
## 077    4  17   840
## 078    4  18   850
## 079    4  19   850
## 080    4  20   780
## 081    5   1   890
## 082    5   2   840
## 083    5   3   780
## 084    5   4   810
## 085    5   5   760
## 086    5   6   810
## 087    5   7   790
## 088    5   8   810
## 089    5   9   820
## 090    5  10   850
## 091    5  11   870
## 092    5  12   870
## 093    5  13   810
## 094    5  14   740
## 095    5  15   810
## 096    5  16   940
## 097    5  17   950
## 098    5  18   800
## 099    5  19   810
## 100    5  20   870
library(tidyverse)

ggplot()+
  geom_point(mapping = aes(x = factor(Run),y = factor(Speed),color= factor(Expt) ),data = morley)

2nd way(eassy)
ggplot(data = morley,mapping = aes(Run, Speed))+
  geom_point()

3rd way
# Scatter plot of Speed against Run
ggplot(morley, aes(Run, Speed)) + 
  geom_point() +
  labs(x = "Run", y = "Speed (km/sec)") +
  ggtitle("Speed vs Run")

1st way (eassy)
ggplot(data = morley,mapping = aes(Run, Speed))+
  geom_point()+
  geom_smooth()
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

2nd way
# Scatter plot of Speed against Run with trend line
ggplot(morley, aes(Run, Speed)) + 
  geom_point() +
  geom_smooth( se = FALSE, color = "red") +
  labs(x = "Run", y = "Speed (km/sec)") +
  ggtitle("Speed vs Run")
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

# Box plot of Speed against Expt
ggplot(morley, aes(factor(Expt), Speed)) + 
  geom_boxplot() +
  labs(x = "Experiment", y = "Speed (km/sec)") +
  ggtitle("Speed vs Experiment")