Exercise 1: Vectors

  1. Generate and print a vector of 10 random numbers between 5 and 500.
sample(5:500, 10)
 [1] 118 268 315  37 322 146 114 288 293 333
  1. Generate a random vector Z of 1000 letters (from “a” to “z”). Hint: the variable letters is already defined in R.
Z <- sample(letters, 1000, replace = T)
head(Z)
[1] "q" "e" "y" "f" "l" "j"
  1. Print a summary of Z in the form of a frequency table.
table(Z)
Z
 a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z 
32 37 39 36 44 49 44 29 36 34 41 36 39 46 41 42 30 32 31 27 35 40 35 50 49 46 
  1. Print the list of letters that appear an even number of times in Z.
table(Z)[table(Z) %% 2 == 0]
Z
 a  d  e  g  i  j  l  n  p  q  r  v  x  z 
32 36 44 44 36 34 36 46 42 30 32 40 50 46 

Exercise 2: Matrices

  1. Create the following 6 by 6 matrix and store it as variable X.
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    7   13   19   25   31
[2,]    2    8   14   20   26   32
[3,]    3    9   15   21   27   33
[4,]    4   10   16   22   28   34
[5,]    5   11   17   23   29   35
[6,]    6   12   18   24   30   36
  1. Create a matrix Y by adding an independent Gaussian noise (random numbers) with mean 0 and standard deviation 1 to each entry of X. e.g.
Y <- X + matrix(rnorm(36), nrow = 6)
Y
            [,1]      [,2]     [,3]     [,4]     [,5]     [,6]
[1,] -0.08834993  6.877115 13.56755 20.66052 25.63874 29.88539
[2,]  2.76798355  8.086891 13.58110 20.57302 27.27305 31.27714
[3,]  3.20178050  8.914622 15.63921 22.18058 28.64890 31.97319
[4,]  5.11792109 10.202627 16.31552 22.60226 27.50422 34.78072
[5,]  6.01432654  8.477239 17.07766 23.86974 27.81812 33.88557
[6,]  6.58302237 11.498260 15.93590 24.24174 31.56693 34.76067
  1. Find the inverse of Y.
invY <- solve(Y)
  1. Show numerically that the matrix product of Y and its inverse is the identity matrix.
res <- invY %*% Y
res
              [,1]          [,2]          [,3]          [,4]          [,5]          [,6]
[1,]  1.000000e+00  1.942890e-15  3.552714e-15  6.439294e-15  9.214851e-15  8.881784e-15
[2,]  0.000000e+00  1.000000e+00 -8.881784e-16 -2.664535e-15 -5.329071e-15 -1.776357e-15
[3,]  8.881784e-16 -1.776357e-15  1.000000e+00  1.776357e-15  0.000000e+00 -1.776357e-15
[4,] -4.440892e-16  8.881784e-16  0.000000e+00  1.000000e+00 -1.776357e-15  3.552714e-15
[5,]  2.220446e-16 -2.220446e-16 -2.220446e-16 -2.220446e-16  1.000000e+00 -1.776357e-15
[6,]  0.000000e+00  0.000000e+00  0.000000e+00  1.332268e-15  8.881784e-16  1.000000e+00
round(res, digits = 6)
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    0    0    0    0    0
[2,]    0    1    0    0    0    0
[3,]    0    0    1    0    0    0
[4,]    0    0    0    1    0    0
[5,]    0    0    0    0    1    0
[6,]    0    0    0    0    0    1

Exercise 3: Data fames

  1. Create the following data frame and name it “exams”.
  1. Compute the mean score for this exam and print it.
mean(exams$score)
[1] 92.8
  1. Find the student with the highest score and print the corresponding row of “exams”. Hint: use the function which.max().
exams[which.max(exams$score), ]
LS0tCnRpdGxlOiAiTGVjdHVyZSAxOiBFeGVyY2lzZSIKZGF0ZTogU2VwdGVtYmVyIDI3dGgsIDIwMTgKb3V0cHV0OiAKICBodG1sX25vdGVib29rOgogICAgdG9jOiB0cnVlCiAgICB0b2NfZmxvYXQ6IHRydWUKLS0tCgojIEV4ZXJjaXNlIDE6IFZlY3RvcnMKCjEuIEdlbmVyYXRlIGFuZCBwcmludCBhIHZlY3RvciBvZiAxMCByYW5kb20gbnVtYmVycyBiZXR3ZWVuIDUgYW5kIDUwMC4KCmBgYHtyfQpzYW1wbGUoNTo1MDAsIDEwKQpgYGAKCgoyLiBHZW5lcmF0ZSBhIHJhbmRvbSB2ZWN0b3IgWiBvZiAxMDAwIGxldHRlcnMgKGZyb20gImEiIHRvICJ6IikuIEhpbnQ6IHRoZSAKdmFyaWFibGUgYGxldHRlcnNgIGlz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