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

Vectors

Examples of vectors can be seen below.

pbc$age
##   [1] 58.76523 56.44627 70.07255 54.74059 38.10541 66.25873 55.53457 53.05681 42.50787 70.55989 53.71389
##  [12] 59.13758 45.68925 56.22177 64.64613 40.44353 52.18344 53.93018 49.56057 59.95346 64.18891 56.27652
##  [23] 55.96715 44.52019 45.07324 52.02464 54.43943 44.94730 63.87680 41.38535 41.55236 53.99589 51.28268
##  [34] 52.06023 48.61875 56.41068 61.72758 36.62697 55.39220 46.66940 33.63450 33.69473 48.87064 37.58248
##  [45] 41.79329 45.79877 47.42779 49.13621 61.15264 53.50856 52.08761 50.54073 67.40862 39.19781 65.76318
##  [56] 33.61807 53.57153 44.56947 40.39425 58.38193 43.89870 60.70637 46.62834 62.90760 40.20260 46.45311
##  [67] 51.28816 32.61328 49.33881 56.39973 48.84600 32.49281 38.49418 51.92060 43.51814 51.94251 49.82615
##  [78] 47.94524 46.51608 67.41136 63.26352 67.31006 56.01369 55.83025 47.21697 52.75838 37.27858 41.39357
##  [89] 52.44353 33.47570 45.60712 76.70910 36.53388 53.91650 46.39014 48.84600 71.89322 28.88433 48.46817
## [100] 51.46886 44.95003 56.56947 48.96372 43.01711 34.03970 68.50924 62.52156 50.35729 44.06297 38.91034
## [111] 41.15264 55.45791 51.23340 52.82683 42.63929 61.07050 49.65640 48.85421 54.25599 35.15127 67.90691
## [122] 55.43600 45.82067 52.88980 47.18138 53.59890 44.10404 41.94935 63.61396 44.22724 62.00137 40.55305
## [133] 62.64476 42.33539 42.96783 55.96167 62.86105 51.24983 46.76249 54.07529 47.03628 55.72621 46.10267
## [144] 52.28747 51.20055 33.86448 75.01164 30.86379 61.80424 34.98700 55.04175 69.94114 49.60438 69.37714
## [155] 43.55647 59.40862 48.75838 36.49281 45.76044 57.37166 42.74333 58.81725 53.49760 43.41410 53.30595
## [166] 41.35524 60.95825 47.75359 35.49076 48.66256 52.66804 49.86995 30.27515 55.56742 52.15332 41.60986
## [177] 55.45243 70.00411 43.94251 42.56810 44.56947 56.94456 40.26010 37.60712 48.36140 70.83641 35.79192
## [188] 62.62286 50.64750 54.52704 52.69268 52.72005 56.77207 44.39699 29.55510 57.04038 44.62697 35.79740
## [199] 40.71732 32.23272 41.09240 61.63997 37.05681 62.57906 48.97741 61.99042 72.77207 61.29500 52.62423
## [210] 49.76318 52.91444 47.26352 50.20397 69.34702 41.16906 59.16496 36.07940 34.59548 42.71321 63.63039
## [221] 56.62971 46.26420 61.24298 38.62012 38.77070 56.69541 58.95140 36.92266 62.41478 34.60917 58.33539
## [232] 50.18207 42.68583 34.37919 33.18275 38.38193 59.76181 66.41205 46.78987 56.07940 41.37440 64.57221
## [243] 67.48802 44.82957 45.77139 32.95003 41.22108 55.41684 47.98084 40.79124 56.97467 68.46270 78.43943
## [254] 39.85763 35.31006 31.44422 58.26420 51.48802 59.96988 74.52430 52.36413 42.78713 34.87474 44.13963
## [265] 46.38193 56.30938 70.90760 55.39493 45.08419 26.27789 50.47228 38.39836 47.41958 47.98084 38.31622
## [276] 50.10815 35.08830 32.50376 56.15332 46.15469 65.88364 33.94387 62.86105 48.56400 46.34908 38.85284
## [287] 58.64750 48.93634 67.57290 65.98494 40.90075 50.24504 57.19644 60.53662 35.35113 31.38125 55.98631
## [298] 52.72553 38.09172 58.17112 45.21013 37.79877 60.65982 35.53457 43.06639 56.39151 30.57358 61.18275
## [309] 58.29979 62.33265 37.99863 33.15264 60.00000 64.99932 54.00137 75.00068 62.00137 43.00068 46.00137
## [320] 44.00000 60.99932 64.00000 40.00000 63.00068 34.00137 52.00000 48.99932 54.00137 63.00068 54.00137
## [331] 46.00137 52.99932 56.00000 56.00000 55.00068 64.99932 56.00000 47.00068 60.00000 52.99932 54.00137
## [342] 50.00137 48.00000 36.00000 48.00000 70.00137 51.00068 52.00000 54.00137 48.00000 66.00137 52.99932
## [353] 62.00137 59.00068 39.00068 67.00068 58.00137 64.00000 46.00137 64.00000 40.99932 48.99932 44.00000
## [364] 59.00068 63.00068 60.99932 64.00000 48.99932 42.00137 50.00137 51.00068 36.99932 62.00137 51.00068
## [375] 52.00000 44.00000 32.99932 60.00000 63.00068 32.99932 40.99932 51.00068 36.99932 59.00068 55.00068
## [386] 54.00137 48.99932 40.00000 67.00068 68.00000 40.99932 68.99932 52.00000 56.99932 36.00000 50.00137
## [397] 64.00000 62.00137 42.00137 44.00000 68.99932 52.00000 66.00137 40.00000 52.00000 46.00137 54.00137
## [408] 51.00068 43.00068 39.00068 51.00068 67.00068 35.00068 67.00068 39.00068 56.99932 58.00137 52.99932
pbc$time
##   [1]  400 4500 1012 1925 1504 2503 1832 2466 2400   51 3762  304 3577 1217 3584 3672  769  131 4232 1356
##  [21] 3445  673  264 4079 4127 1444   77  549 4509  321 3839 4523 3170 3933 2847 3611  223 3244 2297 4467
##  [41] 1350 4453 4556 3428 4025 2256 2576 4427  708 2598 3853 2386 1000 1434 1360 1847 3282 4459 2224 4365
##  [61] 4256 3090  859 1487 3992 4191 2769 4039 1170 3458 4196 4184 4190 1827 1191   71  326 1690 3707  890
##  [81] 2540 3574 4050 4032 3358 1657  198 2452 1741 2689  460  388 3913  750  130 3850  611 3823 3820  552
## [101] 3581 3099  110 3086 3092 3222 3388 2583 2504 2105 2350 3445  980 3395 3422 3336 1083 2288  515 2033
## [121]  191 3297  971 3069 2468  824 3255 1037 3239 1413  850 2944 2796 3149 3150 3098 2990 1297 2106 3059
## [141] 3050 2419  786  943 2976 2615 2995 1427  762 2891 2870 1152 2863  140 2666  853 2835 2475 1536 2772
## [161] 2797  186 2055  264 1077 2721 1682 2713 1212 2692 2574 2301 2657 2644 2624 1492 2609 2580 2573 2563
## [181] 2556 2555 2241  974 2527 1576  733 2332 2456 2504  216 2443  797 2449 2330 2363 2365 2357 1592 2318
## [201] 2294 2272 2221 2090 2081 2255 2171  904 2216 2224 2195 2176 2178 1786 1080 2168  790 2170 2157 1235
## [221] 2050  597  334 1945 2022 1978  999 1967  348 1979 1165 1951 1932 1776 1882 1908 1882 1874  694 1831
## [241]  837 1810  930 1690 1790 1435  732 1785 1783 1769 1457 1770 1765  737 1735 1701 1614 1702 1615 1656
## [261] 1677 1666 1301 1542 1084 1614  179 1191 1363 1568 1569 1525 1558 1447 1349 1481 1434 1420 1433 1412
## [281]   41 1455 1030 1418 1401 1408 1234 1067  799 1363  901 1329 1320 1302  877 1321  533 1300 1293  207
## [301] 1295 1271 1250 1230 1216 1216 1149 1153  994  939  839  788 4062 3561 2844 2071 3030 1680   41 2403
## [321] 1170 2011 3523 3468 4795 1236 4214 2111 1462 1746   94  785 1518  466 3527 2635 2286  791 3492 3495
## [341]  111 3231  625 3157 3021  559 2812 2834 2855  662  727 2716 2698  990 2338 1616 2563 2537 2534  778
## [361]  617 2267 2249  359 1925  249 2202   43 1197 1095  489 2149 2103 1980 1347 1478 1987 1168  597 1725
## [381] 1899  221 1022 1639 1635 1654 1653 1560 1581 1419 1443 1368  193 1367 1329 1343 1328 1375 1260 1223
## [401]  935  943 1141 1092 1150  703 1129 1086 1067 1072 1119 1097  989  681 1103 1055  691  976
  • Create vectors with continuous data.
vec1 <- c(1:100)
vec2 <- c(2, 32, 14, 23, 54, 13, 45)
  • Create a vector with categorical data.
vec3 <- c("male", "female", "female", "male", "female", "female")
  • Create a vector with logical data.
vec4 <- c(TRUE, TRUE, FALSE)

Matrices

  • Create a matrix using the time variable/vector from the pbc data set.
matrix(data = pbc$time[1:9], nrow = 3, ncol = 3)
##      [,1] [,2] [,3]
## [1,]  400 1925 1832
## [2,] 4500 1504 2466
## [3,] 1012 2503 2400

Useful functions to easily combine columns or rows are the cbind() and rbind() functions.
We can combine multiple columns as shown below.

age_time <- cbind(pbc$age, pbc$time)

We can combine multiple rows as shown below.

age_time <- rbind(pbc$age, pbc$time)

TAKE CARE when using these functions.

Columns_com <- cbind(c(1:4), c(1:8))

Since the first column has shorter length, R is repeating the numbers.  
* Create other matrices.
Assume a vector with the elements 1 to 4 and assume 2 rows and 2 columns.
By default the matrix will be filled in by column.

matrix(data = 1:4, nrow = 2, ncol = 2)
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4

Fill matrix in by row.

matrix(data = 1:4, nrow = 2, ncol = 2, byrow = TRUE)
##      [,1] [,2]
## [1,]    1    2
## [2,]    3    4

Arrays

  • Create an array with 3 rows, 3 columns and 2 matrices.
array(data = 1:18, dim = c(3, 3, 2))
## , , 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
  • Create an array with 6 rows, 2 columns and 3 matrices.
array(data = 1:12, dim = c(6, 2, 3))
## , , 1
## 
##      [,1] [,2]
## [1,]    1    7
## [2,]    2    8
## [3,]    3    9
## [4,]    4   10
## [5,]    5   11
## [6,]    6   12
## 
## , , 2
## 
##      [,1] [,2]
## [1,]    1    7
## [2,]    2    8
## [3,]    3    9
## [4,]    4   10
## [5,]    5   11
## [6,]    6   12
## 
## , , 3
## 
##      [,1] [,2]
## [1,]    1    7
## [2,]    2    8
## [3,]    3    9
## [4,]    4   10
## [5,]    5   11
## [6,]    6   12

Notice that all matrices are the same. R is circulating the elements 1 to 12.

Data frames

  • Create a data frame using the pbc data set.
age_time_sex <- data.frame(pbc$age, pbc$time, pbc$sex)

Add names to the columns of the data frame.

age_time_sex <- data.frame(age = pbc$age, time = pbc$time, sex = pbc$sex)
  • Create other data frames.
DF <- data.frame(Age = c(30, 20, 50),
                 Sex = c("male", "female", "female"),
                 Drug = c("yes", "yes", "no"))
DF
DF <- data.frame(Age = runif(30, 1, 80),
                 Sex = sample(1:2, 30, replace = TRUE),
                 Drug = sample(1:3, 30, replace = TRUE))
DF

Lists

  • Create a list using the pbc data set.
list_pbc <- list(pbc$id, pbc$time)
  • Create other lists.
myList <- list(c(1:20),c("male", "female")) 
myList
## [[1]]
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
## 
## [[2]]
## [1] "male"   "female"

Now add the names x to the first element and sex to the second element.

myList <- list(x = c(1:20), sex = c("male", "female")) 
myList
## $x
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
## 
## $sex
## [1] "male"   "female"

Another example.

mylist = list(names = c("Jack","Mary"), child.ages = c(4,7,9,10,11))
mylist
## $names
## [1] "Jack" "Mary"
## 
## $child.ages
## [1]  4  7  9 10 11