-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr.ex.R
2354 lines (2187 loc) · 83.4 KB
/
r.ex.R
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
1000
##########################
## R training exercises ##
##########################
r.ex <- function(){
# DEVELOPED FOR BH MSU R TRAINING
# EXERCISES ARE PROVIDED BELOW FOR PRACTICE
# AUTHOR: MINGMIN
message("Welcome to MSU R training exercise system!")
cat("\n")
message("Every training session comes up with corresponding exercises.")
message("No score will be granted.")
message("Just practice and make sure you fully understand the content of training.")
message("You can press 'Esc' anywhere when you want to exit.")
cat("\n")
na <- readline("Press 'ENTER' to continue...")
cat("\n")
message("You can try 3 times for each exercise.")
message("If you failed all, correct answer will be printed.")
message("Don't be shy, just try! :P")
# when users want to do extra exercises, check and adapt corresponding sourcing codes.
call.extra <- function(){
repeat{
cat("Which system are you using?",
" 1. WINDOWS",
" 2. OS X", "", sep="\n")
syst <- as.numeric(readline("Your system? "))
cat("\n")
if(!syst %in% as.character(c(1,2))){
message("Please enter valid number!")
}else if(syst == 1){
source("https://raw.githubusercontent.com/elliott828/boulot-test/master/extra.ex.R")
break
}else if(syst == 2){
if(!"RCurl" %in% installed.packages()){install.packages("RCurl")}
library(RCurl)
ex <- getURL("https://raw.githubusercontent.com/elliott828/boulot-test/master/extra.ex.R",
ssl.verifypeer=0L, followlocation=1L)
writeLines(ex, "temp.R")
source("temp.R")
break
}
}
}
no.ex <- function(){
cat("\n")
repeat{
cat("Please choose the set of training exercises listed below:",
"PART I - BASIC CONCEPT",
" 1.1 - Basic Intro & Sequence",
" 1.2 - Vector & Factor",
" 1.3 - Matrix, Data Frame & Subsetting",
" 1.4 - List & Missing Values",
"",
"PART II - ADVANCED OPERATION",
" 2.1 - Control Flow & Self-Defined Function",
" 2.2 - Input & Output",
" 2.3 - Fundamental Statistics",
" 2.4 - Linear Regression Model",
"",
"PART III - PLOTTING & APPLICATION",
" 3.1 - Basic Intro to Graphic Tools",
" 3.2 - MSU Modeling Tool: AutoReg()",
"",
"0. Exit this exercise program",
" ", sep = "\n")
no.set <- readline("Please enter the session number: ")
# check1 <- regular expression
# check2 <- no.set %in% c(seq(1.1,1.5,0.1),seq(2.1,2.4,0.1),seq(3.1,3.2,0.1))
if (no.set %in% c(0, seq(1.1,1.4,0.1),seq(2.1,2.4,0.1),seq(3.1,3.2,0.1))){
break
}else{
message("Just enter number of exercise set listed above please.")
message("i.e.: enter '1.2' (without quotation mark) for the exercise of 'Vector & List'.")
cat("\n")
}
}
no.set <- as.numeric(no.set)
if (no.set == 0){
cat("\n")
message("Bye-bye~~")
cat("\n")
# return(the.end())
}else if (no.set == 1.1){
#---------------#
# Exercises 1.1 #
#---------------#
cat("\n")
msm <- "Training Exercise 1.1 - Basic Intro & Sequence"
message(msm)
message(rep("-", nchar(msm)))
message("There are 5 questions below for this training session.")
cat("\n")
# Question 1
q1 <- 1 # initialize the count of answer times
repeat{
cat("Q1. Please enter the result of '3^2 + 17%%3'",
"",
sep = "\n")
ans <- readline("Your answer: ")
ans.bis <- as.numeric(ans)
if (ans.bis == 11){
message("Correct! Let's move to the next question.")
cat("\n")
break
}else{
q1 <- q1 + 1
if(q1>3){
message("Oops, game over! Correct answer below:")
message("3^2 + 17%%3 = 11")
message("Let's move to the next question.")
cat("\n")
break
}else{
message("Bang! Wrong answer! Try again~")
message("Just enter a number, without any other characters.")
cat("\n")
}
}
}
# Question 2
q2 <- 1
repeat{
cat("Q2. Follow the steps below:",
"> apple <- 5",
"> banana <- 6",
"> pear <- 7",
"> fruit <- apple + banana + pear",
"What's the value of variable 'fruit'?",
"",
sep = "\n")
ans <- readline("Your answer: ")
ans.bis <- as.numeric(ans)
if (ans.bis == 18){
message("Hoorey~ Corret answer!")
cat("\n")
break
}else{
q2 <- q2 + 1
if (q2>3){
message("Stop and think, and try it again if you want after all exercises finished.")
message("The value of variable 'fruit' is 18.")
cat("\n")
break
}else{
message("Bang-bang! Try again please")
cat("\n")
}
}
}
# Question 3
q3 <- 1
repeat{
cat("Q3. I want to create a vector with 10 elements:",
" 4 8 12 16 20 24 28 32 36 40",
"Which method below CANNOT help me to realize that?",
" 1. c(4, 8, 12, 16, 20, 24, 28, 32, 36, 40)",
" 2. seq(4, 40, 4)",
" 3. seq(40, 4, 4)",
" 4. 1:10 * 4",
" 5. seq(4, 40, length = 10)",
"",
sep = "\n")
ans <- readline("Your answer (enter the option number): ")
if (ans == "3"){
message("Great! You still have 2 questions to answer!")
cat("\n")
break
}else if(ans %in% c("1", "2", "4", "5")){
q3 <- q3 + 1
if (q3>3){
message("You have run out of 3 trials...")
message("The correct answer is '3. seq(40 ,4, 4)', that leads to error message.")
cat("\n")
break
}else{
message("Ah-oh~ try again please~")
cat("\n")
}
}else{
cat("\n")
message("Only numbers between 1 and 5 are acceptable!")
cat("\n")
}
}
# Question 4
q4 <- 1
repeat{
cat("Q4. Which expression below is FALSE?",
" 1. a <- 1:5; seq(along = a) gets the same result as seq_along(a)",
" 2. pi:5 creates vector: 3.141593 4.141593",
" 3. a <- 1:5; b <- seq(2,10,2); then b/a equals to 2",
" 4. a <- 1:5; b <- 6:10; then b%*%a equals to 1*1 matrice",
" 5. a <- 2:9; b <- 3:5; a+b leads to an warning message",
"",
sep = "\n")
ans <- readline("Your answer (enter the option number): ")
if (ans == "3"){
message("One question to go! Ganbade!")
cat("\n")
break
}else if(ans %in% c("1", "2", "4", "5")){
q4 <- q4 + 1
if (q4>3){
message("Oulala~no more chance for this question~")
message("The correct answer is the 3rd option.")
message("The division of 2 numeric vector (same length) leads to a vector of same length")
cat("\n")
break
}else{
message("Come on~ Try again!")
cat("\n")
}
}else{
cat("\n")
message("Only numbers between 1 and 5 are acceptable!")
cat("\n")
}
}
# Question 5
q5 <- 1
repeat{
cat("Q5. Vector computation",
"> a <- 1:5",
"> b <- 3:7",
"> c <- 6:10",
"What is the result of 'c * 2 + b %/% a + sqrt(9) + 1:10'?",
" 1. 19 21 23 26 29 24 26 28 31 34",
" 2. 16 19 24 27 30 21 24 29 32 35",
" 3. 19 21 23 26 29",
" 4. 16 19 24 27 30",
"",
sep = "\n")
ans <- readline("Your answer: ")
if (ans == "1"){
message("Congratulations! You have finished the exercise 1.1!")
message("See you in next set of exercises~")
cat("\n")
break
}else if(ans %in% c("2", "3", "4")){
q5 <- q5 + 1
if (q5>3){
message("You have tried 3 times...")
message("The correct answer is '1. 19 21 23 26 29 24 26 28 31 34'")
message("You can enter 'r.ex()' again to do this exercise again.")
message("See you around~")
cat("\n")
break
}else{
message("Try~~~~again~~~~~")
cat("\n")
}
}else{
cat("\n")
message("Only numbers between 1 and 4 are acceptable!")
cat("\n")
}
}
cat("Exercise 1.1 statistics:",
paste("You made ", q1-1, " mistake", if(q1<3){""}else{"s"}, " for Q1;", sep =""),
paste("You made ", q2-1, " mistake", if(q2<3){""}else{"s"}, " for Q2;", sep =""),
paste("You made ", q3-1, " mistake", if(q3<3){""}else{"s"}, " for Q3;", sep =""),
paste("You made ", q4-1, " mistake", if(q4<3){""}else{"s"}, " for Q4;", sep =""),
paste("You made ", q5-1, " mistake", if(q5<3){""}else{"s"}, " for Q5;", sep =""),
"", sep = "\n")
if(q1+q2+q3+q4+q5-5 == 0){
message("You made no mistake at all in this set of exercise! Congratulations!")
}
repeat{
cho <- readline("Do you want to do this exercise again or try other exercise (Y/N)? ")
cat("\n")
if (toupper(cho) == "Y"){
return(no.ex())
}else if (toupper(cho) == "N"){
message("c ya~")
cat("\n")
break
}else{
message("Please only enter 'Y' or 'N'")
}
}
}else if(no.set == 1.2){
#---------------#
# Exercises 1.2 #
#---------------#
cat("\n")
msm <- "Training Exercise 1.2 - Vector & Factor"
message(msm)
message(rep("-", nchar(msm)))
message("There are 5 questions below for this training session.")
cat("\n")
# Question 1
q1 <- 1 # initialize the count of answer times
repeat{
cat("Q1. > vect <- c(3, 1, 2, 4)",
"which statement below is right?",
" 1. the data type of 'vect' is integer",
" 2. the data type of 'vect' is numeric",
" 3. > vect1 <- c(vect, FALSE); then vect1 is a vector of 5 logical values",
" 4. > vect2 <- c(vect, FALSE, 'A'); vect2 is still a numeric vector",
"",
sep = "\n")
ans <- readline("Your answer: ")
# ans.bis <- as.numeric(ans)
if (ans == "2"){
message("Correct! Let's move to the next question.")
cat("\n")
break
}else if (ans %in% c("1", "3", "4")){
q1 <- q1 + 1
if(q1>3){
message("Oops, game over! Correct answer below:")
message("The data type of 'vect' is numeric!")
message("Let's move to the next question.")
cat("\n")
break
}else{
message("Bang! Wrong answer! Try again~")
message("Just enter a number between 1 and 4, without any other characters.")
cat("\n")
}
}else{
cat("\n")
message("Only numbers between 1 and 4 are acceptable!")
cat("\n")
}
}
# Question 2
q2 <- 1
repeat{
cat("Q2. 2 vectors of daily spends are created:",
"> XiaoJuan <- c(125, 130, 135, 242, 163)",
"> XiaoFang <- c(132, 184, 162, 212, 182)",
"",
"Which of the following is right?",
" 1. XiaoJuan > XiaoFang",
" 2. sum(XiaoJuan) > sum(XiaoFang)",
" 3. Total spends can be calculated by command: > sum(XiaoJuan, XiaoFang)",
" 4. Total spends can be calculated by command: > XiaoJuan + XiaoFang",
"",
sep = "\n")
ans <- readline("Your answer: ")
if (ans == "3"){
message("Hoorey~ Corret answer!")
cat("\n")
break
}else if (ans %in% c("1", "2", "4")){
q2 <- q2 + 1
if (q2>3){
message("Stop and think, and try it again if you want after all exercises finished.")
message("The only right answer is 3.")
cat("\n")
break
}else{
message("Bang-bang! Try again please")
cat("\n")
}
}else{
cat("\n")
message("Only numbers between 1 and 4 are acceptable!")
cat("\n")
}
}
# Question 3
q3 <- 1
repeat{
cat("Run the following commands:",
'> random <- c(400, -80, -40, 200, -120)',
'> names(random) <- c("Mon", "Tue", "Wed", "Thu", "Fri")',
"",
"Which option is right?",
" 1. > summary(random); and we get:",
" Min. 1st Qu. Median Mean 3rd Qu. Max.",
" -120 -80 -40 72 200 400 ",
"",
" 2. > table(random); and we get:",
" random",
" Mon Tue Wed Thu Fri ",
" 400 -80 -40 200 -120",
"",
" 3. > levels(random); and we get:",
' [1] "Mon" "Tue" "Wed" "Thu" "Fri"',
"",
" 4. > name(random); and we get:",
' [1] "Mon" "Tue" "Wed" "Thu" "Fri"',
"",
sep = "\n")
ans <- readline("Your answer (enter the option number): ")
if (ans == "1"){
message("Great! You still have 2 questions to answer!")
cat("\n")
break
}else if(ans %in% c("2", "3", "4")){
q3 <- q3 + 1
if (q3>3){
message("You have run out of 3 trials...")
message("The correct answer is 1.")
message(" - Using table() on a vector gets only counts of each value;")
message(" - levels() is used for factor instead of a vector with names;")
message(" - There is no functoin name(), only names() is available. :P")
cat("\n")
break
}else{
message("Ah-oh~ try again please~")
cat("\n")
}
}else{
cat("\n")
message("Only numbers between 1 and 4 are acceptable!")
cat("\n")
}
}
# Question 4
q4 <- 1
repeat{
cat("Q4. Which expression below is FALSE?",
" 1. if 'ordered' is not specified, levels adapt alphabetic order without priority;",
" 2. comparing a ordinal factor with a nominal one leads to an error message;",
" 3. in factor(), parameters 'order' and 'ordered' have different functionality;",
" 4. changing names of levels without considering order may lead to wrong info.",
"",
sep = "\n")
ans <- readline("Your answer (enter the option number): ")
if (ans == "3"){
message("One question to go! Ganbade!")
cat("\n")
break
}else if(ans %in% c("1", "2", "4")){
q4 <- q4 + 1
if (q4>3){
message("Oulala~no more chance for this question~")
message("The correct answer is the 3rd option.")
cat("\n")
break
}else{
message("Come on~ Try again!")
cat("\n")
}
}else{
cat("\n")
message("Only numbers between 1 and 4 are acceptable!")
cat("\n")
}
}
# Question 5
q5 <- 1
repeat{
cat("Q5. Check the commands below:",
'> animal <- c("puppy", "piggy", "donkey", "monkey", "kitty") # Line 1',
"> zoo <- c(rep(c(1, 2), each = 3), 3) # Line 2",
"> names(zoo) <- animal # Line 3",
"> summary(zoo) # Line 4",
"> f.zoo <- factor(zoo, order = T) # Line 5",
"Which line will lead to a problem (not necessarily an error or warning message)?",
"",
sep = "\n")
ans <- readline("Your answer (no. of line): ")
if (ans == "3"){
message("Congratulations! You have finished the exercise 1.2!")
message("See you in next set of exercises~")
cat("\n")
break
}else if(ans %in% c("1", "2", "4")){
q5 <- q5 + 1
if (q5>3){
message("You have tried 3 times...")
message("The correct answer is 3. length of 'zoo' is longer than that of 'animal'.")
message("You can enter 'r.ex()' again to do this exercise again.")
message("See you around~")
cat("\n")
break
}else{
message("Try~~~~again~~~~~")
cat("\n")
}
}else{
cat("\n")
message("Only numbers between 1 and 5 are acceptable!")
cat("\n")
}
}
cat("Exercise 1.2 statistics:",
paste("You made ", q1-1, " mistake", if(q1<3){""}else{"s"}, " for Q1;", sep =""),
paste("You made ", q2-1, " mistake", if(q2<3){""}else{"s"}, " for Q2;", sep =""),
paste("You made ", q3-1, " mistake", if(q3<3){""}else{"s"}, " for Q3;", sep =""),
paste("You made ", q4-1, " mistake", if(q4<3){""}else{"s"}, " for Q4;", sep =""),
paste("You made ", q5-1, " mistake", if(q5<3){""}else{"s"}, " for Q5;", sep =""),
"", sep = "\n")
if(q1+q2+q3+q4+q5-5 == 0){
message("You made no mistake at all in this set of exercise! Congratulations!")
}
repeat{
cho <- readline("Do you want to do this exercise again or try other exercise (Y/N)? ")
cat("\n")
if (toupper(cho) == "Y"){
return(no.ex())
}else if (toupper(cho) == "N"){
message("c ya~")
cat("\n")
break
}else{
message("Please only enter 'Y' or 'N'")
}
}
}else if (no.set == 1.3){
#---------------#
# Exercises 1.3 #
#---------------#
cat("\n")
msm <- "Training Exercise 1.3 - Matrix, Data Frame & Subsetting"
message(msm)
message(rep("-", nchar(msm)))
message("There are 5 exercises and 2 optional ones below for this training session.")
cat("\n")
# Question 1
q1 <- 1 # initialize the count of answer times
repeat{
cat("Q1. Which command CANNOT get the matrix/data frame correctly named?",
" 1. > a <- matrix(1:12, 3, 4,",
" dimnames = c('row1','row2','row3',",
" 'col1','col2','col3','col4'))",
"",
" 2. > a <- matrix(1:12, 3, 4)",
" > rownames(a) <- c('row1','row2','row3')",
" > colnames(a) <- c('col1','col2','col3','col4')",
"",
" 3. > b <- data.frame(matrix(1:12, 3 ,4))",
" > rownames(b) <- c('row1','row2','row3')",
" > colnames(b) <- c('col1','col2','col3','col4')",
"",
" 4. > b <- data.frame(matrix(1:12, 3 ,4))",
" > names(b) <- c('col1','col2','col3','col4')",
" > row.names(b) <- c('row1','row2','row3')",
"",
sep = "\n")
ans <- readline("Your answer: ")
# ans.bis <- as.numeric(ans)
if (ans == "1"){
message("Correct! Let's move to the next question.")
cat("\n")
break
}else if (ans %in% c("2", "3", "4")){
q1 <- q1 + 1
if(q1>3){
message("Oops, game over! Correct answer below:")
message("The value of argument 'dimnames' should be a 'list'!")
message("Let's move to the next question.")
cat("\n")
break
}else{
message("Bang! Wrong answer! Try again~")
message("Just enter a number between 1 and 4, without any other characters.")
cat("\n")
}
}else{
cat("\n")
message("Only numbers between 1 and 4 are acceptable!")
cat("\n")
}
}
# Question 2
q2 <- 1
repeat{
cat("Q2. Which statements below is FALSE:",
" 1. A matrix can only store one data type;",
" 2. Data frame is often used for complicated data analysis because of",
" its capability of storing multiple data types;",
" 3. Functions like colSums(), rowMeans(), etc. can only be used on matrix;",
" 4. Functions data.frame() and as.data.frame() have same effect when we want",
" to coerce a matrix to data frame without considering column names.",
"",
sep = "\n")
ans <- readline("Your answer: ")
if (ans == "3"){
message("Hoorey~ Corret answer!")
cat("\n")
break
}else if (ans %in% c("1", "2", "4")){
q2 <- q2 + 1
if (q2>3){
message("Stop and think, and try it again if you want after all exercises finished.")
message("The only right answer is 3.")
cat("\n")
break
}else{
message("Bang-bang! Try again please")
cat("\n")
}
}else{
cat("\n")
message("Only numbers between 1 and 4 are acceptable!")
cat("\n")
}
}
# Question 3
q3 <- 1
repeat{
cat("Run the following command and create a matrix 'matr':",
'> matr <- matrix(11:22, byrow = T, 3, 4)',
"",
"Which command will get an logical value 'TRUE'?",
" 1. > matr[5] == 15",
" 2. > matr[2,3] == 17",
" 3. > matr[1:3] == c(11,12,13)",
" 4. > matr[,2] - matr[,1] == rep(3,3)",
"",
sep = "\n")
ans <- readline("Your answer (enter the option number): ")
if (ans == "2"){
message("Great! You still have 2 questions to answer!")
cat("\n")
break
}else if(ans %in% c("1", "3", "4")){
q3 <- q3 + 1
if (q3>3){
message("You have run out of 3 trials...")
message("The correct answer is 2.")
message("Pay attention on the direction of data filling!")
cat("\n")
break
}else{
message("Ah-oh~ try again please~")
cat("\n")
}
}else{
cat("\n")
message("Only numbers between 1 and 4 are acceptable!")
cat("\n")
}
}
# Question 4
q4 <- 1
repeat{
cat("Q4. Which expression below is FALSE concerning a dataset 'df' with more than 10",
" observations and more than 5 numeric variables?",
" 1. head(df) returns the first 5 rows of 'df';",
" 2. str(df) returns the structure of 'df';",
" 3. tail(df) == df[(nrow(df)-5):nrow(df),];",
" 4. summary(df) returns summraries (min, max, mean, median, etc.) for each variable.",
"",
sep = "\n")
ans <- readline("Your answer (enter the option number): ")
if (ans == "1"){
message("One question to go! Ganbade!")
cat("\n")
break
}else if(ans %in% c("2", "3", "4")){
q4 <- q4 + 1
if (q4>3){
message("Oulala~no more chance for this question~")
message("The correct answer is the 1st option.")
message("head() returns the first 6 rows of a dataset by default.")
cat("\n")
break
}else{
message("Come on~ Try again!")
cat("\n")
}
}else{
cat("\n")
message("Only numbers between 1 and 4 are acceptable!")
cat("\n")
}
}
# Question 5
q5 <- 1
repeat{
cat("Q5. A data frame is created:",
'> a <- c(1,5,2,3)',
"> b <- c(T,F,T,F)",
"> c <- LETTERS[4:7]",
"> df <- data.frame(a,b,c)",
"",
"Which combination below is TRUE?",
"1. > is.vector(df$a) & is.matrix(df)",
"2. > is.character(df$c) & is.logical(df$b)",
"3. > (is.data.frame(df) | is.character(df$c)) & sum(df$a)==11",
"4. > is.numeric(as.matrix(df)[,1])",
"",
sep = "\n")
ans <- readline("Your answer (no. of line): ")
if (ans == "3"){
message("Congratulations! You have finished the exercise 1.3!")
message("See you in next set of exercises~")
cat("\n")
break
}else if(ans %in% c("1", "2", "4")){
q5 <- q5 + 1
if (q5>3){
message("You have tried 3 times...")
message("The correct answer is 3.")
message(" - df$c is a factor, instead of a character vector;")
message(" - after being turned to matrix, df$a becomes a character vector.")
message("See you around~")
cat("\n")
break
}else{
message("Try~~~~again~~~~~")
cat("\n")
}
}else{
cat("\n")
message("Only numbers between 1 and 4 are acceptable!")
cat("\n")
}
}
cat("Exercise 1.3 statistics:",
paste("You made ", q1-1, " mistake", if(q1<3){""}else{"s"}, " for Q1;", sep =""),
paste("You made ", q2-1, " mistake", if(q2<3){""}else{"s"}, " for Q2;", sep =""),
paste("You made ", q3-1, " mistake", if(q3<3){""}else{"s"}, " for Q3;", sep =""),
paste("You made ", q4-1, " mistake", if(q4<3){""}else{"s"}, " for Q4;", sep =""),
paste("You made ", q5-1, " mistake", if(q5<3){""}else{"s"}, " for Q5;", sep =""),
"", sep = "\n")
if(q1+q2+q3+q4+q5-5 == 0){
message("You made no mistake at all in this set of exercise! Congratulations!")
cat("\n")
}
# optional exercises 1.3
repeat{
opt <- readline("Do you want to do 2 more optional exercises (Y/N)? ")
cat("\n")
if (toupper(opt) == "Y"){
message("Da~da~. You've earned yourself a chance of getting additional points!")
message("Each correct answer will be granted 1 point.")
cat("\n")
message("The extra exercises can only be done when you quit r.ex().")
message("Please read the instructions before you quit r.ex().")
cat("\n")
call.extra()
cat("\n")
na <- readline("Press 'ENTER' to continue...")
cat("\n")
# opt ex 1.3.1
extra1.3.1 <- c("SESSION 1.3, EXTRA EX.1",
"PLEASE FOLLOW THE INSTRUCTION:",
"1. Call the built-in dataset: mtcars;",
"2. Type '?mtcars' to read the introduction of this dataset;",
"3. Use head(), tail(), str() to check its basic structure;",
"4. Sorting the DATASET by the variable mpg in DESCENDING order,",
" (the largest value of miles/gallon is put in the 1st place),",
" save the reordered dataset under data frame name 'mtcar1';",
"5. Type the 2 command lines as below (ignore the symbol '>'):",
" > write.csv(mtcar1, 'mtcar1.csv')",
" > extra.ex()")
cat(extra1.3.1, "", sep = "\n")
write.table(extra1.3.1, file="instruction_ex1-3-1.txt", eol="\n",
row.names = F, col.names = F, quote = F)
message("You can also find this instruction in your working directory")
message("under the name of 'instruction_ex1-3-1.txt'")
cat("\n")
message("You only have one chance to submit this exercise. :P")
message("But don't be scared, no point will be substracted even you submit wrong answer.")
cat("\n")
na <- readline("If you are clear about the ex.1, press 'ENTER' to read ex.2...")
cat("\n")
# opt ex 1.3.2
extra1.3.2 <- c("SESSION 1.3, EXTRA EX.2",
"PLEASE FOLLOW THE INSTRUCTION:",
"1. Call the built-in dataset: mtcars;",
"2. Subset a dataset from mtcars which meets conditions below:",
" a) Allocate the top and bottom 3 cars by invesgating their horsepower;",
" b) Keep 6 information in the subsetted dataset:",
" * Milse/(US) gallon",
" * Number of cylinders",
" * Gross horsepower",
" * Weight",
" * 1/4 mile time",
" * V/S",
"3. Save the subsetted dataset under data frame name 'mtcar2';",
"4. Type the 2 command lines as below (ignore the symbol '>'):",
" > write.csv(mtcar2, 'mtcar2.csv')",
" > extra.ex()")
cat(extra1.3.2, "", sep = "\n")
write.table(extra1.3.2, file="instruction_ex1-3-2.txt", eol="\n",
row.names = F, col.names = F, quote = F)
message("You can also find this instruction in your working directory")
message("under the name of 'instruction_ex1-3-2.txt'")
cat("\n")
message("You only have one chance to submit this exercise. :P")
message("Don't mix up the order of variables in this dataset.")
message("Don't type extra.ex() before you are 100% sure about your answer.")
cat("\n")
message("ENJOY THE EXERCISE LAH~")
cat("\n")
break
}else if (toupper(opt) == "N"){
break
}else{
message("Please only enter 'Y' or 'N'!")
}
}
repeat{
cho <- readline("Do you want to do this exercise again or try other exercise (Y/N)? ")
cat("\n")
if (toupper(cho) == "Y"){
return(no.ex())
}else if (toupper(cho) == "N"){
message("c ya~")
cat("\n")
break
}else{
message("Please only enter 'Y' or 'N'!")
}
}
}else if (no.set == 1.4){
#---------------#
# Exercises 1.4 #
#---------------#
cat("\n")
msm <- "Training Exercise 1.4 - List & Missing Values"
message(msm)
message(rep("-", nchar(msm)))
message("There are 4 exercises below for this training session.")
cat("\n")
# Question 1
q1 <- 1 # initialize the count of answer times
repeat{
cat("Q1. Which statement about 'list' is CORRECT:",
" 1. A list can only store 1 data type;",
" 2. Elements of list should be of same length;",
" 3. Elements of list can be subsetted by both [[ ]] and [] operators;",
" 4. A list cannot store another list.",
"",
sep = "\n")
ans <- readline("Your answer: ")
# ans.bis <- as.numeric(ans)
if (ans == "3"){
message("Correct! Let's move to the next question.")
cat("\n")
break
}else if (ans %in% c("1", "2", "4")){
q1 <- q1 + 1
if(q1>3){
message("Oops, game over! Correct answer is 3!")
message("Let's move to the next question.")
cat("\n")
break
}else{
message("Bang! Wrong answer! Try again~")
message("Just enter a number between 1 and 4, without any other characters.")
cat("\n")
}
}else{
cat("\n")
message("Only numbers between 1 and 4 are acceptable!")
cat("\n")
}
}
# Question 2
q2 <- 1
repeat{
cat("Q2. Create a list by code below:",
" > Intro <- 'The most popular soccer stars!'",
" > SoccerStars <- matrix(c('C.Ronaldo','Messi','Roben','Hazard',",
" 28, 26, 30, 23, 15, 12, 8, 6),",
" nrow = 4, ncol = 3,",
" dimnames = list(1:4,",
" c('Names','Age','Goals')))",
" > Nationality <- list('They are from:',",
" c('Portugal','Argentina','Netherland','Belgium'))",
" > Soccer <- list(INTRO=Intro, STARS=SoccerStars, NATION=Nationality)",
"",
" Which command can get the age of Roben?",
" 1. > Soccer[2][3,2]",
" 2. > Soccer[2]$Age[3]",
" 3. > Soccer$STARS[3,2]",
" 4. > Soccer[['STARS']][3,][,2]",
"",
sep = "\n")
ans <- readline("Your answer: ")
if (ans == "3"){
message("Hoorey~ Corret answer!")
cat("\n")
break
}else if (ans %in% c("1", "2", "4")){
q2 <- q2 + 1
if (q2>3){
message("Stop and think, and try it again if you want after all exercises finished.")
message("The only right answer is 3.")
cat("\n")
break
}else{
message("Bang-bang! Try again please")
cat("\n")
}
}else{
cat("\n")
message("Only numbers between 1 and 4 are acceptable!")
cat("\n")
}
}
# Question 3
q3 <- 1
repeat{
cat("Which command below generates error message?",
' > matr <- matrix(rep(LETTERS[1:6],2), byrow = T, 3, 4)',
' > vect <- vector("a", b, 1, T)',
' > fact <- factor(c(LETTERS[3:1],T,1),ordered = T)',
' > list <- list("This is a test", rep(c("Rock","Roll"),3))',
'',
sep = "\n")
ans <- readline("Your answer (enter the option number): ")
if (ans == "2"){
message("Great! One question to go!")
cat("\n")
break
}else if(ans %in% c("1", "3", "4")){
q3 <- q3 + 1
if (q3>3){
message("You have run out of 3 trials...")
message("The correct answer is 2.")
message("The 2nd element is not a valid value!")
cat("\n")
break
}else{
message("Ah-oh~ try again please~")
cat("\n")
}
}else{
cat("\n")
message("Only numbers between 1 and 4 are acceptable!")
cat("\n")
}
}
# Question 4
q4 <- 1
repeat{
cat("Q4. There is a vector with missing value:",
" > Ohlala <- sample(c(rnorm(1000),rep(NA,1000)),20)",
"",
" Which command CAN replace the NA by the average value of valid numbers?",
" 1. > Ohlala[is.na(Ohlala)] <- mean(Ohlala, na.rm = T)",
" 2. > Ohlala[!complete.cases(Ohlala)] <- mean(Ohlala)",