-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path1-9_Indexing_Subsetting_practical.qmd
125 lines (73 loc) · 2.9 KB
/
1-9_Indexing_Subsetting_practical.qmd
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
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
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
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
114
115
116
117
118
119
120
121
122
123
124
125
# Indexing and Subsetting - Practical {.unnumbered}
```{r setup, include=FALSE}
htmltools::tagList(rmarkdown::html_dependency_font_awesome())
```
```{r, echo = FALSE, purl = FALSE}
knitr::knit_hooks$set(purl = knitr::hook_purl)
options(purl = FALSE)
knitr::opts_chunk$set(purl = FALSE)
```
```{r, include = FALSE}
knitr::opts_hooks$set(eval = function(opt) {
if (any(opt$exercise))
opt$eval <- opt$include <- FALSE
opt
})
static <- TRUE
options(width = 100)
```
```{r packages, include = FALSE}
library(kableExtra)
library(knitr)
```
```{r load_data, context="data", include=FALSE}
library(survival)
```
## Preface {data-progressive="FALSE"}
Open Rstudio to do the practicals. Note that tasks with \* are optional.
### R packages
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: `r packageVersion("survival")`)
*`r R.version.string`*
### Dataset {.tabset .tabset-fade .tabset-pills}
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
## Indexing and Subsetting
Sometimes we want to obtain a subset of the data sets before investigating the descriptive statistics and performing the statistical analysis.
### Indexing {.tabset .tabset-fade .tabset-pills}
::: {.panel-tabset .nav-pills}
#### Task 1
Using the **heart** data set:\
- Select the first row.\
- Select the first column.\
- Select the column `surgery`.
:::
::: {.panel-tabset .nav-pills}
#### Task 2
Create a matrix that takes the values 1:4 and has 2 rows and 2 columns. You can name this object `mat`. Select the second row of all columns.
:::
::: {.panel-tabset .nav-pills}
#### Task 3
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. Give the name `ar1` to the this array. Select the 2nd row of all columns from each matrix.
:::
### Subsetting {.tabset .tabset-fade .tabset-pills}
::: {.panel-tabset .nav-pills}
#### Task 1
Using the **retinopathy** data set:\
- Select the `futime` for all `adult` patients.\
- Select all the variables for patients that received treatment.\
:::
::: {.panel-tabset .nav-pills}
#### Task 2
Using the **retinopathy** data set:\
- Select the `age` for patients that have `futime` more than 20.\
- Select the `age` for patients that have `futime` more than 20 and are adults.\
- Select patients that have no missing values in `age`.
:::
::: {.panel-tabset .nav-pills}
#### Task 3
Using the **retinopathy** data set:\
- Select only the rows of the left eye.
- Select only the rows of adult patients.
:::