-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathchapter2.Rmd
999 lines (712 loc) · 30.4 KB
/
chapter2.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
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
---
courseTitle : Introduction to R v2
chapterTitle : Vectors
description : We take you on a trip to Vegas, where you'll learn how to analyze your gambling results using vectors in R! After completing this chapter, you'll be able to create vectors in R, name them, select elements from them and compare different vectors.
framework : datamind
mode: selfcontained
---
## Create a vector
Feeling lucky?
You better be, because this chapter takes you on a trip to the City of Sins, also known as "Statisticians Paradise" ;-)
Thanks to R and your new data-analytical skills, you will see how to uplift your performance at the tables, and fire off your career as a professional gambler. This chapter will show you how to easily keep track of your betting progress and how to do some simple analysis on past actions. Next Stop, Vegas Baby...VEGAS!!!!
*** =instructions
1. Let's look what you remember from last chapter. Assign to the variable `Vegas` the value `"Here we go!"`.
*** =hint
Type: `Vegas <- "Here we go!"`
*** =sample_code
```{r eval=FALSE}
```
*** =solution
```{r eval=FALSE}
Vegas <- "Here we go!"
```
*** =sct
```{r eval=FALSE}
DM.result <- closed_test( "Vegas", 'c("Here we go!")' )
```
*** =pre_exercise_code
```{r eval=FALSE}
```
---
## Create a vector (2)
But hey, let's focus first!
On your way from rags to riches, you will make extensive use of vectors. Vectors are one-dimensional arrays that can hold numeric data, character data, or logical data. Simply said, a vector is a very simple tool to store data away. For example, your daily gains and losses in the casino's.
In R, you create a vector with the combine function `c()`. Between the brackets you place the vector elements separated with a comma. For example:
- `numeric.vector`
- `character.vector`
- `boolean.vector`
Once you have created these vectors in R, you can use them to do calculations.
*** =instructions
1. Complete the code such that boolean.vector contains the three elements: TRUE, FALSE,TRUE
*** =hint
Assign `c(TRUE,FALSE,TRUE)` to the variable `boolean.vector` with the `<-` operator.
*** =sample_code
```{r eval=FALSE}
numeric.vector <- c(1,10,49)
character.vector <- c("a","b","c")
boolean.vector
numeric.vector
character.vector
boolean.vector
```
*** =solution
```{r eval=FALSE}
numeric.vector <- c(1,10,49)
character.vector <- c("a","b","c")
boolean.vector <- c(TRUE,FALSE,TRUE)
numeric.vector
character.vector
boolean.vector
```
*** =sct
```{r eval=FALSE}
DM.result <- closed_test(names="boolean.vector",values="c(TRUE,FALSE,TRUE)")
```
*** =pre_exercise_code
```{r eval=FALSE}
```
---
## Create a vector (3)
After one week in Las Vegas, and still zero Ferraris in your garage, you decide it is time to start using your data analytical superpowers.
Before doing a first analysis, you decide to first collect the winnings and losses for the last week:
For `poker.vector`:
- On monday you won 140\$,
- tuesday you lost 50\$,
- wednesday you won 20\$,
- thursday you lost 120\$
- and friday you won 240\$.
For `roulette.vector`:
- On monday you lost 24\$,
- tuesday you lost 50\$,
- wednesday you won 100\$,
- thursday you lost 350\$ and
- friday you won 10\$.
You only played Poker and Roulette, since there was a delegation of paragnosts that occupied the craps tables. To be able to use this data in R, you decide to create the variables `poker.vector` and `roulette.vector`.
*** =instructions
1. Assign to the variable `roulette.vector` the winnings/losses for roulette.
*** =hint
To help you with this step, the editor already contains the code for creating `poker.vector`. Assign the correct values to `roulette.vector` based on the numbers in the assignment. Don't forget that losses are negative numbers.
*** =sample_code
```{r eval=FALSE}
# Poker winnings from Monday to Friday:
poker.vector <- c(140,-50,20,-120,240)
# Roulette winnings form Monday to Friday:
roulette.vector
```
*** =solution
```{r eval=FALSE}
# Poker winnings from Monday to Friday:
poker.vector <- c(140,-50,20,-120,240)
# Roulette winnings form Monday to Friday:
roulette.vector <- c(-24,-50,100,-350,10)
```
*** =sct
```{r eval=FALSE}
names <- c("poker.vector","roulette.vector")
values <- list("c(140,-50,20,-120,240)", "c(-24,-50,100,-350,10)")
DM.result <- closed_test(names,values)
```
*** =pre_exercise_code
```{r eval=FALSE}
```
---
## Naming a vector
As a data-analyst, it is extremely important to always have a clear view on the data you are using. Understanding what each element refers to is therefore key.
In the previous exercise, we created a vector with your winnings over the week. Each vector element refers to a day of the week but it is hard to tell which element belongs to which day. It would be nice if you could indicate that in the vector itself.
You can give a name to the elements of a vector with the `names()` function. For example: `names(some.vector)`.
*** =instructions
1. Go ahead and assign the days of the week as names to the poker and roulette vector. In case you're not sure, the days of the week are: Monday, Tuesday, Wednesday, Thursday, Friday.
*** =hint
You can use `names( poker.vector )` to set the names of the variable `poker.vector`. Make sure not to forget the parentheses and the fact that weekdays are written with a capital letter! The vector with days of the week is thus given by: `c("Monday","Tuesday","Wednesday","Thursday","Friday")`
*** =sample_code
```{r eval=FALSE}
# Poker winnings from Monday to Friday:
poker.vector <- c(140,-50,20,-120,240)
# Roulette winnings form Monday to Friday:
roulette.vector <- c(-24,-50,100,-350,10)
#Add your code here!
#Print the named vectors to the console
poker.vector
roulette.vector
```
*** =solution
```{r eval=FALSE}
# Poker winnings from Monday to Friday:
poker.vector <- c(140,-50,20,-120,240)
# Roulette winnings form Monday to Friday:
roulette.vector <- c(-24,-50,100,-350,10)
names(poker.vector) <- c("Monday","Tuesday","Wednesday","Thursday","Friday")
names(roulette.vector) <- c("Monday","Tuesday","Wednesday","Thursday","Friday")
#Print the named vectors
poker.vector
```
*** =sct
```{r eval=FALSE}
names <- c("poker.vector","roulette.vector","names(poker.vector)","names(roulette.vector)")
values <- list("poker.vector","roulette.vector",'c("Monday","Tuesday","Wednesday","Thursday","Friday")','c("Monday","Tuesday","Wednesday","Thursday","Friday")')
DM.result <- closed_test(names,values,check.existence=c(TRUE,TRUE,FALSE,FALSE))
```
*** =pre_exercise_code
```{r eval=FALSE}
```
---
## Naming a vector (2)
If you want to become a good statistician, you have to become lazy! (If you are already lazy, feel proud, chances are high your one of those exceptional, natural-born statistical talents)
In the previous exercises you probably experienced it is boring and frustrating to type and retype information such as the days of the week. However, when looking at it from a higher perspective, there is a more efficient way to do this: assign the days of the week vector to a **variable**!
Just like you did with your poker and roulette returns, you can also create a variable that contains the days of the week. This way you can use and re-use it.
*** =instructions
1. Create a variable `days.vector` that contains the days Monday until Friday.
2. Use that variable `days.vector` to set the names of `poker.vector` and `roulette.vector`.
*** =hint
You can use `names( poker.vector )` to set the names of the variable `poker.vector`. Make sure not to forget the parentheses around the days of the week.
*** =sample_code
```{r eval=FALSE}
# Poker winnings from Monday to Friday:
poker.vector <- c(140,-50,20,-120,240)
# Roulette winnings form Monday to Friday:
roulette.vector <- c(-24,-50,100,-350,10)
# Create the variable days:
days.vector <-
#Assign the names of the day to the roulette and poker vectors
names(poker.vector) <-
names(roulette.vector) <-
#Print the named vectors to the console
poker.vector
roulette.vector
```
*** =solution
```{r eval=FALSE}
# Poker winnings from Monday to Friday:
poker.vector <- c(140,-50,20,-120,240)
# Roulette winnings form Monday to Friday:
roulette.vector <- c(-24,-50,100,-350,10)
# Create the variable days:
days.vector <- c("Monday","Tuesday","Wednesday","Thursday","Friday")
#Assign the names of the day to the roulette and poker vectors
names(poker.vector) <- days.vector
names(roulette.vector) <- days.vector
#Print the named vectors
poker.vector
roulette.vector
```
*** =sct
```{r eval=FALSE}
names <- c("poker.vector","roulette.vector","names(poker.vector)","names(roulette.vector)")
values <- list("poker.vector","roulette.vector",'c("Monday","Tuesday","Wednesday","Thursday","Friday")','c("Monday","Tuesday","Wednesday","Thursday","Friday")')
DM.result <- closed_test(names,values,check.existence=c(TRUE,TRUE,FALSE,FALSE))
```
*** =pre_exercise_code
```{r eval=FALSE}
```
---
## Calculating total winnings
Now that you have the poker and roulette winnings nicely as a named vector, you can start doing some data analytical magic.
You want to find out the following type of information:
- How much has been your overall profit or loss per day of the week?
- Have you lost money over the week in total?
- Are you winning/losing money on poker or on roulette?
To get the answer to this, you have to do arithmetic calculations on vectors.
Important to know is that if you sum two vectors in R, it takes the element-wise sum. For example: `c(1,2,3)+c(4,5,6)` is equal to `c(1+4,2+5,3+6)` or `c(5,7,9)`. Let's try this first!
*** =instructions
1. Take the sum of the variables `A.vector` and `B.vector` and assign to `total.vector`.
*** =hint
Use the `+` operator to sum `A.vector` and `B.vector`. Use `<-` to assign the result to `total.vector`.
*** =sample_code
```{r eval=FALSE}
A.vector <- c(1,2,3)
B.vector <- c(4,5,6)
# Take the sum of A.vector and B.vector
total.vector <-
# Show me:
total.vector
```
*** =solution
```{r eval=FALSE}
A.vector <- c(1,2,3)
B.vector <- c(4,5,6)
# Take the sum of A.vector and B.vector
total.vector <- A.vector + B.vector
# Show me:
total.vector
```
*** =sct
```{r eval=FALSE}
names <- c("A.vector","B.vector","total.vector")
values <- list("c(1,2,3)","c(4,5,6)","A.vector+B.vector")
DM.result <- closed_test(names,values)
```
*** =pre_exercise_code
```{r eval=FALSE}
```
---
## Calculating total winnings (2)
So you understand how R does arithmetic calculations with vectors?
Then it is time to get those ferrari's in your garage! First, you need to understand what the overall profit or loss per day of the week was. The total daily profit, is the sum of the profit/loss you realized on poker per day, and the profit/loss you realized on roulette per day.
In R, this is just the sum of the `roulette.vector` and `poker.vector`.
*** =instructions
1. Assign to the variable `total.daily` how much you won or lost on each day in total (poker & roulette combined)
*** =hint
Just click the Run button on the right.
*** =sample_code
```{r eval=FALSE}
# Poker winnings from Monday to Friday:
poker.vector <- c(140,-50,20,-120,240)
# Roulette winnings form Monday to Friday:
roulette.vector <- c(-24,-50,100,-350,10)
# Name poker and roulette
days <- c("Monday","Tuesday","Wednesday","Thursday","Friday")
names(roulette.vector) <- days
names(poker.vector) <- days
# Up to you now:
total.daily <-
# Show me:
total.daily
```
*** =solution
```{r eval=FALSE}
# Poker winnings from Monday to Friday:
poker.vector <- c(140,-50,20,-120,240)
# Roulette winnings form Monday to Friday:
roulette.vector <- c(-24,-50,100,-350,10)
# Name poker and roulette
days <- c("Monday","Tuesday","Wednesday","Thursday","Friday")
names(roulette.vector) <- days
names(poker.vector) <- days
# Up to you now:
total.daily <- poker.vector + roulette.vector
# Show me:
total.daily
```
*** =sct
```{r eval=FALSE}
names <- c("poker.vector","roulette.vector","total.daily")
values <- c("poker.vector","roulette.vector","poker.vector+roulette.vector")
DM.result <- closed_test(names,values)
```
*** =pre_exercise_code
```{r eval=FALSE}
```
---
## Calculating total winnings (3)
Looking back at the previous analysis, it seemed that you had a mix of good and bad days. This is not what your ego expected, and you wonder if maybe there is a (very very very) tiny chance you've lost money over the week in total?
A function that helps you to answer this question is `sum()`. It calculates the sum of all elements of a vector. For example, to calculate the total amount of money you lost/won with poker you do: `total.poker <- poker.vector`.
*** =instructions
1. Calculate the total amount of money you've won/lost with roulette and assign to the variable `total.roulette`.
2. Now you have the totals for roulette and poker, you can easily calculate `total.week` (which is the sum of all gains and losses of the week)
*** =hint
Just click the Run button on the right.
*** =sample_code
```{r eval=FALSE}
# Poker winnings from Monday to Friday:
poker.vector <- c(140,-50,20,-120,240)
# Roulette winnings form Monday to Friday:
roulette.vector <- c(-24,-50,100,-350,10)
# Name poker and roulette
days <- c("Monday","Tuesday","Wednesday","Thursday","Friday")
names(roulette.vector) <- days
names(poker.vector) <- days
total.poker <- sum(poker.vector)
# Up to you now:
total.roulette <-
total.week <-
# Show me:
total.week
```
*** =solution
```{r eval=FALSE}
# Poker winnings from Monday to Friday:
poker.vector <- c(140,-50,20,-120,240)
# Roulette winnings form Monday to Friday:
roulette.vector <- c(-24,-50,100,-350,10)
# Name poker and roulette
days <- c("Monday","Tuesday","Wednesday","Thursday","Friday")
names(roulette.vector) <- days
names(poker.vector) <- days
total.poker <- sum(poker.vector)
# Up to you now:
total.roulette <- sum(roulette.vector)
total.week <- total.roulette + total.poker
#print variables
total.week
```
*** =sct
```{r eval=FALSE}
names <- c("poker.vector","roulette.vector","total.poker","total.roulette","total.week")
values <- c("poker.vector","roulette.vector","sum(poker.vector)","sum(roulette.vector)","total.poker+total.roulette")
DM.result <- closed_test(names,values,check.existence=c(TRUE,TRUE,FALSE,FALSE,TRUE))
```
*** =pre_exercise_code
```{r eval=FALSE}
```
---
## Comparing total winnings
Ooops, it seems like you're losing money :-s. Time to rethink and adapt your strategy! This will require some deeper analysis...
After a short brainstorm in your hotel's jacuzzi, you realize a possible explanation is that your skills in roulette are not as well developed as your skills in poker. So maybe your total gains in poker are higher (or `>` ) than in roulette.
*** =instructions
1. Calculate `total.poker` and `total.roulette`.
2. Check if your total gains in poker are higher than in roulette by using a comparison. Assign the result of this comparison to the variable `answer`.
(Tip: You partly calculated the answer to this question in the previous exercise). What do you conclude, should you focus on roulette or on poker?
*** =hint
To check if 6 is larger than 5, you type `6 > 5`. This returns a variable with data type Logical (`TRUE` or `FALSE`).
*** =sample_code
```{r eval=FALSE}
# Poker winnings from Monday to Friday:
poker.vector <- c(140,-50,20,-120,240)
# Roulette winnings form Monday to Friday:
roulette.vector <- c(-24,-50,100,-350,10)
# Name poker and roulette
days <- c("Monday","Tuesday","Wednesday","Thursday","Friday")
names(roulette.vector) <- days
names(poker.vector) <- days
# Calculate total gains for poker and roulette
total.poker <-
total.roulette <-
# Check if you realized higher total gains in poker then in roulette
answer <-
#Show me the
answer
```
*** =solution
```{r eval=FALSE}
# Poker winnings from Monday to Friday:
poker.vector <- c(140,-50,20,-120,240)
# Roulette winnings form Monday to Friday:
roulette.vector <- c(-24,-50,100,-350,10)
# Name poker and roulette
days <- c("Monday","Tuesday","Wednesday","Thursday","Friday")
names(roulette.vector) <- days
names(poker.vector) <- days
# Calculate total gains for poker and roulette
total.poker <- sum(poker.vector)
total.roulette <- sum(roulette.vector)
# Check if you realized higher total gains in poker then in roulette
answer <- total.poker > total.roulette
```
*** =sct
```{r eval=FALSE}
names <- c("poker.vector","roulette.vector","total.poker","total.roulette","answer")
values <- c("poker.vector","roulette.vector","sum(poker.vector)","sum(roulette.vector)","total.poker > total.roulette")
DM.result <- closed_test(names,values,check.existence=c(TRUE,TRUE,FALSE,FALSE,TRUE))
```
*** =pre_exercise_code
```{r eval=FALSE}
```
---
## Vector selection: the good times
Your hunch seemed to be right, it appears that the game of poker is more your cup of tea than roulette.
Another possible route for investigation, is your performance at the beginning of the working week versus at the end of it? You did have a couple of Margarita cocktails at the end of the week...
Seeing the gains/losses of a certain day, or number of days, means in this case you only want to see a part/selection of the total vector. So our goal is to select specific elements of the vector. To select elements of a vector (and later on matrices, dataframes...) you always make use of square brackets. Between the square brackets, you indicate what elements to select: [...elements to select..]
For example, to select the first element of the vector, you type `poker.vector[1]`. To select the second element of the vector, you type `poker.vector[2]` etc.
*** =instructions
1. Assign to the variable `poker.wednesday` the poker results of wednesday.
*** =hint
Wednesday is the third element of `poker.vector`, and can thus be selected with `poker.vector[3]`.
*** =sample_code
```{r eval=FALSE}
# Poker winnings from Monday to Friday:
poker.vector <- c(140,-50,20,-120,240)
# Roulette winnings form Monday to Friday:
roulette.vector <- c(-24,-50,100,-350,10)
# Name poker and roulette
days.vector <- c("Monday","Tuesday","Wednesday","Thursday","Friday")
names(roulette.vector) <- names(poker.vector) <- days.vector
# Define new variable based on a selection:
poker.wednesday
```
*** =solution
```{r eval=FALSE}
# Poker winnings from Monday to Friday:
poker.vector <- c(140,-50,20,-120,240)
# Roulette winnings form Monday to Friday:
roulette.vector <- c(-24,-50,100,-350,10)
# Name poker and roulette
days.vector <- c("Monday","Tuesday","Wednesday","Thursday","Friday")
names(roulette.vector) <- names(poker.vector) <- days.vector
# Define new variable based on a selection:
poker.wednesday <- poker.vector[3]
```
*** =sct
```{r eval=FALSE}
names <- c("poker.vector","poker.wednesday")
values <- c("poker.vector","poker.vector[3]")
DM.result <- closed_test( names, values )
```
*** =pre_exercise_code
```{r eval=FALSE}
```
---
## Vector selection: the good times (2)
What if you want to analyze your midweek results?
To select multiple elements at once, you put between square brackets a vector that indicates which elements to select. Suppose you want to select the first and the fifth day of the week, you use the vector `c(1,5)`. This gives: `poker.vector[c(1,5)]`. Thus, you select here the first and the fifth element of `poker.vector`.
*** =instructions
1. Assign to variable `poker.midweek` the poker results of Tuesday, Wednesday and Thursday.
*** =hint
Use the vector `c(2,3,4)` between square brackets to select the correct elements of `poker.vector`.
*** =sample_code
```{r eval=FALSE}
# Poker winnings from Monday to Friday:
poker.vector <- c(140,-50,20,-120,240)
# Roulette winnings form Monday to Friday:
roulette.vector <- c(-24,-50,100,-350,10)
# Name poker and roulette
days.vector <- c("Monday","Tuesday","Wednesday","Thursday","Friday")
names(roulette.vector) <- names(poker.vector) <- days.vector
# Define new variable based on a selection:
poker.midweek
# Show me the
poker.midweek
```
*** =solution
```{r eval=FALSE}
# Poker winnings from Monday to Friday:
poker.vector <- c(140,-50,20,-120,240)
# Roulette winnings form Monday to Friday:
roulette.vector <- c(-24,-50,100,-350,10)
# Name poker and roulette
days.vector <- c("Monday","Tuesday","Wednesday","Thursday","Friday")
names(roulette.vector) <- names(poker.vector) <- days.vector
# Define new variable based on a selection:
poker.midweek <- poker.vector[ c(2,3,4) ]
# Show me the
poker.midweek
```
*** =sct
```{r eval=FALSE}
names <- c("poker.vector","poker.midweek")
values <- c("poker.vector","poker.vector[ c(2,3,4) ]")
DM.result <- closed_test( names, values )
```
*** =pre_exercise_code
```{r eval=FALSE}
```
---
## Vector selection: the good times (3)
Selecting multiple elements of poker.midweek with `c(2,3,4)` is repetitive, boring, and not very convenient. But many statisticians are lazy people by nature, so they created an easier way to do this: `c(2,3,4)` can be generated faster by `2:4`, which generates a vector with all natural numbers from 2 upto 4.
You can thus select the elements of poker.midweek by typing: `poker.vector[2:4]`. Notice how the vector `2:4` is placed between the square brackets to select element 2 upto 4.
*** =instructions
1. Assign to `roulette.selection.vector` the results from Tuesday upto Friday by making use of `:`.
*** =hint
Assign to `roulette.selection.vector` a selection of `roulette.vector` by placing `2:5` between square brackets.
*** =sample_code
```{r eval=FALSE}
# Poker winnings from Monday to Friday:
poker.vector <- c(140,-50,20,-120,240)
# Roulette winnings form Monday to Friday:
roulette.vector <- c(-24,-50,100,-350,10)
# Name poker and roulette
days.vector <- c("Monday","Tuesday","Wednesday","Thursday","Friday")
names(roulette.vector) <- names(poker.vector) <- days.vector
# Define new variables containing a selection:
roulette.selection.vector
```
*** =solution
```{r eval=FALSE}
# Poker winnings from Monday to Friday:
poker.vector <- c(140,-50,20,-120,240)
# Roulette winnings form Monday to Friday:
roulette.vector <- c(-24,-50,100,-350,10)
# Name poker and roulette
days.vector <- c("Monday","Tuesday","Wednesday","Thursday","Friday")
names(roulette.vector) <- names(poker.vector) <- days.vector
# Define new variable based on a selection:
roulette.selection.vector <- roulette.vector[2:5]
#Show me the
roulette.selection.vector
```
*** =sct
```{r eval=FALSE}
DM.result <- code_test("roulette.vector[2:5]")
```
*** =pre_exercise_code
```{r eval=FALSE}
```
---
## Vector selection: the good times (4)
Another way to tackle the previous exercise is by making use of the element names (Monday, Tuesday...) instead of the numeric positions. For example, when typing `poker.vector["Monday"]`, the first element of `poker.vector` is selected since we gave it the name "Monday" previously.
Just as you did in the previous exercise with numerics, you can also use the element names to select multiple elements with: `poker.vector[c("Monday","Tuesday")]`.
*** =instructions
1. Calculate the average gain you obtained on poker during the first three days of the week by selecting these elements with the help of the names. Assign this value to `average.midweek.gain`. To get the average of a vector, you can use `mean(vector.name)`.
*** =hint
To get the mean of the vector poker, you can use `mean(poker.vector)`. Now, you don't need the mean of all poker elements, only of the first three days.
*** =sample_code
```{r eval=FALSE}
# Poker winnings from Monday to Friday:
poker.vector <- c(140,-50,20,-120,240)
# Roulette winnings form Monday to Friday:
roulette.vector <- c(-24,-50,100,-350,10)
# Name poker.vector and roulette.vector
days.vector <- c("Monday","Tuesday","Wednesday","Thursday","Friday")
names(roulette.vector) <- names(poker.vector) <- days.vector
average.midweek.gain <-
# Show me the
average.midweek.gain
```
*** =solution
```{r eval=FALSE}
# Poker winnings from Monday to Friday:
poker.vector <- c(140,-50,20,-120,240)
# Roulette winnings form Monday to Friday:
roulette.vector <- c(-24,-50,100,-350,10)
# Name poker and roulette
days.vector <- c("Monday","Tuesday","Wednesday","Thursday","Friday")
names(roulette.vector) <- names(poker.vector) <- days.vector
average.midweek.gain <- mean( poker.vector[c("Monday","Tuesday","Wednesday")] )
# Show me the
average.midweek.gain
```
*** =sct
```{r eval=FALSE}
names <- c("poker.vector","average.midweek.gain")
values <- c("poker.vector",'mean( poker.vector[c("Monday","Tuesday","Wednesday")] )')
DM.result <- closed_test(names,values)
```
*** =pre_exercise_code
```{r eval=FALSE}
```
---
## Selection by comparison - Step 1
By making use of comparison operators, we can approach the previous question in a more pro-active way.
The (logical) comparison operators known to R are:
- `<` for less than
- `>` for greater than
- `>=` for greater than or equal to
- `==` for equal to each other
- `!=` not equal to each other
As seen in the previous chapter, stating `6 > 5` returns TRUE. The cool thing about R, is that you can use these comparison operators also on vectors. For example, the statement `c(4,5,6) > 5` returns: `FALSE FALSE TRUE`. In other words, you test for every element of the vector if the condition stated by the comparison operator is true or false. Don't take our word for it, try it in the console ;-).
*** =instructions
1. Check if your poker winnings are positive on the different days of the week (i.e. > 0), and assign this to `selection.vector`.
*** =hint
To check for which days your poker gains are positive, R should check for each element of `poker.vector` whether it is larger than zero. `Some.vector > 0` is the way to tell R what you're after.
*** =sample_code
```{r eval=FALSE}
# Poker winnings from Monday to Friday:
poker.vector <- c(140,-50,20,-120,240)
# Roulette winnings form Monday to Friday:
roulette.vector <- c(-24,-50,100,-350,10)
# Name poker and roulette
days.vector <- c("Monday","Tuesday","Wednesday","Thursday","Friday")
names(roulette.vector) <- names(poker.vector) <- days.vector
# What days of the week did you make money on poker?
selection.vector <-
# Show me
selection.vector
```
*** =solution
```{r eval=FALSE}
# Poker winnings from Monday to Friday:
poker.vector <- c(140,-50,20,-120,240)
# Roulette winnings form Monday to Friday:
roulette.vector <- c(-24,-50,100,-350,10)
# Name poker and roulette
days.vector <- c("Monday","Tuesday","Wednesday","Thursday","Friday")
names(roulette.vector) <- names(poker.vector) <- days.vector
# What days of the week did you make money on poker?
selection.vector <- poker.vector > 0
# Show me
selection.vector
```
*** =sct
```{r eval=FALSE}
names <- c("poker.vector","selection.vector")
values <- c("poker.vector",'poker.vector > 0')
DM.result <- closed_test(names,values)
```
*** =pre_exercise_code
```{r eval=FALSE}
```
---
## Selection by comparison - Step 2
Working with comparisons will make your data analytical life easier. Instead of selecting a subset of days to investigate yourself (like before), you can simply ask R to return only those days where you realized a positive return for poker.
In the previous exercises you used: `selection.vector <- poker.vector > 0` to find the days on which you had a positive poker return. Now, you would like to know not only the days on which you won, but also how much you won on those days.
You can select the desired elements, by putting `selection.vector` between square brackets when selecting from `poker.vector`. This works, because by default R only selects those elements where selection.vector is TRUE. For `selection.vector` this means where `poker.vector > 0`.
*** =instructions
1. Assign to variable `poker.winning.days`, the amounts you won on the profitable days.
*** =hint
Use `selection.vector` to select the desired elements from `poker.vector`, and assign the result to `poker.winning.days`.
*** =sample_code
```{r eval=FALSE}
# Poker winnings from Monday to Friday:
poker.vector <- c(140,-50,20,-120,240)
# Roulette winnings form Monday to Friday:
roulette.vector <- c(-24,-50,100,-350,10)
# Name poker and roulette
days.vector <- c("Monday","Tuesday","Wednesday","Thursday","Friday")
names(roulette.vector) <- names(poker.vector) <- days.vector
# What days of the week did you make money on poker?
selection.vector <- poker.vector > 0
# Select from poker.vector these days:
poker.winning.days <-
# Show me
poker.winning.days
```
*** =solution
```{r eval=FALSE}
# Poker winnings from Monday to Friday:
poker.vector <- c(140,-50,20,-120,240)
# Roulette winnings form Monday to Friday:
roulette.vector <- c(-24,-50,100,-350,10)
# Name poker and roulette
days.vector <- c("Monday","Tuesday","Wednesday","Thursday","Friday")
names(roulette.vector) <- names(poker.vector) <- days.vector
# What days of the week did you make money on poker?
selection.vector <- poker.vector > 0
# Select from poker.vector these days:
poker.winning.days <- poker.vector[ selection.vector ]
# Show me
poker.winning.days
```
*** =sct
```{r eval=FALSE}
names <- c("poker.vector","poker.winning.days")
values <- c("poker.vector",'poker.vector[ selection.vector ]')
DM.result <- closed_test(names,values)
```
*** =pre_exercise_code
```{r eval=FALSE}
```
---
## Advanced selection
Just like you did for poker, you also want those days where you realized a positive return for roulette.
*** =instructions
1. Assign to variable `roulette.winning.days`, how much you made on the days you ended positively for roulette.
*** =hint
Have a look at the previous exercise, and do the similar analysis but now for `roulette.vector`.
*** =sample_code
```{r eval=FALSE}
# Poker winnings from Monday to Friday:
poker.vector <- c(140,-50,20,-120,240)
# Roulette winnings form Monday to Friday:
roulette.vector <- c(-24,-50,100,-350,10)
# Name poker and roulette
days.vector <- c("Monday","Tuesday","Wednesday","Thursday","Friday")
names(roulette.vector) <- names(poker.vector) <- days.vector
# What days of the week did you make money on roulette?
# Select from roulette.vector these days:
# Show me
roulette.winning.days
```
*** =solution
```{r eval=FALSE}
# Poker winnings from Monday to Friday:
poker.vector <- c(140,-50,20,-120,240)
# Roulette winnings form Monday to Friday:
roulette.vector <- c(-24,-50,100,-350,10)
# Name poker and roulette
days.vector <- c("Monday","Tuesday","Wednesday","Thursday","Friday")
names(roulette.vector) <- names(poker.vector) <- days.vector
# What days of the week did you make money on roulette?
selection.vector <- roulette.vector > 0
# Select from roulette.vector these days:
roulette.winning.days <- roulette.vector[ selection.vector ]
# Show me
roulette.winning.days
```
*** =sct
```{r eval=FALSE}
names <- c("roulette.vector","roulette.winning.days")
values <- c("roulette.vector",'roulette.vector[ selection.vector ]')
DM.result <- closed_test(names,values)
```
*** =pre_exercise_code
```{r eval=FALSE}
```