In this practical, a number of R packages are used. The packages used (with versions that were used to generate the solutions) are:
survival
(version: 3.2.7)For this practical, we will use the heart and retinopathy data sets from the survival
package. More details about the data sets can be found in:
https://stat.ethz.ch/R-manual/R-devel/library/survival/html/heart.html
https://stat.ethz.ch/R-manual/R-devel/library/survival/html/retinopathy.html
Always check your data.
Explore the heart and retinopathy data sets - print the first six and last six rows.
Use the functions head(…) and tail(…) to investigate the data set. Replace the dots with the name of the data set.
library(survival)
head(heart)
tail(heart)
head(retinopathy)
tail(retinopathy)
It is important to distinguish the different object in R.
Let’s investigate some vectors.
View the vectors event
and age
from the heart data set.
Use the dollar sign to select the variables.
$event heart
## [1] 1 1 0 1 0 1 1 1 0 1 1 1 0 1 0 1 1 0 1 0 1 1 0 1 1 0 1 1 0 1 0 1 0 1 0 1 0 1 0 0 0 1 0 1 1 0 1
## [48] 1 0 1 0 0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 0 1 1 1 0 1 0 1 0 1 1 0 0 0 1 0 1 1 0 1 1 0 1 0 0 1 0 1
## [95] 0 0 0 1 1 1 0 0 0 1 0 1 1 0 1 0 1 0 0 0 1 0 0 0 0 0 1 0 1 1 0 0 1 0 0 0 1 0 0 0 0 0 0 1 0 1 1
## [142] 0 0 0 1 0 0 0 1 0 1 1 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 0 0 0 1
$age heart
## [1] -17.15537303 3.83572895 6.29705681 6.29705681 -7.73716632 -7.73716632 -27.21423682
## [8] 6.59548255 2.86926762 2.86926762 -2.65023956 -0.83778234 -5.49760438 -5.49760438
## [15] -0.01916496 -0.01916496 5.19370294 6.57357974 6.57357974 6.01232033 6.01232033
## [22] 5.81519507 1.44832307 1.44832307 -27.66872005 8.84873374 8.84873374 11.12388775
## [29] 7.27994524 7.27994524 -4.65708419 -4.65708419 -5.21560575 -5.21560575 10.35728953
## [36] 10.35728953 3.80013689 3.80013689 -14.77618070 -14.77618070 -17.46475017 -39.21423682
## [43] 6.02327173 6.02327173 2.43394935 -3.08829569 -3.08829569 6.88569473 16.40793977
## [50] 16.40793977 0.90349076 0.90349076 -7.44695414 -7.44695414 -4.53388090 0.92539357
## [57] 0.92539357 13.50034223 13.50034223 -6.52977413 -6.52977413 2.51882272 2.51882272
## [64] 0.48186174 0.48186174 -2.69678303 -2.69678303 -11.55920602 -4.60780287 -5.42094456
## [71] -11.81656400 -11.81656400 0.61054073 0.61054073 -0.90075291 -0.90075291 8.03559206
## [78] -11.34565366 -11.34565366 -2.11362081 -2.11362081 0.73374401 0.73374401 -6.75154004
## [85] -0.65708419 -0.65708419 -0.20807666 4.45448323 4.45448323 -9.25667351 -9.25667351
## [92] -6.73511294 0.01642710 0.01642710 -6.61738535 -6.61738535 1.05407255 1.05407255
## [99] 4.56399726 -8.64613279 -15.34017796 -15.34017796 0.81587953 0.81587953 3.29363450
## [106] 3.29363450 5.21286790 -28.44900753 -28.44900753 -2.75975359 -2.75975359 -0.01095140
## [113] -0.01095140 5.00205339 5.00205339 -0.59137577 -0.59137577 -21.27310062 -21.27310062
## [120] 8.33127995 8.33127995 -18.83367556 -18.83367556 4.18069815 4.08487337 4.08487337
## [127] -6.88843258 0.70362765 0.70362765 5.78234086 5.78234086 -1.55509925 -1.55509925
## [134] 4.89253936 4.89253936 -18.79808350 5.30869268 5.30869268 -5.28131417 -5.28131417
## [141] -0.01916496 0.91991786 0.91991786 -1.74674880 -1.74674880 6.36276523 6.36276523
## [148] 3.04722793 3.04722793 4.03285421 4.03285421 -0.40520192 -3.01711157 -3.01711157
## [155] -0.24914442 -0.24914442 -4.15879535 -4.15879535 -7.71800137 -7.71800137 -21.34976044
## [162] -21.34976044 -24.38329911 -24.38329911 -19.37029432 -19.37029432 1.83436003 -12.93908282
## [169] -12.93908282 1.51676934 -7.60848734 -8.68446270
View the vectors eye
and risk
from the retinopathy data set.
Use the dollar sign to select the variables.
$eye retinopathy
## [1] left left right right right right left left left left right right right right right
## [16] right right right right right left left right right left left right right right right
## [31] left left left left right right right right right right left left right right right
## [46] right left left left left right right right right left left left left left left
## [61] left left left left left left left left left left left left right right left
## [76] left left left right right left left right right left left right right right right
## [91] left left left left right right right right right right right right left left left
## [106] left right right left left left left right right left left right right right right
## [121] right right left left left left left left left left right right left left left
## [136] left right right right right left left left left right right left left left left
## [151] left left left left left left left left right right right right right right left
## [166] left left left left left left left left left right right left left left left
## [181] left left right right left left right right right right left left left left right
## [196] right left left left left left left left left right right right right left left
## [211] left left right right left left left left left left right right left left left
## [226] left left left right right left left right right left left left left left left
## [241] right right left left right right left left left left left left left left right
## [256] right left left right right right right right right left left left left left left
## [271] left left left left left left right right right right left left right right right
## [286] right right right right right left left right right left left right right left left
## [301] right right right right right right right right left left right right left left right
## [316] right left left right right left left left left right right right right left left
## [331] right right left left right right left left left left left left left left right
## [346] right right right left left right right right right right right right right right right
## [361] right right left left left left left left right right left left right right right
## [376] right left left left left right right left left left left left left right right
## [391] right right right right
## Levels: right left
$risk retinopathy
## [1] 9 9 8 6 11 11 11 11 9 10 9 9 8 6 8 9 9 10 9 9 9 9 11 12 11 6 6 10 10 9 9
## [32] 10 12 9 9 9 6 8 11 12 9 9 9 12 10 10 12 11 12 9 9 11 9 11 10 12 11 11 10 9 9 9
## [63] 10 8 9 8 9 9 9 11 11 12 11 8 9 9 9 9 10 12 10 9 8 12 10 12 10 10 11 10 10 10 11
## [94] 9 9 8 10 6 9 12 8 8 9 9 9 10 8 9 9 8 10 9 11 10 10 11 8 9 10 8 10 9 9 10
## [125] 9 9 9 10 12 11 9 12 9 8 12 10 10 9 6 9 12 10 11 11 9 6 9 12 12 12 9 12 8 6 11
## [156] 12 10 9 8 10 11 11 9 12 12 10 8 8 9 11 9 12 11 11 12 9 9 9 12 9 8 8 9 11 11 11
## [187] 9 9 10 12 10 12 10 12 12 12 9 12 9 9 8 6 11 10 10 10 6 8 6 10 6 8 10 10 12 11 10
## [218] 12 12 12 9 10 12 9 9 9 9 9 9 10 12 10 11 11 10 9 9 11 8 9 12 12 11 9 9 9 9 9
## [249] 11 11 10 11 10 11 10 9 9 10 11 11 12 10 9 9 10 9 9 12 10 9 12 11 9 9 11 12 6 10 8
## [280] 8 9 9 9 9 9 9 12 11 9 10 10 9 9 9 10 10 11 8 10 9 9 6 11 9 9 11 10 10 10 9
## [311] 6 6 11 11 11 11 10 9 8 12 11 11 11 11 9 10 8 12 9 9 9 8 11 9 11 9 9 9 12 11 10
## [342] 9 9 9 12 10 12 11 10 9 9 9 9 9 9 10 8 12 9 10 10 10 9 9 9 12 10 12 6 6 11 9
## [373] 9 9 10 12 6 8 9 9 10 8 11 10 8 8 12 11 9 10 10 10 9 9
Create a numerical vector that consists of the values: 34, 24, 19, 23, 16. Give the name numbers
to this new vector.
Use the c(…) function. Replace the dots with the numbers.
<- c(34, 24, 19, 23, 16) numbers
Create a numerical vector that takes the integer values from 1 until 200. Give the name numbers_2
to this new vector.
Use the c(…) function. Replace the dots with the numbers.
<- c(1:200) numbers_2
Create a categorical vector that consists of the values: yes, yes, no, no, no, yes. Give the name treatment
to this new vector.
Use the c(…) function. Replace the dots with the categories.
<- c("yes", "yes", "no", "no", "no", "yes") treatment
Let’s investigate some matrices and data frames.
Create a matrix using the vectors id
and age
from the heart data set. This matrix should have 2 columns where each column represents each variable.
Use the function matrix(…).
matrix(c(heart$id, heart$age), , 2)
## [,1] [,2]
## [1,] 1 -17.15537303
## [2,] 2 3.83572895
## [3,] 3 6.29705681
## [4,] 3 6.29705681
## [5,] 4 -7.73716632
## [6,] 4 -7.73716632
## [7,] 5 -27.21423682
## [8,] 6 6.59548255
## [9,] 7 2.86926762
## [10,] 7 2.86926762
## [11,] 8 -2.65023956
## [12,] 9 -0.83778234
## [13,] 10 -5.49760438
## [14,] 10 -5.49760438
## [15,] 11 -0.01916496
## [16,] 11 -0.01916496
## [17,] 12 5.19370294
## [18,] 13 6.57357974
## [19,] 13 6.57357974
## [20,] 14 6.01232033
## [21,] 14 6.01232033
## [22,] 15 5.81519507
## [23,] 16 1.44832307
## [24,] 16 1.44832307
## [25,] 17 -27.66872005
## [26,] 18 8.84873374
## [27,] 18 8.84873374
## [28,] 19 11.12388775
## [29,] 20 7.27994524
## [30,] 20 7.27994524
## [31,] 21 -4.65708419
## [32,] 21 -4.65708419
## [33,] 22 -5.21560575
## [34,] 22 -5.21560575
## [35,] 23 10.35728953
## [36,] 23 10.35728953
## [37,] 24 3.80013689
## [38,] 24 3.80013689
## [39,] 25 -14.77618070
## [40,] 25 -14.77618070
## [41,] 26 -17.46475017
## [42,] 27 -39.21423682
## [43,] 28 6.02327173
## [44,] 28 6.02327173
## [45,] 29 2.43394935
## [46,] 30 -3.08829569
## [47,] 30 -3.08829569
## [48,] 31 6.88569473
## [49,] 32 16.40793977
## [50,] 32 16.40793977
## [51,] 33 0.90349076
## [52,] 33 0.90349076
## [53,] 34 -7.44695414
## [54,] 34 -7.44695414
## [55,] 35 -4.53388090
## [56,] 36 0.92539357
## [57,] 36 0.92539357
## [58,] 37 13.50034223
## [59,] 37 13.50034223
## [60,] 38 -6.52977413
## [61,] 38 -6.52977413
## [62,] 39 2.51882272
## [63,] 39 2.51882272
## [64,] 40 0.48186174
## [65,] 40 0.48186174
## [66,] 41 -2.69678303
## [67,] 41 -2.69678303
## [68,] 42 -11.55920602
## [69,] 43 -4.60780287
## [70,] 44 -5.42094456
## [71,] 45 -11.81656400
## [72,] 45 -11.81656400
## [73,] 46 0.61054073
## [74,] 46 0.61054073
## [75,] 47 -0.90075291
## [76,] 47 -0.90075291
## [77,] 48 8.03559206
## [78,] 49 -11.34565366
## [79,] 49 -11.34565366
## [80,] 50 -2.11362081
## [81,] 50 -2.11362081
## [82,] 51 0.73374401
## [83,] 51 0.73374401
## [84,] 52 -6.75154004
## [85,] 53 -0.65708419
## [86,] 53 -0.65708419
## [87,] 54 -0.20807666
## [88,] 55 4.45448323
## [89,] 55 4.45448323
## [90,] 56 -9.25667351
## [91,] 56 -9.25667351
## [92,] 57 -6.73511294
## [93,] 58 0.01642710
## [94,] 58 0.01642710
## [95,] 59 -6.61738535
## [96,] 59 -6.61738535
## [97,] 60 1.05407255
## [98,] 60 1.05407255
## [99,] 61 4.56399726
## [100,] 62 -8.64613279
## [101,] 63 -15.34017796
## [102,] 63 -15.34017796
## [103,] 64 0.81587953
## [104,] 64 0.81587953
## [105,] 65 3.29363450
## [106,] 65 3.29363450
## [107,] 66 5.21286790
## [108,] 67 -28.44900753
## [109,] 67 -28.44900753
## [110,] 68 -2.75975359
## [111,] 68 -2.75975359
## [112,] 69 -0.01095140
## [113,] 69 -0.01095140
## [114,] 70 5.00205339
## [115,] 70 5.00205339
## [116,] 71 -0.59137577
## [117,] 71 -0.59137577
## [118,] 72 -21.27310062
## [119,] 72 -21.27310062
## [120,] 73 8.33127995
## [121,] 73 8.33127995
## [122,] 74 -18.83367556
## [123,] 74 -18.83367556
## [124,] 75 4.18069815
## [125,] 76 4.08487337
## [126,] 76 4.08487337
## [127,] 77 -6.88843258
## [128,] 78 0.70362765
## [129,] 78 0.70362765
## [130,] 79 5.78234086
## [131,] 79 5.78234086
## [132,] 80 -1.55509925
## [133,] 80 -1.55509925
## [134,] 81 4.89253936
## [135,] 81 4.89253936
## [136,] 82 -18.79808350
## [137,] 83 5.30869268
## [138,] 83 5.30869268
## [139,] 84 -5.28131417
## [140,] 84 -5.28131417
## [141,] 85 -0.01916496
## [142,] 86 0.91991786
## [143,] 86 0.91991786
## [144,] 87 -1.74674880
## [145,] 87 -1.74674880
## [146,] 88 6.36276523
## [147,] 88 6.36276523
## [148,] 89 3.04722793
## [149,] 89 3.04722793
## [150,] 90 4.03285421
## [151,] 90 4.03285421
## [152,] 91 -0.40520192
## [153,] 92 -3.01711157
## [154,] 92 -3.01711157
## [155,] 93 -0.24914442
## [156,] 93 -0.24914442
## [157,] 94 -4.15879535
## [158,] 94 -4.15879535
## [159,] 95 -7.71800137
## [160,] 95 -7.71800137
## [161,] 96 -21.34976044
## [162,] 96 -21.34976044
## [163,] 97 -24.38329911
## [164,] 97 -24.38329911
## [165,] 98 -19.37029432
## [166,] 98 -19.37029432
## [167,] 99 1.83436003
## [168,] 100 -12.93908282
## [169,] 100 -12.93908282
## [170,] 101 1.51676934
## [171,] 102 -7.60848734
## [172,] 103 -8.68446270
Create a data frame using the vectors id
, type
and trt
from the retinopathy data set. This data frame should have 3 columns, where each column represents each variable.
Use the function data.frame(…).
data.frame(id = retinopathy$id, type = retinopathy$type, trt = retinopathy$trt)
Let’s investigate some arrays.
Create an array that consists of 2 matrices. Matrix 1 will consist of the values 1:4 and matrix 2 will consist of the values 5:8. Both matrices will have 2 columns and 2 rows.
Use the function array(…).
array(data = 1:8, dim = c(2, 2, 2))
## , , 1
##
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
##
## , , 2
##
## [,1] [,2]
## [1,] 5 7
## [2,] 6 8
Give the name ar1
to the previous array. Furthermore, investigate the argument dimnames and change the names of the rows, columns and matrices.
Use the function array(…). Check the help page for the dimnames argument. Note that this must be in a list format.
<- array(data = 1:8, dim = c(2, 2, 2),
ar1 dimnames = list(c("Row1", "Row2"), c("Col1", "Col2"), c("Mat1", "Mat2")))
Let’s investigate some lists.
Create a list using the vectors stop
from the heart data set and id
, risk
from the retinopathy data set. Give the names stop_heart
, id_reti
and risk_reti
.
Use the function list(…).
list(stop_heart = heart$stop, id_reti = retinopathy$id, risk_reti = retinopathy$risk)
## $stop_heart
## [1] 50.0 6.0 1.0 16.0 36.0 39.0 18.0 3.0 51.0 675.0 40.0 85.0 12.0
## [14] 58.0 26.0 153.0 8.0 17.0 81.0 37.0 1387.0 1.0 28.0 308.0 36.0 20.0
## [27] 43.0 37.0 18.0 28.0 8.0 1032.0 12.0 51.0 3.0 733.0 83.0 219.0 25.0
## [40] 1800.0 1401.0 263.0 71.0 72.0 35.0 16.0 852.0 16.0 17.0 77.0 51.0 1587.0
## [53] 23.0 1572.0 12.0 46.0 100.0 19.0 66.0 4.5 5.0 2.0 53.0 41.0 1408.0
## [66] 58.0 1322.0 3.0 2.0 40.0 1.0 45.0 2.0 996.0 21.0 72.0 9.0 36.0
## [79] 1142.0 83.0 980.0 32.0 285.0 102.0 41.0 188.0 3.0 10.0 61.0 67.0 942.0
## [92] 149.0 21.0 343.0 78.0 916.0 3.0 68.0 2.0 69.0 27.0 842.0 33.0 584.0
## [105] 12.0 78.0 32.0 57.0 285.0 3.0 68.0 10.0 670.0 5.0 30.0 31.0 620.0
## [118] 4.0 596.0 27.0 90.0 5.0 17.0 2.0 46.0 545.0 21.0 210.0 515.0 67.0
## [131] 96.0 26.0 482.0 6.0 445.0 428.0 32.0 80.0 37.0 334.0 5.0 8.0 397.0
## [144] 60.0 110.0 31.0 370.0 139.0 207.0 160.0 186.0 340.0 310.0 340.0 28.0 265.0
## [157] 4.0 165.0 2.0 16.0 13.0 180.0 21.0 131.0 96.0 109.0 21.0 38.0 39.0
## [170] 31.0 11.0 6.0
##
## $id_reti
## [1] 5 5 14 14 16 16 25 25 29 29 46 46 49 49 56 56 61 61 71
## [20] 71 100 100 112 112 120 120 127 127 133 133 150 150 167 167 176 176 185 185
## [39] 190 190 202 202 214 214 220 220 243 243 255 255 264 264 266 266 284 284 295
## [58] 295 300 300 302 302 315 315 324 324 328 328 335 335 342 342 349 349 357 357
## [77] 368 368 385 385 396 396 405 405 409 409 419 419 429 429 433 433 445 445 454
## [96] 454 468 468 480 480 485 485 491 491 503 503 515 515 522 522 538 538 547 547
## [115] 550 550 554 554 557 557 561 561 568 568 572 572 576 576 581 581 606 606 610
## [134] 610 615 615 618 618 624 624 631 631 636 636 645 645 653 653 662 662 664 664
## [153] 683 683 687 687 701 701 706 706 717 717 722 722 731 731 740 740 749 749 757
## [172] 757 760 760 766 766 769 769 772 772 778 778 780 780 793 793 800 800 804 804
## [191] 810 810 815 815 832 832 834 834 838 838 857 857 866 866 887 887 903 903 910
## [210] 910 920 920 925 925 931 931 936 936 945 945 949 949 952 952 962 962 964 964
## [229] 971 971 978 978 983 983 987 987 1002 1002 1017 1017 1029 1029 1034 1034 1037 1037 1042
## [248] 1042 1069 1069 1074 1074 1098 1098 1102 1102 1112 1112 1117 1117 1126 1126 1135 1135 1145 1145
## [267] 1148 1148 1167 1167 1184 1184 1191 1191 1205 1205 1213 1213 1228 1228 1247 1247 1250 1250 1253
## [286] 1253 1267 1267 1281 1281 1287 1287 1293 1293 1296 1296 1309 1309 1312 1312 1317 1317 1321 1321
## [305] 1333 1333 1347 1347 1361 1361 1366 1366 1373 1373 1397 1397 1410 1410 1413 1413 1425 1425 1447
## [324] 1447 1461 1461 1469 1469 1480 1480 1487 1487 1491 1491 1499 1499 1503 1503 1513 1513 1524 1524
## [343] 1533 1533 1537 1537 1552 1552 1554 1554 1562 1562 1572 1572 1581 1581 1585 1585 1596 1596 1600
## [362] 1600 1603 1603 1619 1619 1627 1627 1636 1636 1640 1640 1643 1643 1649 1649 1666 1666 1672 1672
## [381] 1683 1683 1688 1688 1705 1705 1717 1717 1727 1727 1746 1746 1749 1749
##
## $risk_reti
## [1] 9 9 8 6 11 11 11 11 9 10 9 9 8 6 8 9 9 10 9 9 9 9 11 12 11 6 6 10 10 9 9
## [32] 10 12 9 9 9 6 8 11 12 9 9 9 12 10 10 12 11 12 9 9 11 9 11 10 12 11 11 10 9 9 9
## [63] 10 8 9 8 9 9 9 11 11 12 11 8 9 9 9 9 10 12 10 9 8 12 10 12 10 10 11 10 10 10 11
## [94] 9 9 8 10 6 9 12 8 8 9 9 9 10 8 9 9 8 10 9 11 10 10 11 8 9 10 8 10 9 9 10
## [125] 9 9 9 10 12 11 9 12 9 8 12 10 10 9 6 9 12 10 11 11 9 6 9 12 12 12 9 12 8 6 11
## [156] 12 10 9 8 10 11 11 9 12 12 10 8 8 9 11 9 12 11 11 12 9 9 9 12 9 8 8 9 11 11 11
## [187] 9 9 10 12 10 12 10 12 12 12 9 12 9 9 8 6 11 10 10 10 6 8 6 10 6 8 10 10 12 11 10
## [218] 12 12 12 9 10 12 9 9 9 9 9 9 10 12 10 11 11 10 9 9 11 8 9 12 12 11 9 9 9 9 9
## [249] 11 11 10 11 10 11 10 9 9 10 11 11 12 10 9 9 10 9 9 12 10 9 12 11 9 9 11 12 6 10 8
## [280] 8 9 9 9 9 9 9 12 11 9 10 10 9 9 9 10 10 11 8 10 9 9 6 11 9 9 11 10 10 10 9
## [311] 6 6 11 11 11 11 10 9 8 12 11 11 11 11 9 10 8 12 9 9 9 8 11 9 11 9 9 9 12 11 10
## [342] 9 9 9 12 10 12 11 10 9 9 9 9 9 9 10 8 12 9 10 10 10 9 9 9 12 10 12 6 6 11 9
## [373] 9 9 10 12 6 8 9 9 10 8 11 10 8 8 12 11 9 10 10 10 9 9
Create a list using the vectors numbers
, numbers_2
and treatment
. These variables can be taken from the exercise called Vectors
. Give the names: numbers
, many_numbers
and treatment
.
Use the function list(…).
list(numbers = numbers, many_numbers = numbers_2, treatment = treatment)
## $numbers
## [1] 34 24 19 23 16
##
## $many_numbers
## [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] 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
## [47] 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
## [70] 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
## [93] 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
## [116] 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
## [139] 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
## [162] 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
## [185] 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
##
## $treatment
## [1] "yes" "yes" "no" "no" "no" "yes"
© Eleni-Rosalina Andrinopoulou