Exercises

pacman::p_load(Epi, 
               tidyverse)

Exercises Using R

  1. Calculate \(\sqrt{3^{2}+4^{2}}\)
sqrt(3^2 + 4^2)
[1] 5
  1. Find the probability above 4.3 in chi-squared distribution on 1 degree of freedom
pchisq(4.3, 1)
[1] 0.9618876

Functions

p2o <- function(p) p/(1-p)
o2p <- function(o) o/(1+o)
p2o(0.25)
[1] 0.3333333
o2p(8)
[1] 0.8888889
o2p(p2o(0.25))
[1] 0.25

Write a function

p2o <- 
  function( p )
  {
    odds <- p / (1 - p)
    odds
  }
p2o(2)
[1] -2

Sequences

seq(15, 85, by = 5)
 [1] 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85
c(5, seq(20, 85, by = 5))
 [1]  5 20 25 30 35 40 45 50 55 60 65 70 75 80 85
3.7:8.1
[1] 3.7 4.7 5.7 6.7 7.7

Vectors

  1. create a vector w with components 1, -1, 2, -2
w <- c(1, -1, 2, -2)
  1. print the w vector
w
[1]  1 -1  2 -2
  1. obtain a description of w
str(w)
 num [1:4] 1 -1 2 -2
  1. create the vector w+1 and print it
w+1
[1]  2  0  3 -1
  1. create the vector (0, 1, 5, 10, 15…75) using c() and seq()
c(0, 1, seq(5,75, by = 5))
 [1]  0  1  5 10 15 20 25 30 35 40 45 50 55 60 65 70 75
  1. create a vector with 20 elements equally spaced between 7 and 23
c(20, seq(7, 23))
 [1] 20  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

Births data

data(births)
?births
str(births)
'data.frame':   500 obs. of  8 variables:
 $ id     : num  1 2 3 4 5 6 7 8 9 10 ...
 $ bweight: num  2974 3270 2620 3751 3200 ...
 $ lowbw  : num  0 0 0 0 0 0 0 0 0 0 ...
 $ gestwks: num  38.5 NA 38.2 39.8 38.9 ...
 $ preterm: num  0 NA 0 0 0 0 0 0 0 0 ...
 $ matage : num  34 30 35 31 33 33 29 37 36 39 ...
 $ hyp    : num  0 0 0 0 1 0 0 0 0 0 ...
 $ sex    : num  2 1 2 1 1 2 2 1 2 1 ...

Selects

data(diet)
births[1:10, "bweight"]
 [1] 2974 3270 2620 3751 3200 3673 3628 3773 3960 3405
  1. print the data on the variable gestwks for subject 7 in births dataframe
births[7, "gestwks"]
[1] 42.14
  1. print all the data for subject #7

    births[7, ]
      id bweight lowbw gestwks preterm matage hyp sex
    7  7    3628     0   42.14       0     29   0   2
  2. Print all the data on the variable gestwks

    births[, "gestwks"]
      [1] 38.52    NA 38.15 39.80 38.89 40.97 42.14 40.21 42.03 39.33 40.92 38.97
     [13] 41.02 40.05 39.23 38.96 39.35 38.99 40.74 39.45 39.27 32.80 38.72 40.19
     [25] 40.14 39.55 39.38 36.53 41.66 40.45 39.71 39.18 38.51 36.40 40.46 39.47
     [37] 39.12 40.94 39.35 38.80 40.67 37.87 38.51 39.04 40.36 37.44 39.58 35.70
     [49] 39.19 40.63 39.43 38.44 38.64 38.44 39.58 38.90 40.03 40.00 40.92 37.62
     [61] 36.48 37.68 36.35 39.95 38.21 39.62 38.27 41.68 37.89 39.57 37.17 37.74
     [73] 34.59 34.67 40.37 39.04 38.27 38.68 38.37    NA 38.58 38.14 40.48 38.48
     [85] 41.24 39.01 38.93 37.33 38.92 40.06    NA 40.18 36.37 40.58 34.87 39.86
     [97] 42.06 38.18 39.97 39.85 39.01 38.09 39.49 39.46 41.04 37.70    NA 37.08
    [109] 39.10 38.26 39.11 41.05 40.15 37.47 36.54 40.84 36.27 37.96 38.74 40.82
    [121] 40.10 39.83 35.14 37.85 41.11 40.55 39.86 39.64 39.45 38.63 39.75 37.40
    [133] 40.11 37.26 40.73 36.92 37.88 37.65 40.50 40.90 37.04 26.95 40.29 39.60
    [145] 40.36 37.74 39.88 37.58 41.11 39.52 39.76 41.02 39.35 41.88 40.99 38.60
    [157] 38.69 38.58 40.89 37.17 37.10 37.22 40.23 37.63 36.85 40.55 34.70 40.03
    [169] 37.39 38.33 38.58 40.07 37.86 37.74 39.71 38.60 40.41 37.89 37.97 35.73
    [181] 28.04 39.93 39.76 40.50 36.41 38.41 39.77 41.22 41.84 32.47 39.02 40.04
    [193] 38.06 37.94 41.00 40.04 39.84 40.93 38.36 36.45 42.30 33.67 40.48 39.67
    [205] 38.92 39.81 39.80 38.25 35.62 36.16 39.14 38.88 37.59 27.33 37.78 40.35
    [217] 32.64 38.94 41.96 34.14 36.94 39.85 39.42 39.94 38.99 27.99 40.33 38.34
    [229] 36.82 38.18 39.98 39.44 39.70 38.75 40.42 43.16 39.05 40.80 39.52 34.64
    [241] 39.12 37.57 39.50 40.80 38.27 41.29 38.55 39.20 40.45 40.74 37.74 39.09
    [253] 30.71 37.46 37.33 40.80 38.44 37.05 38.26 42.86 40.25 34.02 38.02 39.54
    [265] 40.99 38.66 40.75 39.92 37.55 37.13 37.49 37.22 40.56 37.46 24.69 30.65
    [277] 39.91 39.85 38.99 39.85 32.81    NA 37.42 40.74 38.51 38.99 39.21 41.79
    [289] 39.65 37.67 37.83 40.82 39.69 40.01 37.33 40.34 39.61 40.48 40.97 37.43
    [301] 38.48 38.56 41.17 40.64 37.90 39.65 39.04 38.90 41.71 41.18 36.94 35.27
    [313] 30.52 40.64 40.70 39.81 37.84 41.10 40.07 40.09 35.68 40.35 39.49 38.23
    [325] 38.77 40.79 38.69 30.85 39.76 39.51 39.62 38.04 35.41 38.16 41.38 39.22
    [337] 34.67 36.24 38.72 40.73 38.07 38.87 39.63 39.96 38.55 35.50 40.21 39.04
    [349] 38.23 38.29 41.04 37.79 40.95 40.48 39.13 41.05 32.41 38.97 38.91 40.03
    [361] 42.05    NA 41.03 33.23 37.53 36.23 37.79 38.89 38.23 32.53 39.17 38.78
    [373] 39.35 40.59 38.75 39.40 36.44 38.95 37.27 38.69    NA 37.65 37.96 39.49
    [385] 38.15 40.57 39.57 39.76 39.96 38.36 40.12 39.75 38.05 38.26 36.99    NA
    [397] 40.38 37.96    NA    NA 37.82 37.91 38.36 39.64 38.83 39.77 40.45 40.36
    [409] 40.13 37.11 34.06 39.70 39.90 35.95 39.38 39.49 36.28 39.74 40.15 39.60
    [421] 40.27 40.66 37.54 38.67 37.36 38.68 42.20 40.35 34.19 38.86 39.90 36.37
    [433] 39.57 41.99 40.19 37.86 39.74 39.43 31.29 40.93 39.90 38.45 39.94 40.18
    [445] 31.37 38.98 40.62 40.78 40.82 37.94 40.20 35.97 40.61 40.03 39.43 39.18
    [457] 39.06 38.75 41.26 41.31 39.20 41.02 41.13 36.09 38.90 39.86 39.56 39.02
    [469] 40.09 38.93 38.48 37.10 40.44 40.30 40.67 31.71 39.56 39.80 34.34 39.70
    [481] 39.88 39.52 38.85 39.54 39.22 38.09 40.08 38.09 38.26 37.59 39.66 38.91
    [493] 39.43 38.03 41.01 38.45 38.03 38.50 39.92 37.97
    births$gestwks
      [1] 38.52    NA 38.15 39.80 38.89 40.97 42.14 40.21 42.03 39.33 40.92 38.97
     [13] 41.02 40.05 39.23 38.96 39.35 38.99 40.74 39.45 39.27 32.80 38.72 40.19
     [25] 40.14 39.55 39.38 36.53 41.66 40.45 39.71 39.18 38.51 36.40 40.46 39.47
     [37] 39.12 40.94 39.35 38.80 40.67 37.87 38.51 39.04 40.36 37.44 39.58 35.70
     [49] 39.19 40.63 39.43 38.44 38.64 38.44 39.58 38.90 40.03 40.00 40.92 37.62
     [61] 36.48 37.68 36.35 39.95 38.21 39.62 38.27 41.68 37.89 39.57 37.17 37.74
     [73] 34.59 34.67 40.37 39.04 38.27 38.68 38.37    NA 38.58 38.14 40.48 38.48
     [85] 41.24 39.01 38.93 37.33 38.92 40.06    NA 40.18 36.37 40.58 34.87 39.86
     [97] 42.06 38.18 39.97 39.85 39.01 38.09 39.49 39.46 41.04 37.70    NA 37.08
    [109] 39.10 38.26 39.11 41.05 40.15 37.47 36.54 40.84 36.27 37.96 38.74 40.82
    [121] 40.10 39.83 35.14 37.85 41.11 40.55 39.86 39.64 39.45 38.63 39.75 37.40
    [133] 40.11 37.26 40.73 36.92 37.88 37.65 40.50 40.90 37.04 26.95 40.29 39.60
    [145] 40.36 37.74 39.88 37.58 41.11 39.52 39.76 41.02 39.35 41.88 40.99 38.60
    [157] 38.69 38.58 40.89 37.17 37.10 37.22 40.23 37.63 36.85 40.55 34.70 40.03
    [169] 37.39 38.33 38.58 40.07 37.86 37.74 39.71 38.60 40.41 37.89 37.97 35.73
    [181] 28.04 39.93 39.76 40.50 36.41 38.41 39.77 41.22 41.84 32.47 39.02 40.04
    [193] 38.06 37.94 41.00 40.04 39.84 40.93 38.36 36.45 42.30 33.67 40.48 39.67
    [205] 38.92 39.81 39.80 38.25 35.62 36.16 39.14 38.88 37.59 27.33 37.78 40.35
    [217] 32.64 38.94 41.96 34.14 36.94 39.85 39.42 39.94 38.99 27.99 40.33 38.34
    [229] 36.82 38.18 39.98 39.44 39.70 38.75 40.42 43.16 39.05 40.80 39.52 34.64
    [241] 39.12 37.57 39.50 40.80 38.27 41.29 38.55 39.20 40.45 40.74 37.74 39.09
    [253] 30.71 37.46 37.33 40.80 38.44 37.05 38.26 42.86 40.25 34.02 38.02 39.54
    [265] 40.99 38.66 40.75 39.92 37.55 37.13 37.49 37.22 40.56 37.46 24.69 30.65
    [277] 39.91 39.85 38.99 39.85 32.81    NA 37.42 40.74 38.51 38.99 39.21 41.79
    [289] 39.65 37.67 37.83 40.82 39.69 40.01 37.33 40.34 39.61 40.48 40.97 37.43
    [301] 38.48 38.56 41.17 40.64 37.90 39.65 39.04 38.90 41.71 41.18 36.94 35.27
    [313] 30.52 40.64 40.70 39.81 37.84 41.10 40.07 40.09 35.68 40.35 39.49 38.23
    [325] 38.77 40.79 38.69 30.85 39.76 39.51 39.62 38.04 35.41 38.16 41.38 39.22
    [337] 34.67 36.24 38.72 40.73 38.07 38.87 39.63 39.96 38.55 35.50 40.21 39.04
    [349] 38.23 38.29 41.04 37.79 40.95 40.48 39.13 41.05 32.41 38.97 38.91 40.03
    [361] 42.05    NA 41.03 33.23 37.53 36.23 37.79 38.89 38.23 32.53 39.17 38.78
    [373] 39.35 40.59 38.75 39.40 36.44 38.95 37.27 38.69    NA 37.65 37.96 39.49
    [385] 38.15 40.57 39.57 39.76 39.96 38.36 40.12 39.75 38.05 38.26 36.99    NA
    [397] 40.38 37.96    NA    NA 37.82 37.91 38.36 39.64 38.83 39.77 40.45 40.36
    [409] 40.13 37.11 34.06 39.70 39.90 35.95 39.38 39.49 36.28 39.74 40.15 39.60
    [421] 40.27 40.66 37.54 38.67 37.36 38.68 42.20 40.35 34.19 38.86 39.90 36.37
    [433] 39.57 41.99 40.19 37.86 39.74 39.43 31.29 40.93 39.90 38.45 39.94 40.18
    [445] 31.37 38.98 40.62 40.78 40.82 37.94 40.20 35.97 40.61 40.03 39.43 39.18
    [457] 39.06 38.75 41.26 41.31 39.20 41.02 41.13 36.09 38.90 39.86 39.56 39.02
    [469] 40.09 38.93 38.48 37.10 40.44 40.30 40.67 31.71 39.56 39.80 34.34 39.70
    [481] 39.88 39.52 38.85 39.54 39.22 38.09 40.08 38.09 38.26 37.59 39.66 38.91
    [493] 39.43 38.03 41.01 38.45 38.03 38.50 39.92 37.97

New variables

  1. Create a logical variable called early according to whether gestwks is less than 30 or not. Make a frequency table of early using table
births %>% 
  mutate(early = gestwks < 30) %>% 
  with(table(.$early))

FALSE  TRUE 
  485     5 
  1. Print the id numbers of women with gestwks less than 30 weeks

    which(births$gestwks < 30)
    [1] 142 181 214 226 275
births %>% 
  filter(gestwks < 30) %>% 
  select(id)
     id
142 142
181 181
214 214
226 226
275 275