-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathx0A_Starting_with_R_and_other_tools.Rmd
More file actions
504 lines (323 loc) · 12.1 KB
/
x0A_Starting_with_R_and_other_tools.Rmd
File metadata and controls
504 lines (323 loc) · 12.1 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
---
output: github_document
---
00383_example_A.1_of_section_A.2.R
```{r 00383_example_A.1_of_section_A.2.R }
# example A.1 of section A.2
# (example A.1 of section A.2) : Starting with R and other tools : Starting with R
# Title: Trying a few R commands
1
## [1] 1
1/2
## [1] 0.5
'Joe'
## [1] "Joe"
"Joe"
## [1] "Joe"
"Joe"=='Joe'
## [1] TRUE
c()
## NULL
is.null(c())
## [1] TRUE
is.null(5)
## [1] FALSE
c(1)
## [1] 1
c(1, 2)
## [1] 1 2
c("Apple", 'Orange')
## [1] "Apple" "Orange"
length(c(1, 2))
## [1] 2
vec <- c(1, 2)
vec
## [1] 1 2
```
00384_informalexample_A.3_of_section_A.2.1.R
```{r 00384_informalexample_A.3_of_section_A.2.1.R }
# informalexample A.3 of section A.2.1
# (informalexample A.3 of section A.2.1) : Starting with R and other tools : Starting with R : Primary features of R
x <- 2
x < - 3
## [1] FALSE
print(x)
## [1] 2
```
00385_example_A.2_of_section_A.2.1.R
```{r 00385_example_A.2_of_section_A.2.1.R }
# example A.2 of section A.2.1
# (example A.2 of section A.2.1) : Starting with R and other tools : Starting with R : Primary features of R
# Title: Binding values to function arguments
divide <- function(numerator,denominator) { numerator/denominator }
divide(1, 2)
## [1] 0.5
divide(2, 1)
## [1] 2
divide(denominator = 2, numerator = 1)
## [1] 0.5
divide(denominator <- 2, numerator <- 1) # wrong symbol <-, yields 2, a wrong answer!
## [1] 2
```
00386_example_A.3_of_section_A.2.1.R
```{r 00386_example_A.3_of_section_A.2.1.R }
# example A.3 of section A.2.1
# (example A.3 of section A.2.1) : Starting with R and other tools : Starting with R : Primary features of R
# Title: Demonstrating side effects
x<-1
good <- function() { x <- 5}
good()
print(x)
## [1] 1
bad <- function() { x <<- 5}
bad()
print(x)
## [1] 5
```
00387_example_A.4_of_section_A.2.1.R
```{r 00387_example_A.4_of_section_A.2.1.R }
# example A.4 of section A.2.1
# (example A.4 of section A.2.1) : Starting with R and other tools : Starting with R : Primary features of R
# Title: R truth tables for Boolean operators
c(TRUE, TRUE, FALSE, FALSE) == c(TRUE, FALSE, TRUE, FALSE)
## [1] TRUE FALSE FALSE TRUE
c(TRUE, TRUE, FALSE, FALSE) & c(TRUE, FALSE, TRUE, FALSE)
## [1] TRUE FALSE FALSE FALSE
c(TRUE, TRUE, FALSE, FALSE) | c(TRUE, FALSE, TRUE, FALSE)
## [1] TRUE TRUE TRUE FALSE
```
00388_example_A.5_of_section_A.2.1.R
```{r 00388_example_A.5_of_section_A.2.1.R }
# example A.5 of section A.2.1
# (example A.5 of section A.2.1) : Starting with R and other tools : Starting with R : Primary features of R
# Title: Call-by-value effect
a <- c(1, 2)
b <- a
print(b)
a[[1]] <- 5 # Note: 1
print(a)
print(b) # Note: 2
# Note 1:
# “Alter a”. Actually this is
# implemented by building an entirely new vector and
# reassigning a to refer to this new vector. The old
# value remains as it was, and any references
# continue to see the old unaltered value.
# Note 2:
# Notice b’s value is not
# changed.
```
00389_informalexample_A.4_of_section_A.2.2.R
```{r 00389_informalexample_A.4_of_section_A.2.2.R }
# informalexample A.4 of section A.2.2
# (informalexample A.4 of section A.2.2) : Starting with R and other tools : Starting with R : Primary R data types
vec <- c(2, 3)
vec[[2]] <- 5
print(vec)
## [1] 2 5
```
00390_example_A.6_of_section_A.2.2.R
```{r 00390_example_A.6_of_section_A.2.2.R }
# example A.6 of section A.2.2
# (example A.6 of section A.2.2) : Starting with R and other tools : Starting with R : Primary R data types
# Title: Examples of R indexing operators
x <- list('a' = 6, b = 'fred')
names(x)
## [1] "a" "b"
x$a
## [1] 6
x$b
## [1] "fred"
x[['a']]
## $a
## [1] 6
x[c('a', 'a', 'b', 'b')]
## $a
## [1] 6
##
## $a
## [1] 6
##
## $b
## [1] "fred"
##
## $b
## [1] "fred"
```
00391_example_A.7_of_section_A.2.2.R
```{r 00391_example_A.7_of_section_A.2.2.R }
# example A.7 of section A.2.2
# (example A.7 of section A.2.2) : Starting with R and other tools : Starting with R : Primary R data types
# Title: R’s treatment of unexpected factor levels
factor('red', levels = c('red', 'orange'))
## [1] red
## Levels: red orange
factor('apple', levels = c('red', 'orange'))
## [1] <NA>
## Levels: red orange
```
00392_example_A.8_of_section_A.2.2.R
```{r 00392_example_A.8_of_section_A.2.2.R }
# example A.8 of section A.2.2
# (example A.8 of section A.2.2) : Starting with R and other tools : Starting with R : Primary R data types
# Title: Confirm lm() encodes new strings correctly
d <- data.frame(x=factor(c('a','b','c')),
y=c(1,2,3))
m <- lm(y~0+x,data=d) # Note: 1
print(predict(m,
newdata=data.frame(x='b'))[[1]]) # Note: 2
# [1] 2
print(predict(m,
newdata=data.frame(x=factor('b',levels=c('b'))))[[1]]) # Note: 3
# [1] 2
# Note 1:
# Build a data frame and linear model mapping
# a,b,c to 1,2,3.
# Note 2:
# Show that model gets correct prediction for
# b as a string.
# Note 3:
# Show that model gets correct prediction for
# b as a factor, encoded with a different number of
# levels. This shows that lm() is correctly treating
# factors as strings.
```
00393_informalexample_A.5_of_section_A.3.1.R
```{r 00393_informalexample_A.5_of_section_A.3.1.R }
# informalexample A.5 of section A.3.1
# (informalexample A.5 of section A.3.1) : Starting with R and other tools : Using databases with R : Running database queries using a query generator
library("rquery")
raw_connection <- DBI::dbConnect(RPostgres::Postgres(),
host = 'localhost',
port = 5432,
user = 'johnmount',
password = '') # Note: 1
dbopts <- rq_connection_tests(raw_connection) # Note: 2
db <- rquery_db_info(
connection = raw_connection,
is_dbi = TRUE,
connection_options = dbopts)
data_handle <- rq_copy_to( # Note: 3
db,
'offers',
wrapr::build_frame(
"user_name" , "product" , "discount", "predicted_offer_affinity" |
"John" , "Pandemic Board Game" , 0.1 , 0.8596 |
"Nina" , "Pandemic Board Game" , 0.2 , 0.1336 |
"John" , "Dell XPS Laptop" , 0.1 , 0.2402 |
"Nina" , "Dell XPS Laptop" , 0.05 , 0.3179 |
"John" , "Capek's Tales from Two Pockets", 0.05 , 0.2439 |
"Nina" , "Capek's Tales from Two Pockets", 0.05 , 0.06909 |
"John" , "Pelikan M200 Fountain Pen" , 0.2 , 0.6706 |
"Nina" , "Pelikan M200 Fountain Pen" , 0.1 , 0.616 ),
temporary = TRUE,
overwrite = TRUE)
# Note 1:
# Use DBI to connect to a database. In this case it creates a new in-memory SQLite.
# Note 2:
# Build an rquery wrapper of the connection.
# Note 3:
# Copy some example data into the database.
```
00394_informalexample_A.6_of_section_A.3.1.R
```{r 00394_informalexample_A.6_of_section_A.3.1.R }
# informalexample A.6 of section A.3.1
# (informalexample A.6 of section A.3.1) : Starting with R and other tools : Using databases with R : Running database queries using a query generator
data_handle %.>% # Note: 1
extend(.,
simple_rank = rank(), # Note: 2
partitionby = "user_name", # Note: 3
orderby = "predicted_offer_affinity", # Note: 4
reverse = "predicted_offer_affinity") %.>%
execute(db, .) %.>% # Note: 5
knitr::kable(.) # Note: 6
# |user_name |product | discount| predicted_offer_affinity| simple_rank|
# |:---------|:------------------------------|--------:|------------------------:|-----------:|
# |Nina |Pelikan M200 Fountain Pen | 0.10| 0.61600| 1|
# |Nina |Dell XPS Laptop | 0.05| 0.31790| 2|
# |Nina |Pandemic Board Game | 0.20| 0.13360| 3|
# |Nina |Capek's Tales from Two Pockets | 0.05| 0.06909| 4|
# |John |Pandemic Board Game | 0.10| 0.85960| 1|
# |John |Pelikan M200 Fountain Pen | 0.20| 0.67060| 2|
# |John |Capek's Tales from Two Pockets | 0.05| 0.24390| 3|
# |John |Dell XPS Laptop | 0.10| 0.24020| 4|
# Note 1:
# Pipe our data into the
# execute() method. Notice we are using the “wrapr dot
# pipe.”
# Note 2:
# We are going to calculate rank() or the order of the data rows.
# Note 3:
# The ranking will be re-calculated for each user (our window partition).
# Note 4:
# The window ordering that controls the rank is to be from predicted_offer_affinity, reversed (largest first).
# Note 5:
# Translate the operation plan into SQL, send it to the database for execution, and bring the results back to R.
# Note 6:
# Pretty print the results.
```
00395_informalexample_A.7_of_section_A.3.1.R
```{r 00395_informalexample_A.7_of_section_A.3.1.R }
# informalexample A.7 of section A.3.1
# (informalexample A.7 of section A.3.1) : Starting with R and other tools : Using databases with R : Running database queries using a query generator
ops <- data_handle %.>% # Note: 1
extend(., # Note: 2
simple_rank = rank(),
partitionby = "user_name",
orderby = "predicted_offer_affinity",
reverse = "predicted_offer_affinity") %.>%
select_rows(., # Note: 3
simple_rank <= 2) %.>%
orderby(., c("user_name", "simple_rank")) # Note: 4
result_table <- materialize(db, ops) # Note: 5
DBI::dbReadTable(db$connection, result_table$table_name) %.>% # Note: 6
knitr::kable(.)
# |user_name |product | discount| predicted_offer_affinity| simple_rank|
# |:---------|:-------------------------|--------:|------------------------:|-----------:|
# |John |Pandemic Board Game | 0.10| 0.8596| 1|
# |John |Pelikan M200 Fountain Pen | 0.20| 0.6706| 2|
# |Nina |Pelikan M200 Fountain Pen | 0.10| 0.6160| 1|
# |Nina |Dell XPS Laptop | 0.05| 0.3179| 2|
# Note 1:
# Define our sequence of operations
# Note 2:
# Mark each row with its simple per-user rank
# Note 3:
# Select the two rows with highest rank for each
# user
# Note 4:
# Order the rows by user and product rank
# Note 5:
# Run the result in the database, instantiating a new
# result table
# Note 6:
# Copy the result back to R and pretty print it.
```
00396_informalexample_A.8_of_section_A.3.1.R
```{r 00396_informalexample_A.8_of_section_A.3.1.R }
# informalexample A.8 of section A.3.1
# (informalexample A.8 of section A.3.1) : Starting with R and other tools : Using databases with R : Running database queries using a query generator
ops %.>%
to_sql(., db) %.>%
cat(.)
## SELECT * FROM (
## SELECT * FROM (
## SELECT
## "user_name",
## "product",
## "discount",
## "predicted_offer_affinity",
## rank ( ) OVER ( PARTITION BY "user_name" ORDER BY "predicted_offer_affinity" DESC ) AS "simple_rank"
## FROM (
## SELECT
## "user_name",
## "product",
## "discount",
## "predicted_offer_affinity"
## FROM
## "offers"
## ) tsql_17135820721167795865_0000000000
## ) tsql_17135820721167795865_0000000001
## WHERE "simple_rank" <= 2
## ) tsql_17135820721167795865_0000000002 ORDER BY "user_name", "simple_rank"
```