Load packages

If you are using the package for the first time, you will first have to install it.

# install.packages("survival") 

If you have already downloaded this package in the current version of R, you will only have to load the package.

library(survival)

Get the data

Load a data set from a package.
You can use the double colon symbol (:), to return the pbc object from the package survival. We store this data set to an object with the name pbc.

pbc <- survival::pbc
pbcseq <- survival::pbcseq

Remember that for indexing/subsetting we need to use the square brackets.

Vector indexing

Select the 3rd element from vector age of the pbc data set.

pbc$age[3]
## [1] 70.07255

Select the sex of the 10th patient of the pbc data set.

pbc$sex[10]
## [1] f
## Levels: m f

Remove the 1st element from the id vector of the pbc data set.

pbc$id[-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  27  28  29
##  [29]  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57
##  [57]  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85
##  [85]  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
## [113] 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
## [141] 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
## [169] 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
## [197] 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
## [225] 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
## [253] 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
## [281] 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
## [309] 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
## [337] 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
## [365] 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
## [393] 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418

From the vector age of the pbc data set, select patients that are younger than 30.

pbc$age[pbc$age < 30]
## [1] 28.88433 29.55510 26.27789

From the vector age of the pbc data set, select only female patients.

pbc$age[pbc$sex == "f"]
##   [1] 58.76523 56.44627 54.74059 38.10541 66.25873 55.53457 53.05681 42.50787 70.55989 53.71389 59.13758 45.68925
##  [13] 64.64613 40.44353 52.18344 53.93018 49.56057 59.95346 56.27652 55.96715 45.07324 52.02464 54.43943 44.94730
##  [25] 63.87680 41.38535 41.55236 53.99589 51.28268 52.06023 48.61875 56.41068 61.72758 36.62697 55.39220 46.66940
##  [37] 33.63450 33.69473 48.87064 37.58248 41.79329 45.79877 47.42779 61.15264 53.50856 52.08761 67.40862 39.19781
##  [49] 33.61807 53.57153 40.39425 58.38193 60.70637 46.62834 62.90760 40.20260 51.28816 32.61328 49.33881 56.39973
##  [61] 48.84600 32.49281 38.49418 51.92060 43.51814 51.94251 49.82615 47.94524 46.51608 63.26352 67.31006 56.01369
##  [73] 55.83025 47.21697 52.75838 37.27858 41.39357 52.44353 45.60712 76.70910 36.53388 53.91650 46.39014 48.84600
##  [85] 28.88433 44.95003 56.56947 48.96372 43.01711 34.03970 68.50924 62.52156 50.35729 44.06297 38.91034 41.15264
##  [97] 55.45791 51.23340 42.63929 61.07050 49.65640 48.85421 54.25599 55.43600 45.82067 47.18138 53.59890 44.10404
## [109] 41.94935 63.61396 44.22724 62.00137 40.55305 42.33539 42.96783 55.96167 62.86105 46.76249 54.07529 47.03628
## [121] 55.72621 46.10267 52.28747 51.20055 33.86448 75.01164 30.86379 34.98700 55.04175 49.60438 43.55647 59.40862
## [133] 48.75838 36.49281 57.37166 42.74333 58.81725 53.49760 43.41410 41.35524 47.75359 35.49076 48.66256 52.66804
## [145] 49.86995 30.27515 55.56742 52.15332 41.60986 55.45243 70.00411 43.94251 42.56810 44.56947 56.94456 40.26010
## [157] 37.60712 48.36140 70.83641 35.79192 62.62286 50.64750 54.52704 52.69268 52.72005 56.77207 44.39699 29.55510
## [169] 57.04038 44.62697 35.79740 40.71732 32.23272 41.09240 61.63997 37.05681 62.57906 48.97741 61.99042 72.77207
## [181] 61.29500 52.62423 52.91444 47.26352 50.20397 69.34702 41.16906 59.16496 36.07940 34.59548 42.71321 63.63039
## [193] 56.62971 46.26420 61.24298 38.62012 38.77070 56.69541 36.92266 62.41478 34.60917 58.33539 50.18207 42.68583
## [205] 34.37919 33.18275 38.38193 59.76181 66.41205 46.78987 56.07940 41.37440 64.57221 67.48802 44.82957 45.77139
## [217] 32.95003 41.22108 55.41684 47.98084 40.79124 56.97467 68.46270 39.85763 35.31006 31.44422 58.26420 51.48802
## [229] 59.96988 52.36413 42.78713 34.87474 44.13963 46.38193 56.30938 70.90760 55.39493 45.08419 26.27789 50.47228
## [241] 38.39836 47.41958 47.98084 38.31622 50.10815 35.08830 32.50376 56.15332 46.15469 65.88364 33.94387 62.86105
## [253] 48.56400 46.34908 38.85284 58.64750 48.93634 65.98494 40.90075 57.19644 31.38125 52.72553 38.09172 58.17112
## [265] 45.21013 37.79877 60.65982 35.53457 43.06639 56.39151 30.57358 61.18275 58.29979 62.33265 37.99863 33.15264
## [277] 60.00000 64.99932 54.00137 75.00068 62.00137 43.00068 46.00137 44.00000 64.00000 40.00000 63.00068 34.00137
## [289] 52.00000 48.99932 54.00137 63.00068 46.00137 52.99932 56.00000 56.00000 55.00068 64.99932 56.00000 47.00068
## [301] 60.00000 52.99932 54.00137 50.00137 48.00000 36.00000 48.00000 70.00137 51.00068 54.00137 48.00000 66.00137
## [313] 52.99932 62.00137 59.00068 39.00068 67.00068 58.00137 64.00000 46.00137 64.00000 40.99932 48.99932 44.00000
## [325] 59.00068 63.00068 60.99932 64.00000 48.99932 42.00137 50.00137 51.00068 36.99932 62.00137 51.00068 52.00000
## [337] 32.99932 60.00000 63.00068 32.99932 51.00068 36.99932 59.00068 55.00068 48.99932 40.00000 67.00068 40.99932
## [349] 68.99932 52.00000 56.99932 36.00000 50.00137 64.00000 62.00137 42.00137 44.00000 68.99932 52.00000 66.00137
## [361] 40.00000 52.00000 46.00137 51.00068 43.00068 39.00068 51.00068 67.00068 35.00068 67.00068 39.00068 56.99932
## [373] 58.00137 52.99932

Matrix and data frame indexing

Select the 3rd column of the pbc data set. To do so we type 3 in the second index which represents the columns. 

pbc[, 3]
##   [1] 2 0 2 2 1 2 0 2 2 2 2 2 0 2 2 0 2 2 0 2 0 2 2 2 0 2 2 2 0 2 2 0 2 0 2 0 2 2 2 0 2 0 0 2 0 2 0 0 2 2 2 2 2 2 2 2
##  [57] 2 0 2 0 0 2 2 2 0 2 2 0 2 0 0 0 0 2 2 2 2 2 0 2 2 2 0 0 2 2 2 0 2 2 2 2 0 2 2 0 2 0 0 2 0 0 2 2 1 2 0 2 0 2 1 2
## [113] 2 2 0 0 2 2 2 1 2 0 2 0 1 2 0 2 0 2 2 0 2 0 0 0 0 2 0 0 0 2 2 2 0 0 0 2 2 0 0 2 0 2 0 2 0 1 2 0 0 2 2 2 2 0 2 0
## [169] 2 0 0 0 0 0 0 2 0 0 0 0 0 0 1 2 0 2 2 0 0 0 2 0 2 0 0 0 0 0 0 0 0 0 0 2 2 0 0 2 0 0 0 0 0 2 2 0 2 0 0 2 0 2 2 0
## [225] 0 0 2 0 2 0 2 0 0 0 0 0 0 0 2 0 1 0 2 2 0 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 1 0 2 2 0 0 0 0 0 1 0 0 0 0 0 0
## [281] 2 0 0 0 0 0 0 1 2 0 1 0 0 0 1 0 1 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 2 0 0 2 0 0 2 0 0 0 0 0 2 2 2 2 2 2 2 0 0
## [337] 2 2 0 0 2 0 2 0 1 2 2 0 0 2 2 0 0 2 0 2 0 0 0 2 1 1 0 2 0 2 0 2 2 2 2 0 0 0 1 2 0 2 2 1 0 2 1 0 0 0 0 0 0 0 0 0
## [393] 2 0 0 0 0 0 0 0 2 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0

Different ways exist to obtain that. As shown below, we can also use double square bracket with a single index.

pbc[[3]]
##   [1] 2 0 2 2 1 2 0 2 2 2 2 2 0 2 2 0 2 2 0 2 0 2 2 2 0 2 2 2 0 2 2 0 2 0 2 0 2 2 2 0 2 0 0 2 0 2 0 0 2 2 2 2 2 2 2 2
##  [57] 2 0 2 0 0 2 2 2 0 2 2 0 2 0 0 0 0 2 2 2 2 2 0 2 2 2 0 0 2 2 2 0 2 2 2 2 0 2 2 0 2 0 0 2 0 0 2 2 1 2 0 2 0 2 1 2
## [113] 2 2 0 0 2 2 2 1 2 0 2 0 1 2 0 2 0 2 2 0 2 0 0 0 0 2 0 0 0 2 2 2 0 0 0 2 2 0 0 2 0 2 0 2 0 1 2 0 0 2 2 2 2 0 2 0
## [169] 2 0 0 0 0 0 0 2 0 0 0 0 0 0 1 2 0 2 2 0 0 0 2 0 2 0 0 0 0 0 0 0 0 0 0 2 2 0 0 2 0 0 0 0 0 2 2 0 2 0 0 2 0 2 2 0
## [225] 0 0 2 0 2 0 2 0 0 0 0 0 0 0 2 0 1 0 2 2 0 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 1 0 2 2 0 0 0 0 0 1 0 0 0 0 0 0
## [281] 2 0 0 0 0 0 0 1 2 0 1 0 0 0 1 0 1 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 2 0 0 2 0 0 2 0 0 0 0 0 2 2 2 2 2 2 2 0 0
## [337] 2 2 0 0 2 0 2 0 1 2 2 0 0 2 2 0 0 2 0 2 0 0 0 2 1 1 0 2 0 2 0 2 2 2 2 0 0 0 1 2 0 2 2 1 0 2 1 0 0 0 0 0 0 0 0 0
## [393] 2 0 0 0 0 0 0 0 2 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0

Select the baseline details of the 5th patient of the pbc data set. In that case we only need to specify the row index.

pbc[pbc$id == 5, ]
##   id time status trt      age sex ascites hepato spiders edema bili chol albumin copper alk.phos    ast trig
## 5  5 1504      1   2 38.10541   f       0      1       1     0  3.4  279    3.53    143      671 113.15   72
##   platelet protime stage
## 5      136    10.9     3

Select the serum bilirubin for all males of the pbc data set. In that case we need to specify the row and column index.

pbc[pbc$sex == "m", "bili"]
##  [1] 1.4 0.8 0.6 2.1 1.9 6.0 1.8 0.7 0.6 1.4 7.2 1.6 2.0 1.8 2.3 3.2 3.5 1.3 0.6 1.5 7.3 3.0 2.3 2.4 2.5 4.0 0.9 0.9
## [29] 2.3 7.1 5.6 4.0 8.6 6.6 2.4 1.2 1.3 3.5 0.9 9.5 1.7 1.7 3.0 1.1

Different ways exist to obtain that. We can take the vector bili and then look for male patients.

pbc$bili[pbc$sex == "m"]
##  [1] 1.4 0.8 0.6 2.1 1.9 6.0 1.8 0.7 0.6 1.4 7.2 1.6 2.0 1.8 2.3 3.2 3.5 1.3 0.6 1.5 7.3 3.0 2.3 2.4 2.5 4.0 0.9 0.9
## [29] 2.3 7.1 5.6 4.0 8.6 6.6 2.4 1.2 1.3 3.5 0.9 9.5 1.7 1.7 3.0 1.1

Select the age for male patients or patients that have serum bilirubin more than 5 of the pbc data set. Here we want one of the two conditions to be satisfied, therefore we use the symbol |.

pbc[pbc$sex == "m" | pbc$bili > 5, "age"]
##   [1] 58.76523 70.07255 70.55989 56.22177 53.93018 59.95346 64.18891 55.96715 44.52019 52.02464 54.43943 44.94730
##  [13] 61.72758 33.63450 45.79877 49.13621 50.54073 65.76318 44.56947 43.89870 46.62834 46.45311 49.33881 51.92060
##  [25] 43.51814 51.94251 49.82615 47.94524 67.41136 63.26352 33.47570 46.39014 71.89322 48.46817 51.46886 41.15264
##  [37] 51.23340 52.82683 49.65640 35.15127 67.90691 45.82067 52.88980 41.94935 44.22724 62.64476 51.24983 52.28747
##  [49] 30.86379 61.80424 69.94114 69.37714 59.40862 45.76044 43.41410 53.30595 41.35524 60.95825 37.60712 35.79192
##  [61] 52.69268 56.77207 49.76318 41.16906 36.07940 61.24298 58.95140 33.18275 67.48802 41.22108 78.43943 74.52430
##  [73] 70.90760 55.39493 32.50376 65.88364 58.64750 48.93634 67.57290 50.24504 57.19644 60.53662 35.35113 55.98631
##  [85] 58.17112 33.15264 60.99932 54.00137 52.99932 56.00000 47.00068 52.99932 54.00137 48.00000 36.00000 52.00000
##  [97] 66.00137 40.99932 48.99932 48.99932 50.00137 51.00068 44.00000 40.99932 51.00068 54.00137 48.99932 68.00000
## [109] 62.00137 42.00137 54.00137

Different ways exist to obtain that. We can take the vector age and from there look for male patients or patients that have serum bilirubin more than 5.

pbc$age[pbc$sex == "m" | pbc$bili > 5]
##   [1] 58.76523 70.07255 70.55989 56.22177 53.93018 59.95346 64.18891 55.96715 44.52019 52.02464 54.43943 44.94730
##  [13] 61.72758 33.63450 45.79877 49.13621 50.54073 65.76318 44.56947 43.89870 46.62834 46.45311 49.33881 51.92060
##  [25] 43.51814 51.94251 49.82615 47.94524 67.41136 63.26352 33.47570 46.39014 71.89322 48.46817 51.46886 41.15264
##  [37] 51.23340 52.82683 49.65640 35.15127 67.90691 45.82067 52.88980 41.94935 44.22724 62.64476 51.24983 52.28747
##  [49] 30.86379 61.80424 69.94114 69.37714 59.40862 45.76044 43.41410 53.30595 41.35524 60.95825 37.60712 35.79192
##  [61] 52.69268 56.77207 49.76318 41.16906 36.07940 61.24298 58.95140 33.18275 67.48802 41.22108 78.43943 74.52430
##  [73] 70.90760 55.39493 32.50376 65.88364 58.64750 48.93634 67.57290 50.24504 57.19644 60.53662 35.35113 55.98631
##  [85] 58.17112 33.15264 60.99932 54.00137 52.99932 56.00000 47.00068 52.99932 54.00137 48.00000 36.00000 52.00000
##  [97] 66.00137 40.99932 48.99932 48.99932 50.00137 51.00068 44.00000 40.99932 51.00068 54.00137 48.99932 68.00000
## [109] 62.00137 42.00137 54.00137

Select the first measurement per patient using the pbcseq data set.
Tip: use the function duplicated().
First think of whether you want to select rows or columns. In that case we want to select rows therefore the first index should be specified.
The code duplicated(pbcseq[, "id"]) will return a logical vector indicating whether the element is duplicated or not.
We want the opposite (not dublicated). In R we can obtain the opposite by using the symbol !.

pbcseq[!duplicated(pbcseq[, "id"]), ]
##     id futime status trt      age sex day ascites hepato spiders edema bili chol albumin alk.phos   ast platelet
## 1    1    400      2   1 58.76523   f   0       1      1       1   1.0 14.5  261    2.60     1718 138.0      190
## 3    2   5169      0   1 56.44627   f   0       0      1       1   0.0  1.1  302    4.14     7395 113.5      221
## 12   3   1012      2   1 70.07255   m   0       0      0       0   0.5  1.4  176    3.48      516  96.1      151
## 16   4   1925      2   1 54.74059   f   0       0      1       1   0.5  1.8  244    2.54     6122  60.6      183
## 23   5   1505      1   0 38.10541   f   0       0      1       1   0.0  3.4  279    3.53      671 113.2      136
## 29   6   2503      2   0 66.25873   f   0       0      1       0   0.0  0.8  248    3.98      944  93.0       NA
## 35   7   2501      0   0 55.53457   f   0       0      1       0   0.0  1.0  322    4.09      824  60.5      204
## 42   8   2466      2   0 53.05681   f   0       0      0       0   0.0  0.3  280    4.00     4651  28.4      373
## 50   9   2400      2   1 42.50787   f   0       0      0       1   0.0  3.2  562    3.08     2276 144.2      251
## 57  10     51      2   0 70.55989   f   0       1      0       1   1.0 12.6  200    2.74      918 147.3      302
## 58  11   3762      2   0 53.71389   f   0       0      1       1   0.0  1.4  259    4.16     1104  79.1      258
## 70  12    304      2   0 59.13758   f   0       0      0       1   0.0  3.6  236    3.52      591  82.2       71
## 72  13   4247      0   0 45.68925   f   0       0      0       0   0.0  0.7  281    3.85     1181  88.4      244
## 84  14   1217      2   0 56.22177   m   0       1      1       0   1.0  0.8   NA    2.27      728  71.0      156
## 91  15   3584      2   1 64.64613   f   0       0      0       0   0.0  0.8  231    3.87     9010 127.7      295
## 102 16   4345      0   0 40.44353   f   0       0      0       0   0.0  0.7  204    3.66      685  72.9      198
## 115 17    769      2   0 52.18344   f   0       0      1       0   0.0  2.7  274    3.15     1533 117.8      224
## 118 18    132      2   1 53.93018   f   0       0      1       1   1.0 11.4  178    2.80      961 280.6      283
## 119 19   4901      0   1 49.56057   f   0       0      1       0   0.5  0.7  235    3.56     1881  93.0      209
## 134 20   1356      2   0 59.95346   f   0       0      1       0   0.0  5.1  374    3.51     1919 122.5      322
## 138 21   3657      2   0 64.18891   m   0       0      1       1   0.0  0.6  252    3.83      843  65.1      336
## 150 22    673      2   1 56.27652   f   0       0      0       1   0.0  3.4  271    3.63     1376 120.9      173
## 153 23    264      2   0 55.96715   f   0       1      1       1   1.0 17.4  395    2.94     6065 227.0      214
## 155 24   4079      2   1 44.52019   m   0       0      1       0   0.0  2.1  456    4.00     5719 221.9       70
## 168 25   4796      0   0 45.07324   f   0       0      0       0   0.0  0.7  298    4.10      661 107.0      324
## 180 26   1444      2   0 52.02464   f   0       0      1       1   0.0  5.2 1128    3.68     3228 165.9      421
## 186 27     77      2   0 54.43943   f   0       1      1       1   0.5 21.6  175    3.31     3697 101.9       80
## 187 28    549      2   0 44.94730   f   0       1      1       1   1.0 17.2  222    3.23     1975 189.1      144
## 190 29   5074      2   0 63.87680   f   0       0      0       0   0.0  0.7  370    3.78     5833  73.5      390
## 200 30    321      2   0 41.38535   f   0       0      1       1   0.0  3.6  260    2.54     7277 121.3      124
## 203 31   3839      2   0 41.55236   f   0       0      1       0   0.0  4.7  296    3.44     9933 206.4      195
## 215 32   5192      0   0 53.99589   f   0       0      1       0   0.5  1.8  262    3.34     7277  82.6      286
## 231 33   3170      2   0 51.28268   f   0       0      0       0   0.0  0.8  210    3.19     1592 218.6      180
## 241 34   4602      0   1 52.06023   f   0       0      0       0   0.5  0.8  364    3.70     1840 170.5      273
## 255 35   2847      2   0 48.61875   f   0       0      0       0   0.0  1.2  314    3.20    12259  72.2      431
## 259 36   4281      0   0 56.41068   f   0       0      0       0   0.0  0.3  172    3.39      558  71.3      311
## 270 37    223      2   1 61.72758   f   0       1      1       0   1.0  7.1  334    3.01     6931 180.6      102
## 272 38   3244      2   0 36.62697   f   0       0      1       1   0.0  3.3  383    3.53     1234 138.0      234
## 282 39   2297      2   1 55.39220   f   0       0      1       0   0.0  0.7  282    3.00     9067  72.2      563
## 290 40   5136      0   1 46.66940   f   0       0      0       0   0.0  1.3   NA    3.34    11047 104.5      358
## 305 41   1350      2   1 33.63450   f   0       0      1       0   0.0  6.8   NA    3.26     1215 151.9      226
## 309 42   5122      0   0 33.69473   f   0       0      1       1   0.0  2.1   NA    3.54     8778  56.8      344
## 325 43   5225      0   1 48.87064   f   0       0      0       0   0.0  1.1  361    3.64     5430  67.1      203
## 340 44   3428      2   0 37.58248   f   0       0      1       1   1.0  3.3  299    3.55     1029 119.4      199
## 351 45   4694      0   0 41.79329   f   0       0      0       0   0.0  0.6   NA    3.93     1826  71.3      474
## 360 46   2256      2   1 45.79877   f   0       0      1       0   0.0  5.7  482    2.84    11552 136.7      518
## 368 47   3245      0   0 47.42779   f   0       0      0       0   0.0  0.5  316    3.65     1716 187.6      356
## 375 48   5096      0   0 49.13621   m   0       0      0       0   0.0  1.9  259    3.70    10397 188.3      214
## 384 49    708      2   0 61.15264   f   0       0      1       0   0.0  0.8   NA    3.82      678  97.7      233
## 388 50   2598      2   1 53.50856   f   0       0      1       0   0.0  1.1  257    3.36     1080 107.0      128
## 397 51   3853      2   0 52.08761   f   0       0      0       0   0.0  0.8  276    3.60     4332  99.3      273
## 407 52   2386      2   1 50.54073   m   0       0      0       0   0.0  6.0  614    3.70     5084 206.4      362
##     protime stage
## 1      12.2     4
## 3      10.6     3
## 12     12.0     4
## 16     10.3     4
## 23     10.9     3
## 29     11.0     3
## 35      9.7     3
## 42     11.0     3
## 50     11.0     2
## 57     11.5     4
## 58     12.0     4
## 70     13.6     4
## 72     10.6     3
## 84     11.0     4
## 91     11.0     3
## 102    10.8     3
## 115    10.5     4
## 118    12.4     4
## 119    11.0     3
## 134    13.0     4
## 138    11.4     4
## 150    11.6     4
## 153    11.7     4
## 155     9.9     2
## 168    11.3     2
## 180     9.9     3
## 186    12.0     4
## 187    13.0     4
## 190    10.6     2
## 200    11.0     4
## 203    10.3     2
## 215    10.6     4
## 231    12.0     3
## 241    10.5     2
## 255    10.6     3
## 259    10.6     2
## 270    12.0     4
## 272    11.0     4
## 282    10.6     4
## 290    11.0     4
## 305    11.7     4
## 309    11.0     4
## 325    10.6     2
## 340    11.7     3
## 351    10.9     2
## 360    12.7     3
## 368     9.8     3
## 375    11.0     3
## 384    11.0     4
## 388    10.6     4
## 397    10.6     2
## 407    10.6     1
##  [ reached 'max' / getOption("max.print") -- omitted 260 rows ]

Select the last measurement per patient using the pbcseq data set.
We can use the exact same code as before, but we need to start checking for dublicates from the last measurement per patient.
The duplicated() function has an argument called fromLast for that. The code duplicated(pbcseq[, "id"], fromLast = TRUE) will return a logical vector indicating whether the element is duplicated or not starting from the last observation per patient.
We want the opposite (not dublicated). In R we can obtain the opposite by using the symbol !.

pbcseq[!duplicated(pbcseq[, "id"], fromLast = TRUE), ]
##     id futime status trt      age sex  day ascites hepato spiders edema bili chol albumin alk.phos   ast platelet
## 2    1    400      2   1 58.76523   f  192       1      1       1   1.0 21.3   NA    2.94     1612   6.2      183
## 11   2   5169      0   1 56.44627   f 3226       1      1       1   1.0  4.6  237    2.67      669  88.0      100
## 15   3   1012      2   1 70.07255   m  743       0      0       1   0.5  1.8  185    3.25      447  88.4      109
## 22   4   1925      2   1 54.74059   f 1824       1      1       1   1.0  5.3  140    1.83      623 131.8      101
## 28   5   1505      1   0 38.10541   f 1455       0      1       1   0.5 19.0   91    2.09      377 156.0      101
## 34   6   2503      2   0 66.25873   f 2453      NA     NA      NA   0.0  0.7   NA    4.20       NA  60.0       NA
## 41   7   2501      0   0 55.53457   f 2262       0      0       0   0.0  1.4  260    3.59      448  54.0      114
## 49   8   2466      2   0 53.05681   f 2218       1      1       0   0.5  5.4  341    2.47     1203 120.9      238
## 56   9   2400      2   1 42.50787   f 2278       1      1       0   1.0 14.8  418    2.41     2268 221.7      161
## 57  10     51      2   0 70.55989   f    0       1      0       1   1.0 12.6  200    2.74      918 147.3      302
## 69  11   3762      2   0 53.71389   f 3694       1      1       1   1.0  9.6   NA    2.31      660 121.0      132
## 71  12    304      2   0 59.13758   f  180       1      0       1   0.5 10.0   NA    3.00      737 114.7       98
## 83  13   4247      0   0 45.68925   f 4243       0      0       0   0.0  1.2  221    4.26      751  72.0      176
## 90  14   1217      2   0 56.22177   m 1204      NA     NA      NA   0.5  0.9   NA    2.39       NA  71.0       NA
## 101 15   3584      2   1 64.64613   f 3537      NA     NA      NA   1.0 11.4   NA    2.40       NA 114.0       NA
## 114 16   4345      0   0 40.44353   f 4054       0      0       0   0.0  0.9  306    3.51     1004 120.0      231
## 117 17    769      2   0 52.18344   f  673       1      1       1   1.0 13.6  246    2.44      539 141.0      322
## 118 18    132      2   1 53.93018   f    0       0      1       1   1.0 11.4  178    2.80      961 280.6      283
## 133 19   4901      0   1 49.56057   f 4696       0      1       0   0.0  0.8  197    3.30      798  59.0      162
## 137 20   1356      2   0 59.95346   f 1344      NA     NA      NA   0.0 32.0   NA    2.40       NA 156.6       NA
## 149 21   3657      2   0 64.18891   m 3513      NA     NA      NA   1.0  7.8   NA    2.40       NA  73.0       NA
## 152 22    673      2   1 56.27652   f  360       0      0       1   0.5  2.4   NA    2.37     1756 148.8      182
## 154 23    264      2   0 55.96715   f  182       1      1       1   1.0 19.5   NA    2.45      949 167.4      252
## 167 24   4079      2   1 44.52019   m 4034       1      1       0   0.5 16.8  126    1.56      836 229.0      252
## 179 25   4796      0   0 45.07324   f 3637       0      0       1   0.0  0.7   NA    2.83     1560  75.0      148
## 185 26   1444      2   0 52.02464   f 1434      NA     NA      NA   1.0 16.2   NA    1.81       NA 241.0       NA
## 186 27     77      2   0 54.43943   f    0       1      1       1   0.5 21.6  175    3.31     3697 101.9       80
## 189 28    549      2   0 44.94730   f  375       1      1       1   1.0 21.3   NA    2.59     1257 162.8      143
## 199 29   5074      2   0 63.87680   f 4333       1      1       1   1.0  1.4  242    2.47      830  73.0      163
## 202 30    321      2   0 41.38535   f  299       1      1       1   0.5 36.0   NA    2.90     1904 300.7      116
## 214 31   3839      2   0 41.55236   f 3590       0      0       1   0.0 12.0  219    2.89      995 211.0      120
## 230 32   5192      0   0 53.99589   f 5152       0      0       0   0.5  0.9  200    3.32      866  61.0      132
## 240 33   3170      2   0 51.28268   f 3004       0      0       0   0.0  1.3   NA    3.37     1498  85.0      147
## 254 34   4602      0   1 52.06023   f 4457       0      1       0   0.5  1.0  329    2.99      661  97.0      193
## 258 35   2847      2   0 48.61875   f  743       0      1       0   0.0  4.2  271    3.70     2780 117.8      473
## 269 36   4281      0   0 56.41068   f 3956       0      1       1   0.0  0.7  236    2.76      761  43.0      220
## 271 37    223      2   1 61.72758   f  144       1      1       1   1.0 13.5   NA    2.38     1737 218.6      114
## 281 38   3244      2   0 36.62697   f 3209      NA     NA      NA   0.5  5.6   NA    1.90       NA  69.0       NA
## 289 39   2297      2   1 55.39220   f 2247       0      1       0   0.5 10.5  275    2.60     1034 258.9      556
## 304 40   5136      0   1 46.66940   f 4845       0      1       1   1.0 18.0  192    2.88     1195 144.0      222
## 308 41   1350      2   1 33.63450   f 1151       0      1       1   1.0 16.2   NA    2.73     1240 145.7      109
## 324 42   5122      0   0 33.69473   f 5118       0      1       1   0.5 13.0  261    2.34     2337 217.0      152
## 339 43   5225      0   1 48.87064   f 4877       1      1       0   0.0  1.1   NA    3.10      802  53.0       70
## 350 44   3428      2   0 37.58248   f 3414      NA     NA      NA   0.5 29.4   NA    1.90       NA 204.0       NA
## 359 45   4694      0   0 41.79329   f 3219       0      0       1   0.0  0.8  430    3.15       73  70.0      257
## 367 46   2256      2   1 45.79877   f 2174       0      1       1   1.0 16.5   NA    2.75      881 266.6      110
## 374 47   3245      0   0 47.42779   f 2311       0      1       0   0.0  0.9  311    3.26     1420  99.0      151
## 383 48   5096      0   0 49.13621   m 2517       0      0       0   0.0  1.4  235    4.15     1258 150.4      154
## 387 49    708      2   0 61.15264   f  525      NA     NA      NA   0.0  3.1   NA    2.31       NA 170.5       NA
## 396 50   2598      2   1 53.50856   f 2561       1      0       1   1.0  5.8  266    2.86      879 187.0       82
## 406 51   3853      2   0 52.08761   f 3529       0      1       1   1.0 16.9  197    2.82      996 134.0      198
## 415 52   2386      2   1 50.54073   m 2380       1      1       1   1.0 27.1   NA    2.90      866 210.8      108
##     protime stage
## 2      11.2     4
## 11     11.5     3
## 15     13.3     4
## 22     17.0     4
## 28     13.9     4
## 34     10.5     4
## 41     12.0     3
## 49     11.3     4
## 56     13.0     3
## 57     11.5     4
## 69     15.1     4
## 71     14.1     4
## 83     10.2     4
## 90     11.9     4
## 101    13.0     4
## 114    11.8     4
## 117    12.1     4
## 118    12.4     4
## 133    11.9     4
## 137    19.0     4
## 149    13.8     4
## 152    11.7     4
## 154    21.0     4
## 167    11.6     3
## 179    10.7     4
## 185    12.5     4
## 186    12.0     4
## 189    13.0     4
## 199    12.4     4
## 202    13.1     4
## 214    12.6     4
## 230    11.1     4
## 240    10.7     3
## 254    11.6     3
## 258    10.5     4
## 269    12.3     4
## 271    13.3     4
## 281    11.9     4
## 289    10.3     4
## 304    12.7     4
## 308    13.6     4
## 324    12.6     4
## 339    12.2     4
## 350    16.1     4
## 359     9.8     3
## 367    13.4     4
## 374    11.5     4
## 383     9.9     2
## 387    11.3     4
## 396    15.1     3
## 406    13.5     3
## 415    13.5     4
##  [ reached 'max' / getOption("max.print") -- omitted 260 rows ]

Select all male patients that died of the pbc data set. Here we want both conditions to be satisfied, therefore we use the symbol &.

pbc[pbc$sex == "m" & pbc$status == 2, ]      
##      id time status trt      age sex ascites hepato spiders edema bili chol albumin copper alk.phos    ast trig
## 3     3 1012      2   1 70.07255   m       0      0       0   0.5  1.4  176    3.48    210    516.0  96.10   55
## 14   14 1217      2   2 56.22177   m       1      1       0   1.0  0.8   NA    2.27     43    728.0  71.00   NA
## 24   24 4079      2   1 44.52019   m       0      1       0   0.0  2.1  456    4.00    124   5719.0 221.88  230
## 52   52 2386      2   1 50.54073   m       0      0       0   0.0  6.0  614    3.70    158   5084.4 206.40   93
## 55   55 1360      2   1 65.76318   m       0      0       0   0.0  1.8  416    3.94    121  10165.0  79.98  219
## 66   66 4191      2   1 46.45311   m       0      1       0   0.0  1.4  427    3.70    105   1909.0 182.90  171
## 80   80  890      2   2 67.41136   m       0      1       0   0.0  7.2  247    3.72    269   1303.0 176.70   91
## 90   90 2689      2   1 33.47570   m       0      0       0   0.0  1.6  660    4.22     94   1857.0 151.90  155
## 97   97  611      2   2 71.89322   m       0      1       0   0.5  2.0  420    3.26     62   3196.0  77.50   91
## 100 100  552      2   2 51.46886   m       0      1       0   0.0  2.3  178    3.00    145    746.0 178.25  122
## 114 114 3395      2   2 52.82683   m       0      0       0   0.0  3.2  259    4.30    208   1040.0 110.05   78
## 121 121  191      2   2 67.90691   m       1      1       0   1.0  1.3  151    3.08     73   1112.0  46.50   49
## 133 133 2796      2   2 62.64476   m       0      0       0   0.0  1.5  331    3.95     13    577.0 128.65   99
## 138 138 1297      2   1 51.24983   m       0      1       0   0.0  7.3  426    3.93    262   2424.0 145.70  218
## 149 149  762      2   1 61.80424   m       0      1       1   0.5  3.0  257    3.79    290   1664.0 102.30  112
## 152 152 1152      2   1 69.94114   m       0      1       0   0.0  2.3  586    3.01    243   2276.0 114.70  126
## 154 154  140      2   1 69.37714   m       0      0       1   1.0  2.4  168    2.56    225   1056.0 120.90   75
## 159 159 1536      2   2 45.76044   m       0      0       0   0.0  2.5  317    3.46    217    714.0 130.20  140
## 165 165 1077      2   1 53.30595   m       0      1       0   0.0  4.0  196    3.45     80   2496.0 133.30  142
## 167 167 1682      2   1 60.95825   m       0      1       0   0.0  0.9  376    3.86    200   1015.0  83.70  154
## 227 227  999      2   1 58.95140   m       0      0       0   0.0  2.3  316    3.35    172   1601.0 179.80   63
## 289 289  799      2   1 67.57290   m       0      1       0   0.5  4.0  416    3.99    177    960.0  86.00  242
## 330 330 1746      2  NA 54.00137   m      NA     NA      NA   0.0  3.5   NA    3.63     NA       NA     NA   NA
## 376 376 1478      2  NA 44.00000   m      NA     NA      NA   0.0  9.5   NA    3.63     NA       NA     NA   NA
##     platelet protime stage
## 3        151    12.0     4
## 14       156    11.0     4
## 24        70     9.9     2
## 52       362    10.6     1
## 55       213    11.0     3
## 66       123    11.0     3
## 80       360    11.2     4
## 90       337    11.0     2
## 97       344    11.4     3
## 100      119    12.0     4
## 114      268    11.7     3
## 121      213    13.2     4
## 133      165    10.1     4
## 138      252    10.5     3
## 149      140     9.9     4
## 152      339    10.9     3
## 154      108    14.1     3
## 159      207    10.1     3
## 165      212    11.3     4
## 167      238    10.3     4
## 227      394     9.7     2
## 289      269     9.8     2
## 330      325    10.3     2
## 376      292    10.2     3

Select male patients or patients that died of the pbc data set. Here we want one of the two conditions to be satisfied, therefore we use the symbol |. Use to function head() if you do not want to print the full data set.

head(pbc[pbc$sex == "m" | pbc$status == 2, ])
##   id time status trt      age sex ascites hepato spiders edema bili chol albumin copper alk.phos    ast trig
## 1  1  400      2   1 58.76523   f       1      1       1   1.0 14.5  261    2.60    156   1718.0 137.95  172
## 3  3 1012      2   1 70.07255   m       0      0       0   0.5  1.4  176    3.48    210    516.0  96.10   55
## 4  4 1925      2   1 54.74059   f       0      1       1   0.5  1.8  244    2.54     64   6121.8  60.63   92
## 6  6 2503      2   2 66.25873   f       0      1       0   0.0  0.8  248    3.98     50    944.0  93.00   63
## 8  8 2466      2   2 53.05681   f       0      0       0   0.0  0.3  280    4.00     52   4651.2  28.38  189
## 9  9 2400      2   1 42.50787   f       0      0       1   0.0  3.2  562    3.08     79   2276.0 144.15   88
##   platelet protime stage
## 1      190    12.2     4
## 3      151    12.0     4
## 4      183    10.3     4
## 6       NA    11.0     3
## 8      373    11.0     3
## 9      251    11.0     2

Select the serum bilirubin measurements only for female patients of the pbc data set.

pbc[pbc$sex == "f", "bili"]   
##   [1] 14.5  1.1  1.8  3.4  0.8  1.0  0.3  3.2 12.6  1.4  3.6  0.7  0.8  0.7  2.7 11.4  0.7  5.1  3.4 17.4  0.7  5.2
##  [23] 21.6 17.2  0.7  3.6  4.7  1.8  0.8  0.8  1.2  0.3  7.1  3.3  0.7  1.3  6.8  2.1  1.1  3.3  0.6  5.7  0.5  0.8
##  [45]  1.1  0.8  2.6  1.3  1.1  2.3  0.8  0.9  1.3 22.5  2.1  1.2  1.1  0.7 20.0  0.6  1.2  0.5  0.7  8.4 17.1 12.2
##  [67]  6.6  6.3  0.8 14.4  4.5  1.3  0.4  2.1  5.0  1.1  0.6  2.0  5.0  1.4  1.3  3.2 17.4  1.0  1.0  0.9  0.9  2.5
##  [89]  1.1  1.1  2.1  0.6  0.4  0.5  1.9  5.5  2.0  6.7  0.7  3.0  6.5  3.5  0.6  0.6  5.1  1.3  1.2  0.5 16.2  0.9
## [111] 17.4  2.8  1.9  0.7  0.4  0.8  1.1  1.1  1.1  0.9  1.0  2.9 28.0  0.7  1.2  1.2  7.2  1.0  0.9  0.5  0.6 25.5
## [133]  0.6  3.4  0.6  2.3  3.2  0.3  8.5  5.7  0.4  1.3  1.2  0.5  1.3  3.0  0.5  0.8  3.2  0.9  0.6  1.8  4.7  1.4
## [155]  0.6  0.5 11.0  0.8  2.0 14.0  0.7  1.3  2.3 24.5  0.9 10.8  1.5  3.7  1.4  0.6  0.7  2.1  4.7  0.6  0.5  0.5
## [177]  0.7  2.5  0.6  0.6  3.9  0.7  1.3  1.2  0.5  0.9  5.9  0.5 11.4  0.5  1.6  3.8  0.9  4.5 14.1  1.0  0.7  0.5
## [199]  0.7  4.5  3.3  3.4  0.4  0.9  0.9 13.0  1.5  1.6  0.6  0.8  0.4  4.4  1.9  8.0  3.9  0.6  2.1  6.1  0.8  1.3
## [221]  0.6  0.5  1.1  3.1  0.7  1.1  0.5  1.1  3.1  3.2  2.8  1.1  3.4  3.5  0.5  6.6  6.4  3.6  1.0  1.0  0.5  2.2
## [243]  1.6  2.2  1.0  1.0  5.6  0.5  1.6 17.9  1.3  1.1  1.3  0.8  2.0  6.4  8.7  1.4  3.2  8.5  0.8  1.1  2.4  5.2
## [265]  1.0  0.7  1.0  0.5  2.9  0.6  0.8  0.4  0.4  1.7  2.0  6.4  0.7  1.4  0.7  0.7  0.8  0.7  5.0  0.4  1.1  0.6
## [287]  0.6  1.8  1.5  1.2  1.0  0.7  3.1 12.6  2.8  7.1  0.6  2.1  1.8 16.0  0.6  5.4  9.0  0.9 11.1  8.9  0.5  0.6
## [309]  3.4  1.4  2.1 15.0  0.6  1.3  1.3  1.6  2.2  3.0  0.8  0.8  1.8  5.5 18.0  0.6  2.7  0.9  1.3  1.1 13.8  4.4
## [331] 16.0  7.3  0.6  0.7  0.7  1.7  2.2  1.8  3.3  2.9 14.0  0.8  1.3  0.7 13.6  0.9  0.7  1.2  0.4  0.7  2.0  1.4
## [353]  1.6  0.5  7.3  8.1  0.5  4.2  0.8  2.5  4.6  1.0  4.5  1.9  0.7  1.5  0.6  1.0  0.7  1.2  0.9  1.6  0.8  0.7

Select all rows of the pbc data set where the serum bilirubin measurements are smaller that 10.

head(pbc[pbc$bili < 10, ])
##   id time status trt      age sex ascites hepato spiders edema bili chol albumin copper alk.phos    ast trig
## 2  2 4500      0   1 56.44627   f       0      1       1   0.0  1.1  302    4.14     54   7394.8 113.52   88
## 3  3 1012      2   1 70.07255   m       0      0       0   0.5  1.4  176    3.48    210    516.0  96.10   55
## 4  4 1925      2   1 54.74059   f       0      1       1   0.5  1.8  244    2.54     64   6121.8  60.63   92
## 5  5 1504      1   2 38.10541   f       0      1       1   0.0  3.4  279    3.53    143    671.0 113.15   72
## 6  6 2503      2   2 66.25873   f       0      1       0   0.0  0.8  248    3.98     50    944.0  93.00   63
## 7  7 1832      0   2 55.53457   f       0      1       0   0.0  1.0  322    4.09     52    824.0  60.45  213
##   platelet protime stage
## 2      221    10.6     3
## 3      151    12.0     4
## 4      183    10.3     4
## 5      136    10.9     3
## 6       NA    11.0     3
## 7      204     9.7     3

Array indexing

Create an array

ar <- array(data = 1:19, dim = c(3, 3, 2))
ar 
## , , 1
## 
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,]   10   13   16
## [2,]   11   14   17
## [3,]   12   15   18

Select the 2nd row of each matrix

ar[2, , ]
##      [,1] [,2]
## [1,]    2   11
## [2,]    5   14
## [3,]    8   17

Select the 2nd column of each matrix

ar[, 2, ]
##      [,1] [,2]
## [1,]    4   13
## [2,]    5   14
## [3,]    6   15

Select the 2nd row and column of the first matrix

ar[2, 2, 1]
## [1] 5

List indexing

Create a list with 3 elements:

  • 1st element: all pbc$id
  • 2nd element: pbc$bili for males
  • 3rd element: pbc$age > 30
myList <- list(pbc$id, pbc$bili[pbc$sex == "m"], pbc$age[pbc$age > 30])

Select the second element (the output should be a list).

myList[2]
## [[1]]
##  [1] 1.4 0.8 0.6 2.1 1.9 6.0 1.8 0.7 0.6 1.4 7.2 1.6 2.0 1.8 2.3 3.2 3.5 1.3 0.6 1.5 7.3 3.0 2.3 2.4 2.5 4.0 0.9 0.9
## [29] 2.3 7.1 5.6 4.0 8.6 6.6 2.4 1.2 1.3 3.5 0.9 9.5 1.7 1.7 3.0 1.1

Select the third element (the output should be a vector).

myList[[3]]
##   [1] 58.76523 56.44627 70.07255 54.74059 38.10541 66.25873 55.53457 53.05681 42.50787 70.55989 53.71389 59.13758
##  [13] 45.68925 56.22177 64.64613 40.44353 52.18344 53.93018 49.56057 59.95346 64.18891 56.27652 55.96715 44.52019
##  [25] 45.07324 52.02464 54.43943 44.94730 63.87680 41.38535 41.55236 53.99589 51.28268 52.06023 48.61875 56.41068
##  [37] 61.72758 36.62697 55.39220 46.66940 33.63450 33.69473 48.87064 37.58248 41.79329 45.79877 47.42779 49.13621
##  [49] 61.15264 53.50856 52.08761 50.54073 67.40862 39.19781 65.76318 33.61807 53.57153 44.56947 40.39425 58.38193
##  [61] 43.89870 60.70637 46.62834 62.90760 40.20260 46.45311 51.28816 32.61328 49.33881 56.39973 48.84600 32.49281
##  [73] 38.49418 51.92060 43.51814 51.94251 49.82615 47.94524 46.51608 67.41136 63.26352 67.31006 56.01369 55.83025
##  [85] 47.21697 52.75838 37.27858 41.39357 52.44353 33.47570 45.60712 76.70910 36.53388 53.91650 46.39014 48.84600
##  [97] 71.89322 48.46817 51.46886 44.95003 56.56947 48.96372 43.01711 34.03970 68.50924 62.52156 50.35729 44.06297
## [109] 38.91034 41.15264 55.45791 51.23340 52.82683 42.63929 61.07050 49.65640 48.85421 54.25599 35.15127 67.90691
## [121] 55.43600 45.82067 52.88980 47.18138 53.59890 44.10404 41.94935 63.61396 44.22724 62.00137 40.55305 62.64476
## [133] 42.33539 42.96783 55.96167 62.86105 51.24983 46.76249 54.07529 47.03628 55.72621 46.10267 52.28747 51.20055
## [145] 33.86448 75.01164 30.86379 61.80424 34.98700 55.04175 69.94114 49.60438 69.37714 43.55647 59.40862 48.75838
## [157] 36.49281 45.76044 57.37166 42.74333 58.81725 53.49760 43.41410 53.30595 41.35524 60.95825 47.75359 35.49076
## [169] 48.66256 52.66804 49.86995 30.27515 55.56742 52.15332 41.60986 55.45243 70.00411 43.94251 42.56810 44.56947
## [181] 56.94456 40.26010 37.60712 48.36140 70.83641 35.79192 62.62286 50.64750 54.52704 52.69268 52.72005 56.77207
## [193] 44.39699 57.04038 44.62697 35.79740 40.71732 32.23272 41.09240 61.63997 37.05681 62.57906 48.97741 61.99042
## [205] 72.77207 61.29500 52.62423 49.76318 52.91444 47.26352 50.20397 69.34702 41.16906 59.16496 36.07940 34.59548
## [217] 42.71321 63.63039 56.62971 46.26420 61.24298 38.62012 38.77070 56.69541 58.95140 36.92266 62.41478 34.60917
## [229] 58.33539 50.18207 42.68583 34.37919 33.18275 38.38193 59.76181 66.41205 46.78987 56.07940 41.37440 64.57221
## [241] 67.48802 44.82957 45.77139 32.95003 41.22108 55.41684 47.98084 40.79124 56.97467 68.46270 78.43943 39.85763
## [253] 35.31006 31.44422 58.26420 51.48802 59.96988 74.52430 52.36413 42.78713 34.87474 44.13963 46.38193 56.30938
## [265] 70.90760 55.39493 45.08419 50.47228 38.39836 47.41958 47.98084 38.31622 50.10815 35.08830 32.50376 56.15332
## [277] 46.15469 65.88364 33.94387 62.86105 48.56400 46.34908 38.85284 58.64750 48.93634 67.57290 65.98494 40.90075
## [289] 50.24504 57.19644 60.53662 35.35113 31.38125 55.98631 52.72553 38.09172 58.17112 45.21013 37.79877 60.65982
## [301] 35.53457 43.06639 56.39151 30.57358 61.18275 58.29979 62.33265 37.99863 33.15264 60.00000 64.99932 54.00137
## [313] 75.00068 62.00137 43.00068 46.00137 44.00000 60.99932 64.00000 40.00000 63.00068 34.00137 52.00000 48.99932
## [325] 54.00137 63.00068 54.00137 46.00137 52.99932 56.00000 56.00000 55.00068 64.99932 56.00000 47.00068 60.00000
## [337] 52.99932 54.00137 50.00137 48.00000 36.00000 48.00000 70.00137 51.00068 52.00000 54.00137 48.00000 66.00137
## [349] 52.99932 62.00137 59.00068 39.00068 67.00068 58.00137 64.00000 46.00137 64.00000 40.99932 48.99932 44.00000
## [361] 59.00068 63.00068 60.99932 64.00000 48.99932 42.00137 50.00137 51.00068 36.99932 62.00137 51.00068 52.00000
## [373] 44.00000 32.99932 60.00000 63.00068 32.99932 40.99932 51.00068 36.99932 59.00068 55.00068 54.00137 48.99932
## [385] 40.00000 67.00068 68.00000 40.99932 68.99932 52.00000 56.99932 36.00000 50.00137 64.00000 62.00137 42.00137
## [397] 44.00000 68.99932 52.00000 66.00137 40.00000 52.00000 46.00137 54.00137 51.00068 43.00068 39.00068 51.00068
## [409] 67.00068 35.00068 67.00068 39.00068 56.99932 58.00137 52.99932

Select the third element (the output should be a vector).
Then, from the third element, select the elements that are smaller than 20. Tips: do not try doing everything in one step.

newData <- myList[[3]]
newData[newData < 20]
## numeric(0)

Create a list with 3 elements and give them names:

  • 1st element - all_id: all pbc$id
  • 2nd element - bili_male: pbc$bili for males
  • 3rd element - young: pbc$age < 30
myList <- list(all_id = pbc$id, bili_male = pbc$bili[pbc$sex == "m"], young = pbc$age[pbc$age < 30])

Select all_id by name indexing.

myList$all_id
##   [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  27  28
##  [29]  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56
##  [57]  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84
##  [85]  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112
## [113] 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
## [141] 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
## [169] 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
## [197] 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
## [225] 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
## [253] 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
## [281] 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
## [309] 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
## [337] 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
## [365] 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
## [393] 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418