-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmanipulating_data_practical.qmd
214 lines (163 loc) · 3.37 KB
/
manipulating_data_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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# Indexing Practical
## Selecting Data
**Answer the following without typing the commands into R. Use `?` if you're not sure what the object is or what the function does**
### Question 1: length of objects
#### a {.unnumbered}
What is
```{r}
#| eval: false
#| quiet: true
length(letters)
```
<br>
#### b {.unnumbered}
What is
```{r}
#| eval: false
#| quiet: true
length(letters == LETTERS)
```
### Question 2: `which`
#### a {.unnumbered}
What is
```{r}
#| eval: false
#| quiet: true
which(letters %in% c("a", "d"))
```
#### b {.unnumbered}
What is
```{r}
#| eval: false
#| quiet: true
which(c("a", 7, "d") %in% letters)
```
<br>
### Question 3: Logical indexing of a vector
#### a {.unnumbered}
What is
```{r}
#| eval: false
#| quiet: true
letters[LETTERS > "W"]
```
<br>
#### b {.unnumbered}
What is
```{r}
#| eval: false
#| quiet: true
letters[!LETTERS > "C"]
```
<br>
### Question 3: Indexing of a vector using integers
#### a {.unnumbered}
What is
```{r}
#| eval: false
#| quiet: true
letters[8]
```
<br>
#### b {.unnumbered}
What is
```{r}
#| eval: false
#| quiet: true
letters[-26:-2]
```
<br>
### Question 5: Sequences
#### a {.unnumbered}
What is
```{r}
#| eval: false
#| quiet: true
1:6
```
#### b {.unnumbered}
What is
```{r}
#| eval: false
#| quiet: true
10:3
```
#### c {.unnumbered}
What is
```{r}
#| eval: false
#| quiet: true
seq(from = 1, to = 20, by = 3)
```
<br>
### Question 6: Selection from a matrix
We define `x` as
```{r}
#| eval: false
#| quiet: true
x <- matrix(8:11, nrow = 6, ncol = 4)
```
What are
```{r}
#| eval: false
#| quiet: true
x[, 3] + 2 * x[, 2]
nrow(x)
x[x[, 3] > 10, ]
```
### Question 7
Use R to answer the following:
Create a vector (using `c()`) called `a` (i.e. assign it to an object called `a`) with four elements which are the integers 5 to 8 (inclusive).
a) Display element 2 of `a`.
<br>
b) Display element 4 of `a`.
<br>
c) Assign the integers 3 and 4 to object `b` and use `b` to select elements 3 and 4 of object `a`.
<br>
d) Display every element of `a` except element 2.
<br>
e) Display every element of `a` except elements 3 and 4.
<br>
f) Display only those elements of `a` that are greater than or equal to 6.
<br>
g) Display only those elements of `a` that are not equal to 7.
<br>
h) Use the `list` function to create an object `ab` which is a list of the two objects `a` and `b`.
<br>
i) Display `ab`.
<br>
j) Change the names of the elements in `ab` to `a` and `b`.
<br>
k) Display `ab` again. What has changed?
<br>
### Question 8
a) Create this matrix `m`:
```{r}
m <- matrix(1:9, nrow = 3, byrow = T)
m
```
b) Why are the numbers 1, 2, and 3 in the first row and not the first column?
<br>
c) Display the element on the second row and second column of `m`.
<br>
d) Display only the 2nd row of `m`.
<br>
e) Display only the 3rd column of `m`.
<br>
f) Display only the 2nd and 3rd columns of `m`. Do so in two different ways.
### Question 9
Using the **heart** data set (remember to load the survival package):
* Select the first row.
* Select the first column.
* Select the column `surgery`.
### Question 10
Create a list:
```{r}
x <- c(1,2,3,4)
mat <- matrix(1:9, 3, 3)
myList <- list(a=x, b=mat)
```
a) What is the class of `myList[[2]]`
b) What is the class of `myList[2]`
c) Write R code to extract the first element (the vector) by name
**If you want to save your work: save your R session and/or source code!**