-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path1_2_ST-R.tex
1316 lines (954 loc) · 29.8 KB
/
1_2_ST-R.tex
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
%\documentclass[mathserif]{beamer}
\documentclass[handout]{beamer}
%\usetheme{Goettingen}
\usetheme{Warsaw}
%\usetheme{Singapore}
%\usetheme{Frankfurt}
%\usetheme{Copenhagen}
%\usetheme{Szeged}
%\usetheme{Montpellier}
%\usetheme{CambridgeUS}
%\usecolortheme{}
%\setbeamercovered{transparent}
\usepackage[english, activeacute]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amsmath, amssymb}
\usepackage{dsfont}
\usepackage{graphics}
\usepackage{cases}
\usepackage{graphicx}
\usepackage{pgf}
\usepackage{epsfig}
\usepackage{amssymb}
\usepackage{multirow}
\usepackage{amstext}
\usepackage[ruled,vlined,lined]{algorithm2e}
\usepackage{amsmath}
\usepackage{epic}
\usepackage{epsfig}
\usepackage{fontenc}
\usepackage{framed,color}
\usepackage{palatino, url, multicol}
\usepackage{listings}
%\algsetup{indent=2em}
\vspace{-0.5cm}
\title{Introduction to R}
\vspace{-0.5cm}
\author[Felipe Bravo Márquez]{\footnotesize
%\author{\footnotesize
\textcolor[rgb]{0.00,0.00,1.00}{Felipe José Bravo Márquez}}
\date{ \today }
\begin{document}
\begin{frame}
\titlepage
\end{frame}
%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Introduction}
\begin{frame}{The R project for computational statistics}
\begin{figure}[h!]
\centering
\includegraphics[scale=0.6]{pics/Rlogo.png}
\end{figure}
\scriptsize{
\begin{itemize}
\item R is a free statistical programming environment: \url{http://www.r-project.org/}
\item It allows to manipulate and store data in an effective way.
\item R is a complete programming language: variables, loops, conditions, functions.
\item It provides many libraries that implement pretty much any statistical method.
\item These libraries together with their dependencies are organized in a repository called \textbf{CRAN}: \url{http://cran.r-project.org/}
\end{itemize}
}
\end{frame}
\begin{frame}{Why use R?}
\scriptsize{
\begin{itemize}
\item R is free software unlike Matlab, SPSS, STATA.
\item It is available for many operating systems: Windows, MAC OS X, Linux.
\item As of 2013 \textbf{KDnuggets} survey showed R as the preferred programming language for performing data analysis, data mining and data science.
\item \url{http://www.kdnuggets.com/2013/08/languages-for-analytics-data-mining-data-science.html}
\item Today Python has become a very strong competitor.
\end{itemize}
}
\begin{figure}[h!]
\centering
\includegraphics[scale=0.3]{pics/rpoll.png}
\end{figure}
\end{frame}
\begin{frame}{RStudio}
\scriptsize{
\begin{itemize}
\item R works through the command line.
\item To work in a more friendly environment we will use RStudio.
\item It is also free and can be downloaded for different operating systems at this link: \url{https://www.rstudio.com/products/rstudio/download/}
\end{itemize}
}
\begin{figure}[h!]
\centering
\includegraphics[scale=0.2]{pics/rstudio.png}
\end{figure}
\end{frame}
\section{R basic commands}
\begin{frame}[fragile]{R as a calculator}
\begin{verbatim}
> 4*5
[1] 20
> 2^3
[1] 8
> exp(-5)
[1] 0.006737947
> log(4)
[1] 1.386294
\end{verbatim}
\footnotemark{The following slides are based on \cite{venables2009introduction}}
\end{frame}
\begin{frame}[fragile]{Declaring Variables}
\scriptsize{
\begin{itemize}
\item Variables can be assigned using \verb+<-+ , \verb+=+ or the function \verb+assign+.
\begin{verbatim}
a<-1
b=3
assign("three",3)
d<-a+b
ver<-T # equivalent to TRUE
word<-"hello"
\end{verbatim}
\item By convention we use the first form (\verb+<-+).
\item Variables can be of class \textbf{numeric}, \textbf{factor}, \textbf{character}, \textbf{logical}, among others.
\item To see the type of a variable we use the command \verb+class+.
\begin{verbatim}
> class(a)
[1] "numeric"
> class(ver)
[1] "logical"
> class(word)
[1] "character"
\end{verbatim}
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Functions}
\scriptsize{
\begin{itemize}
\item Functions are declared as variables and are created with the expression \textbf{function}:
\begin{verbatim}
my.sum<-function(a=2,b=1){
return(a+b);
}
fac<-function(n){
ifelse(n==1,return(1),return(n*fac(n-1)))
}
\end{verbatim}
\item Function parameters can be declared with a specific value to be used as default values when we do not provide values for those parameters:
\begin{verbatim}
> my.sum(3,4)
[1] 7
> my.sum()
[1] 3
\end{verbatim}
\item The functions are of the type \textbf{function}:
\begin{verbatim}
> class(my.sum)
[1] "function"
\end{verbatim}
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Help and Workspace}
\scriptsize{
\begin{itemize}
\item To read documentation about a function we use either \textbf{help} or \textbf{?}:
\begin{verbatim}
help(ls)
?ls
#for a particular command
help("for")
\end{verbatim}
\item All variables are stored in my \textbf{workspace} environment. To list them we use the command \textbf{objects} or \textbf{ls}. To delete a variable we use \textbf{rm}:
\begin{verbatim}
objects()
ls()
rm(a)
#to delete them all
rm(list=ls())
\end{verbatim}
\item We can save all my workspace variables in a file and then retrieve my work in a future session:
\begin{verbatim}
save.image("myworkspace.RData")
# we can load it in a new session
load("myworkspace.RData")
\end{verbatim}
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Vectors}
\scriptsize{
\begin{itemize}
\item To work with collections of elements we declare \textbf{vectors} which are constructed with the command \textbf{c}:
\begin{verbatim}
ages<-c(21,33,12,34,23,70,90,80,7,29,14,2,
88,11,55,24,13,11,56,28,33)
\end{verbatim}
\item To get the length of a vector we use the command \textbf{length}, then to get the sum of all elements we use \textbf{sum}:
\begin{verbatim}
> a.sum<-sum(ages)
> a.length<-length(ages)
> a.sum
[1] 734
> a.length
[1] 21
\end{verbatim}
\item If we operate a vector by a scalar this value is recycled for all elements of the vector:
\begin{verbatim}
> numbers<-c(1,2,3)
> numbers+3
[1] 4 5 6
> numbers*5
[1] 5 10 15
\end{verbatim}
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Vectors (2)}
\scriptsize{
\begin{itemize}
\item Calculate the mean and variance of the vector ages using the commands \textbf{sum} and \textbf{length} based on the following equations:
\begin{equation}
\text{mean}(\text{ages})=\frac{\sum_{i=1}^n\text{ages}_{i}}{n}
\end{equation}
\begin{equation}
\text{variance}(\text{ages})=\frac{\sum_{i=1}^n(\text{ages}_{i}-\text{media}(\text{ages}))^2 }{n-1}
\end{equation}
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Vectors (3)}
\scriptsize{
\begin{itemize}
\item Answer:
\begin{verbatim}
> a.mean<-sum(ages)/length(ages)
> a.mean
[1] 34.95238
> a.var<-sum((ages-a.mean)^2)/(length(ages)-1)
> a.var
[1] 747.9476
\end{verbatim}
\item R provides \textbf{mean} and \textbf{var} functions:
\begin{verbatim}
> mean(ages)
[1] 34.95238
> var(ages)
[1] 747.9476
\end{verbatim}
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Vectors (4)}
\scriptsize{
\begin{itemize}
\item When we construct vectors with elements of different types, R converts them all to a single type:
\begin{verbatim}
> c("hello",2,T)
[1] "hello" "2" "TRUE"
> c(TRUE,FALSE,500)
[1] 1 0 500
\end{verbatim}
\item The elements of a vector can be declared with names and then retrieved with the \textbf{names} command:
\begin{verbatim}
> grades<-c(Juan=4.5,Luis=6.2,Romina=3.9,Felipe=2.8,Mariana=6.7)
> names(grades)
[1] "Juan" "Luis" "Romina" "Felipe" "Mariana"
\end{verbatim}
\item We can sort a vector using the \textbf{sort} command:
\begin{verbatim}
> names(sort(x=grades,decreasing=T))
[1] "Mariana" "Luis" "Juan" "Romina" "Felipe"
\end{verbatim}
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Vector Access}
\scriptsize{
\begin{itemize}
\item R allows access to the elements of a vector by means of numerical indexes \verb+[i]+:
\begin{verbatim}
> grades[1] # first element
Juan
4.5
\end{verbatim}
\item The index can be another numeric vector to access more than one element:
\begin{verbatim}
> grades[c(1,5)] # first and fifth element
Juan Mariana
4.5 6.7
\end{verbatim}
\item If we want to omit any element we use negative indexes:
\begin{verbatim}
> grades[-2] # All but the second one
Juan Romina Felipe Mariana
4.5 3.9 2.8 6.7
\end{verbatim}
\item The elements can also be accessed by their names:
\begin{verbatim}
> grades[c("Juan","Mariana")] # Only Juan and Mariana
Juan Mariana
4.5 6.7
\end{verbatim}
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Operating Vectors}
\scriptsize{
\begin{itemize}
\item We saw earlier that if we operate a scalar by a vector, the scalar applies to all the elements of the vector.
\item If we now have two vectors of the same length and operate on them, the operation is done element by element (element-wise):
\begin{verbatim}
a<-c(1,2)
b<-c(3,4)
> a+b
[1] 4 6
> a*b
[1] 3 8
\end{verbatim}
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Operating Vectors (2)}
\scriptsize{
\begin{itemize}
\item If the vectors are of different lengths, the smaller one recycles its elements:
\begin{verbatim}
> d<-c(4,5,6,9)
> a+d
[1] 5 7 7 11
> c(a,a)+d
[1] 5 7 7 11
\end{verbatim}
\item If the length of the longest is not a multiple of the length of the shortest, we get a warning:
\begin{verbatim}
> c(1,2)+c(-9,2,3)
[1] -8 4 4
Warning message:
In c(1, 2) + c(-9, 2, 3) :
longer object length is not a multiple of shorter object length
\end{verbatim}
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Comparing Vectors}
\scriptsize{
\begin{itemize}
\item R supports the comparison operators for numeric variables:\verb+ >,<, ==, <=, >=, !=+ in addition to \verb+& |+ as well as the operators \textbf{and} and \textbf{or} for logical variables:
\begin{verbatim}
> younger<-ages<18
> younger
[1] FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE TRUE TRUE FALSE TRUE FALSE FALSE
[17] TRUE TRUE FALSE FALSE FALSE
\end{verbatim}
\item If we give a vector an index of logical variables we retrieve the values where the index takes the true value:
\begin{verbatim}
> ages[younger]
[1] 12 7 14 2 11 13 11
\end{verbatim}
\item Exercise: calculate the average age of the elements older or equal to 18 years old.
\begin{verbatim}
mean(ages[ages>=18])
\end{verbatim}
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Null Values}
\scriptsize{
\begin{itemize}
\item In R, missing values are written as \verb+NA+. It is common that they appear when we read data from a database. Some functions do not accept null values so they must be taken into account.
\begin{verbatim}
> missing_vector<-c(12,15,NA)
> missing_vector
[1] 12 15 NA
\end{verbatim}
\item To check if a variable is null we use the command \verb+is.na+:
\begin{verbatim}
> missing_vector[!is.na(missing_vector)]
[1] 12 15
\end{verbatim}
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Sequences}
\scriptsize{
\begin{itemize}
\item To create a vector consisting of a sequence of numbers we use the command \textbf{seq}:
\begin{verbatim}
> my_pairs<-seq(from=2,to=20,by=2)
> mult_four<-seq(from=4,by=4,length=100)
> my_pairs
[1] 2 4 6 8 10 12 14 16 18 20
\end{verbatim}
\item It can also be created using the operator (\verb+:+):
\begin{verbatim}
> 1:10
[1] 1 2 3 4 5 6 7 8 9 10
> seq(1,10,1)
[1] 1 2 3 4 5 6 7 8 9 10
\end{verbatim}
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Repetitions}
\scriptsize{
\begin{itemize}
\item To create vectors that repeat a value or another vector multiple times we use the command \textbf{rep}. The first value is the object to repeat and the second is the number of repetitions:
\begin{verbatim}
> rep(10,3)
[1] 10 10 10
> rep(c("hello", "bye"),4)
[1] "hello" "bye" "hello" "bye" "hello" "bye"
"hello" "bye" "hello" "bye"
\end{verbatim}
\item Problem: Create a sequence that repeats 3 times the first 4 multiples of 7.
\begin{verbatim}
> rep(seq(from=7,by=7,length=4),3)
[1] 7 14 21 28 7 14 21 28 7 14 21 28
\end{verbatim}
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Random vector generation}
\scriptsize{
\begin{itemize}
\item To perform experiments or simulate phenomena of known behavior it is very useful to generate random vectors.
\item If we want uniformly distributed numbers between a maximum and a minimum we use \textbf{runif}:
\begin{verbatim}
> runif(n=5, min = 1, max = 10)
[1] 5.058862 1.737830 9.450956 9.149376 2.652774
\end{verbatim}
\item If we want numbers centered on a mean $\mu$ with a standard deviation $\sigma$, we use a normal distribution with the command \textbf{rnorm} where we know that $68\%$ of the observations will be within the range $\mu\pm\sigma$, $95\%$ in $\mu\pm2\sigma$ and $99.7\%$ in $\mu\pm3\sigma$:
\begin{verbatim}
> rnorm(n=5, mean = 10, sd = 4)
[1] 12.081286 2.636001 16.001953 0.120463 6.211835
\end{verbatim}
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Random Vector Generation (2)}
\scriptsize{
\begin{itemize}
\item When we want to model the number of arrivals per unit of time to simulate queuing models, we use the \textbf{Poisson} distribution with \textbf{rpos}. The parameter $\lambda$ tells us the average number of arrivals per time period
\begin{verbatim}
> rpois(n=10, lambda = 3)
[1] 1 3 8 6 1 1 6 3 4 7
\end{verbatim}
\item The binomial distribution can be used to represent experiments in which $k$ independent trials of a coin tossing Bernoulli experiment are performed. The probability of success (e.g., getting a head) in each trial is $p$. We can simulate the number of hits obtained for $n$ different Binomial experiments with the command \textbf{rbinom} ($k$ is named as ``size'').
\begin{verbatim}
> rbinom(n=10,size=2,prob=0.5)
[1] 0 1 2 1 1 0 2 0 0 1
> rbinom(n=10,size=2,prob=0.7)
[1] 1 2 2 1 0 1 2 2 2 2
> rbinom(n=10,size=2,prob=0.2)
[1] 0 0 0 0 1 0 1 0 1 0
\end{verbatim}
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Categorical Variables or Factors}
\scriptsize{
\begin{itemize}
\item In addition to numerical or logical variables, it is possible to work with categorical variables. E.g.: color, gender, social class.
\item They are created with the command \textbf{factor} and the possible values of the variable are stored in the attribute \textbf{levels}.
\begin{verbatim}
> people<-factor(c("Hombre","Mujer","Mujer","Mujer","Hombre"))
> people
[1] Hombre Mujer Mujer Mujer Hombre
Levels: Hombre Mujer
> class(people)
[1] "factor"
> levels(people)
[1] "Hombre" "Mujer"
#We can rename the levels
> levels(people)<-c("Man","Woman")
> people
[1] Man Woman Woman Woman Man
Levels: Man Woman
\end{verbatim}
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Aggregating variables by category with \textbf{tapply}}
\scriptsize{
\begin{itemize}
\item If we have a numeric vector and a categorical vector of the same length we can apply the aggregation function \textbf{tapply}.
\item Example: let's create a category for the vector ``ages'' with levels \emph{child}, \emph{adolescent}, \emph{adult}:
\begin{verbatim}
categ_ages<-ifelse(ages<12,"child",
ifelse(ages<18,"adolescent","adult"))
class(categ_ages)
[1] "character"
#We convert variables to factor with as.factor
categ_ages<-as.factor(categ_ages)
\end{verbatim}
\item Now, we can count the number of people per category, and calculate the mean and standard deviation for each group:
\begin{verbatim}
> tapply(ages,categ_ages,length)
adolescent adult child
3 14 4
> tapply(ages,categ_ages,mean)
adolescent adult child
13.00000 47.42857 7.75000
> tapply(ages,categ_ages,sd)
adolescent adult child
1.000000 25.294312 4.272002
\end{verbatim}
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Strings}
\scriptsize{
\begin{itemize}
\item We can print a string using the command \textbf{cat}:
\begin{verbatim}
> greeting<-"Hello World"
> cat(greeting)
Hello World
\end{verbatim}
\item To concatenate two strings we can use the command \textbf{paste}:
\begin{verbatim}
> paste("Hello","Bye",sep="-")
[1] "Hola-Bye"
> paste("person",1:4, sep="")
[1] "person1" "person2" "person3" "person4"
> paste(greeting,1:3, sep=" ")
[1] "Hello World 1" "Hello World 2" "Hello World 3"
\end{verbatim}
\item To extract substrings we use the command \textbf{substr}:
\begin{verbatim}
> substr(greeting,1,4)
[1] "Hell"
\end{verbatim}
\item There is a vector called \textbf{letters} that has all the letters of the alphabet. It is useful for naming variables:
\begin{verbatim}
> letters[1:4]
[1] "a" "b" "c" "d"
\end{verbatim}
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Matrices}
\scriptsize{
\begin{itemize}
\item Matrices are two-dimensional vectors. By default they are filled by column:
\begin{verbatim}
> matrix_by_col<-matrix(data=1:12,nrow=3,ncol=4)
> matrix_by_col
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
\end{verbatim}
\item To fill them per row, we can use the parameter \textbf{byrow}:
\begin{verbatim}
> matrix_per_row<-matrix(data=1:12,nrow=4,ncol=3,byrow=T)
> matrix_per_row
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
[4,] 10 11 12
\end{verbatim}
\item We access the dimension of the matrix with the command \textbf{dim}.
\begin{verbatim}
> dim(matrix_per_row)
[1] 4 3
\end{verbatim}
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Matrices (2)}
\scriptsize{
\begin{itemize}
\item To access the elements of a matrix we have to specify the rows $i$ and columns $j$ \verb+[i,j]+. If we leave any of the two values empty, all rows or columns are retrieved:
\begin{verbatim}
> matrix_per_row[2,] #Second row, all columns
[1] 4 5 6
> matrix_per_row[2,1] # Second row, first column
[1] 4
> matrix_per_row[-1,-2] # All but row 1 and column 2
[,1] [,2]
[1,] 4 6
[2,] 7 9
[3,] 10 12
\end{verbatim}
\item To access row or column names, we use \textbf{rownames} and \textbf{colnames} analogously to how we use \textbf{names} for vectors.
\begin{verbatim}
> rownames(matrix_per_row)<-paste("r",1:4,sep="")
> colnames(matrix_per_row)<-paste("c",1:3,sep="")
> matrix_per_row["r2","c3"]
[1] 6
\end{verbatim}
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Matrices (3)}
\scriptsize{
\begin{itemize}
\item We can add new rows or new columns to a matrix using \textbf{rbind} and \textbf{cbind} respectively:
\begin{verbatim}
> rbind(matrix_per_row,r5=1:3)
c1 c2 c3
r1 1 2 3
r2 4 5 6
r3 7 8 9
r4 10 11 12
r5 1 2 3
> cbind(matrix_per_row,c4=4:1)
c1 c2 c3 c4
r1 1 2 3 4
r2 4 5 6 3
r3 7 8 9 2
r4 10 11 12 1
\end{verbatim}
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Matrices (4)}
\scriptsize{
\begin{itemize}
\item Matrix multiplication is done with command \verb+%*%+:
\begin{verbatim}
>a<-matrix_by_col %*% matrix_per_row
c1 c2 c3
[1,] 166 188 210
[2,] 188 214 240
[3,] 210 240 270
\end{verbatim}
\item If we use only the operator \verb+*+, the multiplication is done element by element (only for matrices of equal dimension). This also applies to addition, subtraction, division and other operators.
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Matrices (5)}
\scriptsize{
\begin{itemize}
\item We can transpose a matrix with the command \textbf{t}:
\begin{verbatim}
> t(a)
[,1] [,2] [,3]
c1 166 188 210
c2 188 214 240
c3 210 240 270
\end{verbatim}
\item Eigenvalues and eigenvectors are calculated with \textbf{eigen}:
\begin{verbatim}
> eigen(a)
$values
[1] 6.483342e+02 1.665808e+00 3.437970e-14
$vectors
[,1] [,2] [,3]
[1,] -0.5045331 -0.76077568 0.4082483
[2,] -0.5745157 -0.05714052 -0.8164966
[3,] -0.6444983 0.64649464 0.4082483
\end{verbatim}
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Arrays or Tensors}
\scriptsize{
\begin{itemize}
\item Arrays (or tensors) are like matrices but with more dimensions:
\begin{verbatim}
> my_array<-array(1:8, dim=c(2,2,2))
> my_array
, , 1
[,1] [,2]
[1,] 1 3
[2,] 2 4
, , 2
[,1] [,2]
[1,] 5 7
[2,] 6 8
> my_array[1,2,1]
[1] 3
\end{verbatim}
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Lists}
\scriptsize{
\begin{itemize}
\item Matrices and arrays are restricted to have all vectors of the same length and of the same type.
\item Lists allow us to group objects of any type and any length:
\begin{verbatim}
my_list<-list(man="Pepe",woman="Juana",
children=3,ages=c(4,8,12))
\end{verbatim}
\item When we access its elements using \verb+[i]+ we get a sublist:
\begin{verbatim}
> my_list[c(3,4)] # sublist
$children
[1] 3
$ages
[1] 4 8 12
\end{verbatim}
\item We have three options to access a particular item in a list:
\begin{verbatim}
my_list[[1]]
my_list[["man"]]
my_list$man
[1] "Pepe"
\end{verbatim}
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Exercise List}
\scriptsize{
\begin{itemize}
\item Create a list with three vectors of length 100 generated by one of the mechanisms seen for generating random vectors. You can vary the distributions or parameters. Assign names to each of the vectors.
\begin{verbatim}
vectors<-list(normal=rnorm(n=100,mean=10,sd=5),
poisson=rpois(n=100,lambda=10),
uniform=runif(n=100,min=5,max=15))
\end{verbatim}
\item Calculate the mean and standard deviation of each of the vectors in the list.
\begin{verbatim}
means<-vector()
desv<-vector()
for(i in 1:length(vectors)){
means[i]<-mean(vectors[[i]])
desv[i]<-sd(vectors[[i]])
}
> means
[1] 10.589222 10.390000 9.579866
> desv
[1] 5.155478 2.711349 2.905810
\end{verbatim}
\end{itemize}
}
\end{frame}
\begin{frame}[fragile]{Aggregated calculations to Lists with \textbf{sapply} and \textbf{lapply}}
\scriptsize{
\begin{itemize}
\item The previous exercise can be solved in a much simpler way in R with some special functions to perform aggregation over lists.
\item The command \textbf{sapply} allows to apply a function to each element of a list and returns the results in a vector. Then \textbf{lapply} does the same but returns a list:
\begin{verbatim}
> sapply(vectors,mean)
normal poisson uniform
10.589222 10.390000 9.579866
> sapply(vectors,sd)
normal poisson uniform
5.155478 2.711349 2.905810
\end{verbatim}
\item Exercise: program your own version of \textbf{sapply}. Hint: In R, a function can receive another function as parameter.
\begin{verbatim}
my_apply<-function(a_list,fun,...){
result<-vector(length=length(a_list))
for(i in 1:length(a_list)){
result[i]<-fun(a_list[[i]],...)