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] -1.09331727  0.72008413  0.18097488  0.36768992 -0.06341894

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:10844/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.009998 0.281972 0.441006 0.490320 0.707990 0.991474
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
## 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
## 3         3  10/1/08      UK      F  25  3  5  5  5  2  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
## 80   Mc3 Mississippi    chilled  250   17.9
## 17   Qn3      Quebec nonchilled  250   40.3
## 15   Qn3      Quebec nonchilled   95   16.2
## 29   Qc2      Quebec    chilled   95    9.3
## 70   Mc1 Mississippi    chilled 1000   21.9
## 65   Mc1 Mississippi    chilled  175   14.9
## 16   Qn3      Quebec nonchilled  175   32.4
## 23   Qc1      Quebec    chilled  175   24.1
## 50   Mn2 Mississippi nonchilled   95   12.0
## 60   Mn3 Mississippi nonchilled  350   27.9

ex-3 coloum sample

mysample3 <- CO2[,sample(1:ncol(CO2), 2,
replace=FALSE)]
mysample3
##     Treatment conc
## 1  nonchilled   95
## 2  nonchilled  175
## 3  nonchilled  250
## 4  nonchilled  350
## 5  nonchilled  500
## 6  nonchilled  675
## 7  nonchilled 1000
## 8  nonchilled   95
## 9  nonchilled  175
## 10 nonchilled  250
## 11 nonchilled  350
## 12 nonchilled  500
## 13 nonchilled  675
## 14 nonchilled 1000
## 15 nonchilled   95
## 16 nonchilled  175
## 17 nonchilled  250
## 18 nonchilled  350
## 19 nonchilled  500
## 20 nonchilled  675
## 21 nonchilled 1000
## 22    chilled   95
## 23    chilled  175
## 24    chilled  250
## 25    chilled  350
## 26    chilled  500
## 27    chilled  675
## 28    chilled 1000
## 29    chilled   95
## 30    chilled  175
## 31    chilled  250
## 32    chilled  350
## 33    chilled  500
## 34    chilled  675
## 35    chilled 1000
## 36    chilled   95
## 37    chilled  175
## 38    chilled  250
## 39    chilled  350
## 40    chilled  500
## 41    chilled  675
## 42    chilled 1000
## 43 nonchilled   95
## 44 nonchilled  175
## 45 nonchilled  250
## 46 nonchilled  350
## 47 nonchilled  500
## 48 nonchilled  675
## 49 nonchilled 1000
## 50 nonchilled   95
## 51 nonchilled  175
## 52 nonchilled  250
## 53 nonchilled  350
## 54 nonchilled  500
## 55 nonchilled  675
## 56 nonchilled 1000
## 57 nonchilled   95
## 58 nonchilled  175
## 59 nonchilled  250
## 60 nonchilled  350
## 61 nonchilled  500
## 62 nonchilled  675
## 63 nonchilled 1000
## 64    chilled   95
## 65    chilled  175
## 66    chilled  250
## 67    chilled  350
## 68    chilled  500
## 69    chilled  675
## 70    chilled 1000
## 71    chilled   95
## 72    chilled  175
## 73    chilled  250
## 74    chilled  350
## 75    chilled  500
## 76    chilled  675
## 77    chilled 1000
## 78    chilled   95
## 79    chilled  175
## 80    chilled  250
## 81    chilled  350
## 82    chilled  500
## 83    chilled  675
## 84    chilled 1000

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)
#Aggregation
#cast(md, ID~variable, mean)
#cast(md, Semester~variable, mean)
#cast(md, ID~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