-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chapter_4.Rmd
239 lines (166 loc) · 5.37 KB
/
Chapter_4.Rmd
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
---
title: "Chapter 4"
author: "Julin Maloof"
date: "2022-09-24"
output:
html_document:
keep_md: yes
---
## Quiz
1. What is the result of subsetting a vector with positive integers, negative integers, a logical vector, or a character vector?
_Positive integers select elements by positions; negative integers exclude elements by positions; a logical vector selects by position and needs to be the same length as the vector (or maybe it can be recycled); a character vector selects by name.
2. What’s the difference between [, [[, and $ when applied to a list?
_[ returns a list with those elements, [[ extracts that element and doesn't return a list (unless the element itself is a list), $ will pull out an element by name_
3. When should you use drop = FALSE?
When you do not want a data frame to be converted to a vector (or maybe when you don't want to lose dimensions)
4. If x is a matrix, what does x[] <- 0 do? How is it different from x <- 0?
_`x[] <- 0` will set all elements to 0. `x <- 0` will change x to a vector of length 1 with a value of 0._
5. How can you use a named vector to relabel categorical variables?
_I am not sure what this is asking_
## 4.2 Selecting multiple elements
skipped what I knew... this is the code for selecting from matrices with matrices:
```{r}
vals <- outer(1:5, 1:5, FUN = "paste", sep = ",")
vals
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] "1,1" "1,2" "1,3" "1,4" "1,5"
#> [2,] "2,1" "2,2" "2,3" "2,4" "2,5"
#> [3,] "3,1" "3,2" "3,3" "3,4" "3,5"
#> [4,] "4,1" "4,2" "4,3" "4,4" "4,5"
#> [5,] "5,1" "5,2" "5,3" "5,4" "5,5"
vals[c(4, 15)] #Selects the 4th and 15th elements, going down columns
```
```{r}
select <- matrix(ncol = 2, byrow = TRUE, c(
1, 1,
3, 1,
2, 4
))
select
vals[select]
```
Oh, cool, so these are selecting by "address"
### 4.2.6 Exercises
#### 1. Fix each of the following common data frame subsetting errors:
__Original__
```{r, eval=FALSE}
mtcars[mtcars$cyl = 4, ]
mtcars[-1:4, ]
mtcars[mtcars$cyl <= 5]
mtcars[mtcars$cyl == 4 | 6, ]
```
__Fixed__
```{r}
mtcars[mtcars$cyl == 4, ]
mtcars[-1:-4, ]
mtcars[mtcars$cyl <= 5,]
mtcars[mtcars$cyl == 4 | mtcars$cyl == 6, ]
```
#### 2. Why does the following code yield five missing values? (Hint: why is it different from x[NA_real_]?)
```{r}
x <- 1:5
x[NA]
x[NA_real_]
```
_I guess because [NA] is recycled and when you have an NA value it always returns NA in extraction. But why is [NA_real_] different? Maybe because it is testing for NA_Real?
#### 3. What does upper.tri() return? How does subsetting a matrix with it work? Do we need any additional subsetting rules to describe its behaviour?
```{r}
x <- outer(1:5, 1:5, FUN = "*")
x
upper.tri(x)
x[upper.tri(x)]
```
_`upper.tri()` returns a matrix of TRUES and FALSES where TRUES correspond to the upper triangle. We do not need any additional rules_
#### 4. Why does mtcars[1:20] return an error? How does it differ from the similar mtcars[1:20, ]?
```{r}
dim(mtcars)
```
_When you are subsetting a data frame and do not include a comma, it subsets columns. `mtcars` has less than 20 columns._
#### 5. Implement your own function that extracts the diagonal entries from a matrix (it should behave like diag(x) where x is a matrix).
```{r}
m <- matrix(1:32, ncol=8)
m
diag(m)
diag2 <- function(x) {
end <- ifelse(nrow(x) > ncol(x), length(x), nrow(x)^2) #anyway more clever to do this?
d <- seq(1, end, by=nrow(x)+1)
x[d]
}
diag2(m)
```
alternate way
```{r}
diag3 <- function(x) {
select <- 1:min(dim(x))
select <- cbind(select,select)
x[select]
}
diag(matrix(1:32,ncol=4))
diag3(matrix(1:32,ncol=4))
diag(matrix(1:32,ncol=8))
diag3(matrix(1:32,ncol=8))
```
#### 6. What does df[is.na(df)] <- 0 do? How does it work?
```{r}
df <- as.data.frame(m)
df
df[3,2] <- NA
df
df[is.na(df)] <- 0
df
```
_subsets the df to positions with NA and replaces them with 0_
## 4.3
Huh?
```{r, eval=FALSE}
for (i in 2:length(x)) {
out[[i]] <- fun(x[[i]], out[[i - 1]])
}
```
### Exercises 4.3.5
#### 1. Brainstorm as many ways as possible to extract the third value from the cyl variable in the mtcars dataset.
```{r}
mtcars[3,"cyl"]
mtcars[3,2]
mtcars$cyl[3]
mtcars[["cyl"]][3]
purrr::pluck(mtcars, "cyl", 3)
```
#### 2. Given a linear model, e.g., mod <- lm(mpg ~ wt, data = mtcars), extract the residual degrees of freedom. Then extract the R squared from the model summary (summary(mod))
```{r}
mod <- lm(mpg ~ wt, data = mtcars)
str(mod)
```
```{r}
mod$df.residual
```
```{r}
mod.sum <- summary(mod)
str(mod.sum)
```
```{r}
mod.sum$r.squared
```
### Exercises 4.5.9
#### 1. How would you randomly permute the columns of a data frame? (This is an important technique in random forests.) Can you simultaneously permute the rows and columns in one step?
```{r}
df <- data.frame(x=LETTERS[1:5], y=1:5, z=letters[26:22])
df
#columns
df[,sample(ncol(df))]
#columns and rows
df[sample(nrow(df)), sample(ncol(df))]
```
#### 2. How would you select a random sample of m rows from a data frame? What if the sample had to be contiguous (i.e., with an initial row, a final row, and every row in between)?
_I think the question means a random subset or random subsample_
```{r}
# 5 rows at random
mtcars[sample(nrow(mtcars), size = 5),]
# contiguous 5 rows
start <- sample(nrow(mtcars)-4, size=1)
mtcars[start:(start+4),]
```
#### 3. How could you put the columns in a data frame in alphabetical order?
```{r}
mtcars[, order(colnames(mtcars))]
```