-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLecture7.r
More file actions
237 lines (170 loc) · 5.57 KB
/
Copy pathLecture7.r
File metadata and controls
237 lines (170 loc) · 5.57 KB
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
# The structure
func_name <- function(argument) {
statement
}
# Build the function
a_b <- function(a, b) {
f = a - b
return(f)
}
# Use the function
a_b(5, 3)
a_b(3, 5)
a_b(3, a = 5)
a_b(a = 5, b = 3)
a_b(b = 3, a = 5)
a_b(a = c(5,6,7), b = c(3,2,1))
# Recall
9 %/% 2
9 %% 2
int.div <- function(a, b){
int <- floor(a/b)
mod <- a - int*b
return(list(integer = int, modulus = mod))
}
# Recall: how do we access the modulus?
result <- int.div(21, 4)
result
str(result)
result$integer
int.div <- function(a, b){
int <- a%/%b
mod <- a%%b
cat(a, "%%", b, ": \n integer =", int,"\n ------------------ \n modulus =", mod, "\n")
}
int.div(21, 4)
int.div <- function(a, b){
int <- a%/%b
mod <- a%%b
output <- c(int, mod)
names(output) <- c("integer", "modulus")
return(output)
}
int.div(21, 4)
# No need to worry about the details here.
# Just want to show that functions do not always have to return() something.
AIcanadian <- function(who, reply_to) {
system(paste("say -v", who, "Sorry!"))
}
# AIcanadian("Alex", "Sorry I stepped on your foot.")
# Train my chatbot - AlphaGo style.
# I'll let Alex and Victoria talk to each other.
# MacOS has their voices recorded.
# chat_log <- rep(NA, 8)
# for (i in 1:8) {
# if (i == 1) {
# chat_log[1] <- "Sorry I stepped on your foot."
# system("say -v Victoria Sorry, I stepped on your foot.")
# } else {
# if (i %% 2 == 0)
# chat_log[i] <- AIcanadian("Alex", chat_log[i - 1])
# else
# chat_log[i] <- AIcanadian("Victoria", chat_log[i - 1])
# }
# }
# chat_log
data_summary <- function(func) {
data <- read.csv("https://raw.githubusercontent.com/ly129/EPIB613_2020/master/scores.csv", header = TRUE)
by(data = data$scores, INDICES = list(data$course), FUN = func)
}
data_summary(summary)
times_2_by_default_dislikes_3_hates_4 <- function(a, b = 2){
if (b == 3) {
warning("I dislike 3!")
}
if (b == 4) {
stop("I hate 4!")
}
return(a*b)
}
times_2_by_default_dislikes_3_hates_4(a = 6)
times_2_by_default_dislikes_3_hates_4(a = 6, b = 3)
times_2_by_default_dislikes_3_hates_4(a = 6, b = 4)
times_2_by_default_dislikes_3_hates_4(a = 6, b = 5)
# Sample data
set.seed(613)
students <- rep(c("Lucy", "John", "Mark", "Candy", "Chris"), 3)
course <- rep(c("epib601", "epib607", "epib613"), each = 5)
scores <- sample(50:100, size = 15, replace = T)
df <- data.frame(students, course, scores)
df <- df[sample(1:nrow(df)), ] # shuffle the rows to make things more complicated
df
library(tidyverse)
df_wide <- pivot_wider(data = df, names_from = course, values_from = scores)
df_wide
df_long <- pivot_longer(data = df_wide, cols = contains("epib"), names_to = "course", values_to = "scores")
df_long
# ?dplyr::select # for how to select columns
my_wider <- function(data, pivot, names_from, values_from) {
}
my_wider(df_long, pivot = "students", names_from = "course", values_from = "scores")
# In-class progress
my_longer <- function(data, pivot, cols, names_to, values_to) {
# Figure out # of pivots - rep the col names by this many times
n.pivot <- nrow(data)
cat("n.pivot = ", n.pivot, "\n")
# Figure out # of columns - rep the pivots by this many times
n.col <- length(cols)
cat("n.col = ", n.col, "\n")
# replicate to what we want
# (lucy, chris, ...) x n.col
pivot.rep <- rep(data[, pivot], n.col)
# (epib601 x n.pivot, epib607 x n.pivot, epib613 x n.pivot)
name.rep <- rep(cols, each = n.pivot)
# Extract the values - n.pivot by n.col
# Transform to a vector of length (n.pivot x n.cols)
# cbind rep'ed pivots, rep'ed col names, vectorized values
# return
}
my_longer(df_wide,
pivot = "students",
cols = c("epib601", "epib607", "epib613"),
names_to = "course",
values_to = "scores")
## A sample solution
my_wider <- function(data, pivot, names_from, values_from) {
# reorder data - to take advantage of
data <- data[order(data[, pivot]), ]
data <- data[order(data[, names_from]), ]
# get column names
col.names <- unique(data[, names_from])
# get number of columns - one for each course
n.col <- length(col.names)
# generate pivot variable
pivot.var <- unique(data[, pivot])
# generate number of pivots
n.pivot <- length(pivot.var)
# generate value matrix
value.mat <- matrix(data[, values_from], nrow = n.pivot, ncol = n.col, byrow = F)
# assemble wide data frame
data_wide <- data.frame(pivot.var, value.mat)
# rename wide data frame
names(data_wide) <- c(pivot, as.character(col.names))
return(data_wide)
}
df_wide <- my_wider(df, pivot = "students", names_from = "course", values_from = "scores")
df_wide
my_longer <- function(data, pivot, cols, names_to, values_to) {
# extract the values
values <- data[, cols]
# change it to a vector, by column
values <- as.vector(as.matrix(values))
# determine number of cols
n.col <- length(cols)
# rep the pivot variable
pivot.var <- rep(unlist(data[, pivot]), n.col)
# determine the number of pivots
n.pivot <- nrow(data)
# make the names_to variable
names_to.var <- rep(cols, each = n.pivot)
# assemble long data frame
data.long <- data.frame(pivot.var, names_to.var, values)
# rename long data frame
names(data.long) <- c(pivot, names_to, values_to)
return(data.long)
}
my_longer(df_wide,
pivot = "students",
cols = c("epib601", "epib607", "epib613"),
names_to = "course",
values_to = "scores")