Introducción a R

Alex Zambrano alexzambrano@usantotomas.edu.co

Cálculo numérico y simulación

R como calculadora

A continuación encontraran algunas operaciones básicas en R

5+8
2*4
75-12
(3-5)*4
sqrt(16) # raíz cuadrada de 16
exp(1) # número euler
log(3) # logaritmo natural de 3
log(1000,10) # logaritmo de 1000 en base 10
pi # número pi
sin(pi/2)
cos(pi)
tan(pi)
floor(4.8) # redondeo por debajo
ceiling(4.3) # redondeo por arriba
round(pi,digits=2) # redondear por 2 decimales
trunc(12.48) # redondeo eliminando decimales

Actividad

Realice los siguientes cálculos aritméticos y redondee los resultados a 2 cifras decimales

Cálculos sencillos

x <- 3
x
## [1] 3
x^3
## [1] 27
x+5
## [1] 8
x=3+7
x
## [1] 10

Cálculos sencillos

x^4
## [1] 10000
x <- c(1, 4, 9, 2.25, 1/4)
x
## [1] 1.00 4.00 9.00 2.25 0.25
length(x)
## [1] 5
class(x)
## [1] "numeric"
sqrt(x)
## [1] 1.0 2.0 3.0 1.5 0.5

Operaciones sencillas con vectores

x + 1
## [1]  2.00  5.00 10.00  3.25  1.25
y <- 1:10
x + y
##  [1]  2.00  6.00 12.00  6.25  5.25  7.00 11.00 17.00 11.25 10.25
x * y
##  [1]  1.00  8.00 27.00  9.00  1.25  6.00 28.00 72.00 20.25  2.50
x^2
## [1]  1.0000 16.0000 81.0000  5.0625  0.0625

Operaciones sencillas con vectores

x^2 + y^3
##  [1]    2.0000   24.0000  108.0000   69.0625  125.0625  217.0000  359.0000
##  [8]  593.0000  734.0625 1000.0625
exp(x)
## [1]    2.718282   54.598150 8103.083928    9.487736    1.284025
log(x)
## [1]  0.0000000  1.3862944  2.1972246  0.8109302 -1.3862944

¿Y qué hago cuando necesito ayuda?

help(exp)
help(log)
help(seq)

Actividad

Considere los siguientes vectores \(A^t=\begin{bmatrix} 3 & 9 & 8 & 7 \end{bmatrix}\) y \(B^t=\begin{bmatrix} 1 & 5 & 0 & 4 \end{bmatrix}\) y \(c=15\). Realice las siguientes operaciones

Generar vectores con seq

x1 <- seq(1, 100, by=2)
x1
##  [1]  1  3  5  7  9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45
## [24] 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91
## [47] 93 95 97 99
seq(0, 100, 10)
##  [1]   0  10  20  30  40  50  60  70  80  90 100
x <- seq(1, 100, length=10)
x
##  [1]   1  12  23  34  45  56  67  78  89 100
length(x)
## [1] 10
x <- seq(1, 100, length=10)
y <- seq(2, 100, length=50)

Unir vectores con c

z <- c(x, y)
z
##  [1]   1  12  23  34  45  56  67  78  89 100   2   4   6   8  10  12  14
## [18]  16  18  20  22  24  26  28  30  32  34  36  38  40  42  44  46  48
## [35]  50  52  54  56  58  60  62  64  66  68  70  72  74  76  78  80  82
## [52]  84  86  88  90  92  94  96  98 100
z + c(1, 2)
##  [1]   2  14  24  36  46  58  68  80  90 102   3   6   7  10  11  14  15
## [18]  18  19  22  23  26  27  30  31  34  35  38  39  42  43  46  47  50
## [35]  51  54  55  58  59  62  63  66  67  70  71  74  75  78  79  82  83
## [52]  86  87  90  91  94  95  98  99 102
z <- c(z, z, z, z)
z
##   [1]   1  12  23  34  45  56  67  78  89 100   2   4   6   8  10  12  14
##  [18]  16  18  20  22  24  26  28  30  32  34  36  38  40  42  44  46  48
##  [35]  50  52  54  56  58  60  62  64  66  68  70  72  74  76  78  80  82
##  [52]  84  86  88  90  92  94  96  98 100   1  12  23  34  45  56  67  78
##  [69]  89 100   2   4   6   8  10  12  14  16  18  20  22  24  26  28  30
##  [86]  32  34  36  38  40  42  44  46  48  50  52  54  56  58  60  62  64
## [103]  66  68  70  72  74  76  78  80  82  84  86  88  90  92  94  96  98
## [120] 100   1  12  23  34  45  56  67  78  89 100   2   4   6   8  10  12
## [137]  14  16  18  20  22  24  26  28  30  32  34  36  38  40  42  44  46
## [154]  48  50  52  54  56  58  60  62  64  66  68  70  72  74  76  78  80
## [171]  82  84  86  88  90  92  94  96  98 100   1  12  23  34  45  56  67
## [188]  78  89 100   2   4   6   8  10  12  14  16  18  20  22  24  26  28
## [205]  30  32  34  36  38  40  42  44  46  48  50  52  54  56  58  60  62
## [222]  64  66  68  70  72  74  76  78  80  82  84  86  88  90  92  94  96
## [239]  98 100

Generar vectores con rep

rep(1:10, 4)
##  [1]  1  2  3  4  5  6  7  8  9 10  1  2  3  4  5  6  7  8  9 10  1  2  3
## [24]  4  5  6  7  8  9 10  1  2  3  4  5  6  7  8  9 10
length(z)
## [1] 240
rep(c(1, 2, 3), 10)
##  [1] 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
rep(c(1, 2, 3), each=10)
##  [1] 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3

Indexado numérico de vectores

x <- seq(1, 100, 2)
x
##  [1]  1  3  5  7  9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45
## [24] 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91
## [47] 93 95 97 99
x[c(1, 2, 3, 4, 5)]
## [1] 1 3 5 7 9
x[1:5]
## [1] 1 3 5 7 9
x[10:5]
## [1] 19 17 15 13 11  9

Indexado de vectores con condiciones lógicas

condicion <- (x>30)
condicion
##  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [12] FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [23]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [34]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [45]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
class(condicion)
## [1] "logical"
x==37
##  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [12] FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE
## [23] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [34] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [45] FALSE FALSE FALSE FALSE FALSE FALSE
x[x==37]
## [1] 37
x[x!=9]
##  [1]  1  3  5  7 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47
## [24] 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93
## [47] 95 97 99
x[x>20]
##  [1] 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65
## [24] 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99

Indexado de vectores con %in%

y <- seq(101, 200, 2)
y %in% c(101, 127, 141)
##  [1]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [12] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE
## [23] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [34] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [45] FALSE FALSE FALSE FALSE FALSE FALSE
y
##  [1] 101 103 105 107 109 111 113 115 117 119 121 123 125 127 129 131 133
## [18] 135 137 139 141 143 145 147 149 151 153 155 157 159 161 163 165 167
## [35] 169 171 173 175 177 179 181 183 185 187 189 191 193 195 197 199
y[y %in% c(101, 127, 141)]
## [1] 101 127 141

Indexado de vectores con condiciones múltiples

z <- c(x, y)
z
##   [1]   1   3   5   7   9  11  13  15  17  19  21  23  25  27  29  31  33
##  [18]  35  37  39  41  43  45  47  49  51  53  55  57  59  61  63  65  67
##  [35]  69  71  73  75  77  79  81  83  85  87  89  91  93  95  97  99 101
##  [52] 103 105 107 109 111 113 115 117 119 121 123 125 127 129 131 133 135
##  [69] 137 139 141 143 145 147 149 151 153 155 157 159 161 163 165 167 169
##  [86] 171 173 175 177 179 181 183 185 187 189 191 193 195 197 199
z>150
##   [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [12] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [23] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [34] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [45] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [56] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [67] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE
##  [78]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [89]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [100]  TRUE
z[z>150]
##  [1] 151 153 155 157 159 161 163 165 167 169 171 173 175 177 179 181 183
## [18] 185 187 189 191 193 195 197 199
z[z<30 | z>150]
##  [1]   1   3   5   7   9  11  13  15  17  19  21  23  25  27  29 151 153
## [18] 155 157 159 161 163 165 167 169 171 173 175 177 179 181 183 185 187
## [35] 189 191 193 195 197 199
z[z>=30 & z<=150]
##  [1]  31  33  35  37  39  41  43  45  47  49  51  53  55  57  59  61  63
## [18]  65  67  69  71  73  75  77  79  81  83  85  87  89  91  93  95  97
## [35]  99 101 103 105 107 109 111 113 115 117 119 121 123 125 127 129 131
## [52] 133 135 137 139 141 143 145 147 149
z[c(1, 10, 40, 80)]
## [1]   1  19  79 159

Indexado de vectores con condiciones múltiples

cond  <-  (x>10) & (x<50)
cond
##  [1] FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [12]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [23]  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [34] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [45] FALSE FALSE FALSE FALSE FALSE FALSE
x[cond]
##  [1] 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49

Con las condiciones se pueden hacer operaciones

sum(cond)
## [1] 20
!cond
##  [1]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
## [12] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [23] FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [34]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [45]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
sum(!cond)
## [1] 30
length(x[cond])
## [1] 20
length(x[!cond])
## [1] 30
as.numeric(cond)
##  [1] 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
## [36] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Funciones predefinidas

summary(x)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     1.0    25.5    50.0    50.0    74.5    99.0
mean(x)
## [1] 50
sd(x)
## [1] 29.15476
median(x)
## [1] 50
max(x)
## [1] 99
min(x)
## [1] 1
range(x)
## [1]  1 99
quantile(x)
##   0%  25%  50%  75% 100% 
##  1.0 25.5 50.0 74.5 99.0

Actividad

Considere la construcción de los siguientes vectores

Construcción de matrices

z <- 1:12
M  <-  matrix(z, nrow=3)
M
##      [,1] [,2] [,3] [,4]
## [1,]    1    4    7   10
## [2,]    2    5    8   11
## [3,]    3    6    9   12
z
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12
class(M)
## [1] "matrix"
dim(M)
## [1] 3 4
summary(M)
##        V1            V2            V3            V4      
##  Min.   :1.0   Min.   :4.0   Min.   :7.0   Min.   :10.0  
##  1st Qu.:1.5   1st Qu.:4.5   1st Qu.:7.5   1st Qu.:10.5  
##  Median :2.0   Median :5.0   Median :8.0   Median :11.0  
##  Mean   :2.0   Mean   :5.0   Mean   :8.0   Mean   :11.0  
##  3rd Qu.:2.5   3rd Qu.:5.5   3rd Qu.:8.5   3rd Qu.:11.5  
##  Max.   :3.0   Max.   :6.0   Max.   :9.0   Max.   :12.0

Matrices a partir de vectores: rbind y cbind

x <- 1:10
y <- 1:10
z <- 1:10
z <- y <- x <- 1:10

M <- cbind(x, y, z)
M
##        x  y  z
##  [1,]  1  1  1
##  [2,]  2  2  2
##  [3,]  3  3  3
##  [4,]  4  4  4
##  [5,]  5  5  5
##  [6,]  6  6  6
##  [7,]  7  7  7
##  [8,]  8  8  8
##  [9,]  9  9  9
## [10,] 10 10 10
M <- rbind(x, y, z)
M
##   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## x    1    2    3    4    5    6    7    8    9    10
## y    1    2    3    4    5    6    7    8    9    10
## z    1    2    3    4    5    6    7    8    9    10
rbind(M, M)
##   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## x    1    2    3    4    5    6    7    8    9    10
## y    1    2    3    4    5    6    7    8    9    10
## z    1    2    3    4    5    6    7    8    9    10
## x    1    2    3    4    5    6    7    8    9    10
## y    1    2    3    4    5    6    7    8    9    10
## z    1    2    3    4    5    6    7    8    9    10
cbind(M, M)
##   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
## x    1    2    3    4    5    6    7    8    9    10     1     2     3
## y    1    2    3    4    5    6    7    8    9    10     1     2     3
## z    1    2    3    4    5    6    7    8    9    10     1     2     3
##   [,14] [,15] [,16] [,17] [,18] [,19] [,20]
## x     4     5     6     7     8     9    10
## y     4     5     6     7     8     9    10
## z     4     5     6     7     8     9    10

Transponer una matriz

t(M)
##        x  y  z
##  [1,]  1  1  1
##  [2,]  2  2  2
##  [3,]  3  3  3
##  [4,]  4  4  4
##  [5,]  5  5  5
##  [6,]  6  6  6
##  [7,]  7  7  7
##  [8,]  8  8  8
##  [9,]  9  9  9
## [10,] 10 10 10
class(t)
## [1] "function"
dim(t(M))
## [1] 10  3

Operaciones con matrices

M * M
##   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## x    1    4    9   16   25   36   49   64   81   100
## y    1    4    9   16   25   36   49   64   81   100
## z    1    4    9   16   25   36   49   64   81   100
M ^ 2
##   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## x    1    4    9   16   25   36   49   64   81   100
## y    1    4    9   16   25   36   49   64   81   100
## z    1    4    9   16   25   36   49   64   81   100
M %*% t(M)
##     x   y   z
## x 385 385 385
## y 385 385 385
## z 385 385 385

Operaciones con matrices: funciones predefinidas

sum(M)
## [1] 165
rowSums(M)
##  x  y  z 
## 55 55 55
colSums(M)
##  [1]  3  6  9 12 15 18 21 24 27 30
rowMeans(M)
##   x   y   z 
## 5.5 5.5 5.5
colMeans(M)
##  [1]  1  2  3  4  5  6  7  8  9 10

La función apply

apply(M, 1, sum)
##  x  y  z 
## 55 55 55
apply(M, 2, sum)
##  [1]  3  6  9 12 15 18 21 24 27 30
apply(M, 1, mean)
##   x   y   z 
## 5.5 5.5 5.5
apply(M, 2, mean)
##  [1]  1  2  3  4  5  6  7  8  9 10
apply(M, 1, sd, na.rm=TRUE)
##       x       y       z 
## 3.02765 3.02765 3.02765
apply(M, 2, sd)
##  [1] 0 0 0 0 0 0 0 0 0 0

Indexado de matrices

M
##   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## x    1    2    3    4    5    6    7    8    9    10
## y    1    2    3    4    5    6    7    8    9    10
## z    1    2    3    4    5    6    7    8    9    10
M[1, ]
##  [1]  1  2  3  4  5  6  7  8  9 10
M[, 1]
## x y z 
## 1 1 1
sum(M[, 1])
## [1] 3
M[1:2, ]
##   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## x    1    2    3    4    5    6    7    8    9    10
## y    1    2    3    4    5    6    7    8    9    10
M[1:2, 2:3]
##   [,1] [,2]
## x    2    3
## y    2    3
M[1, c(1, 4)]
## [1] 1 4
M[-1,]
##   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## y    1    2    3    4    5    6    7    8    9    10
## z    1    2    3    4    5    6    7    8    9    10
M[-c(1, 2),]
##  [1]  1  2  3  4  5  6  7  8  9 10

Actividad

Considere la siguiente matriz \[ A=\begin{bmatrix} 1 & 3 & 5\\ 7 & 9 & 11 \end{bmatrix} \] Realice las siguientes actividades

Valores perdidos

¿Qué es NA?

class(NA)
## [1] "logical"
x <- rnorm(100)
idx <- sample(length(x), 10)
idx
##  [1]  15  58  25  65  68  53  26 100   1  93
x[idx]
##  [1]  1.14647984  0.36879681  0.01790061 -0.56176864  0.49362117
##  [6]  0.49931496  1.21866759  0.28951296 -0.29224724  0.46837009
x2 <- x
x2[idx] <- NA
x2
##   [1]          NA -1.01196637 -1.93529609  0.56918751 -0.68478134
##   [6]  0.54965059 -0.44029130  1.21187516 -0.15473938  1.36252118
##  [11]  0.48607502 -1.88985562 -2.28318893 -1.04936589          NA
##  [16] -0.92397514 -0.38084970 -1.00300659 -0.47461172 -1.26780948
##  [21]  0.04976101 -0.51638374  0.92700865  1.48934841          NA
##  [26]          NA  0.19166205 -0.88186069 -0.31786515 -0.11173517
##  [31]  1.11017795  0.66481828 -0.91236827  0.80070279 -0.85756533
##  [36]  0.90185797 -1.31074218 -0.07684127  0.14892329  0.26882167
##  [41]  0.95752763  0.02584229  0.26246898  0.29551474 -0.63120483
##  [46]  1.14552221  0.05395648 -1.49438473 -0.57613215  0.99855461
##  [51]  1.61283305  1.18768606          NA  1.10072709 -0.53569464
##  [56]  0.78627270  0.54171518          NA  0.51017399  1.23689257
##  [61] -1.47339140 -1.98487186 -0.63740616  0.63742224          NA
##  [66] -1.50922992  0.47364303          NA -0.60033515 -0.92973606
##  [71]  0.22322362  1.51949279  0.22876158 -1.50202731  0.58589824
##  [76]  0.36391157 -1.36678942  0.05883473 -1.38568970  0.72713959
##  [81] -0.38908880  0.50433500 -0.46537795 -0.99430031 -1.70938617
##  [86] -0.76725177 -1.12713458 -0.65443908  1.26663831  2.24165077
##  [91]  1.22550674 -0.21810631          NA  1.24072381  0.14226060
##  [96] -0.66065375 -0.89880084  1.00087224 -1.65474682          NA

NA en las funciones

summary(x)
##     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
## -2.28300 -0.78980  0.05186 -0.05114  0.64430  2.24200
mean(x)
## [1] -0.05114237
sum(x)
## [1] -5.114237
summary(x2)
##     Min.  1st Qu.   Median     Mean  3rd Qu.     Max.     NA's 
## -2.28300 -0.89460 -0.02550 -0.09737  0.65800  2.24200       10
mean(x2)
## [1] NA
sum(x2)
## [1] NA
mean(x2, na.rm=TRUE)
## [1] -0.09736539
sum(x2, na.rm=TRUE)
## [1] -8.762885
sd(x2, na.rm=TRUE)
## [1] 1.002925
class(TRUE)
## [1] "logical"

Actividad

Considere una matriz \(A\) de números aleatorios (runif) la cual tenga 10 filas y 5 columnas.

Realice las siguientes actividades

Actividad

Del libro http://www.math.csi.cuny.edu/Statistics/R/simpleR/printable/simpleR.pdf, realizar los ejercicios capitulo 2.

Definición de funciones

Para definir una función usamos la función function

Forma general

NombreDeFuncion <-function(arg 1, arg 2, ...) expresión

Ejemplo

myFun <- function(x, y=c(5,6,7)) x + y
myFun(4)
## [1]  9 10 11
class(myFun)
## [1] "function"

Definición de funciones

Ejemplo se desea escribir una función que realice lo siguiente \[ f(x,y)=x^2+y^2 \]

circulo <- function(x,y){
  x^2 + y^2
}
circulo(3,4)
## [1] 25

Definir una función a partir de funciones

foo  <-  function(x, ...){
  mx <- mean(x, ...)
  medx <- median(x, ...)
  sdx <- sd(x, ...)
  c(mx, medx, sdx)
  }

O en forma resumida:

foo <- function(x, ...){c(mean(x, ...), median(x, ...), sd(x, ...))}

Uso de funciones

foo(1:10)
## [1] 5.50000 5.50000 3.02765
rnorm(100)
##   [1]  0.4441046345 -0.3274719914 -0.5125091355 -0.3235932253  1.5172273979
##   [6] -0.2435050090 -1.5278071166  1.5951050339 -0.5536066620  0.8250528447
##  [11]  0.2006054577 -0.4105256594  0.5013749568 -0.0134349212  0.6925492220
##  [16] -2.2994968771  0.0743250215 -2.2068199084 -0.6711446018 -1.8595810292
##  [21]  0.7376082254  1.3256126739  0.9908097660 -0.0289418041  0.3539566105
##  [26] -0.2253952647  1.5500674053 -0.3917605322  0.0676260690  0.8916515211
##  [31]  1.0681590417 -0.0392379736 -0.7907134535 -0.4610996014  0.1187527709
##  [36]  2.1489187372 -0.6709536396 -0.3955274313 -0.4113039981  0.7270341107
##  [41] -0.1163036013 -1.5734500189  1.1845592127 -0.6460994615 -0.3267298057
##  [46]  0.9438161157  0.0407273814  0.0261637608 -0.2955890903  0.7151524223
##  [51] -0.6678626695  0.0520578451 -0.0004309447 -0.0964092676 -0.4769331802
##  [56]  1.1010804201 -0.3209755824  1.3966757183  0.7213468431  1.5452717224
##  [61] -0.4713255621 -0.5302686115 -0.4954506741  0.4188757396 -0.5284797808
##  [66]  0.9680375695  2.8964030695  0.9208650315 -1.1362592608 -0.4499486351
##  [71]  0.7905743754 -2.6257662362 -0.8759858634 -0.0912099048  0.2245257139
##  [76] -0.0478100541  0.3539354800  0.2076458071 -0.5555275888 -0.2946299786
##  [81] -0.1749947487  1.3752304092 -0.9420608846  0.3387829194  1.3579813301
##  [86]  2.8051926623 -0.6288637996  0.0240420628 -0.8845738592  0.0686462369
##  [91]  0.1002138900  1.0325299251  1.0069427694 -0.0270195990  0.2486336189
##  [96]  0.5515710831  0.5292114822  0.7968520986 -0.5508131496 -0.1671491874
foo(rnorm(1e5))
## [1] 0.004016187 0.001099934 0.997965639

Actividades

Realice las siguientes actividades

Operadores de control de flujo

if(cond) expr
if(cond) cons.expr  else  alt.expr

for(var in seq) expr
while(cond) expr
repeat expr
break
next

Ejemplo

De un vector de valores aleatorios normales, elevar al cuadrado cada uno de ellos.

for(n in c(2,5,10,20,50)) {
x <- rnorm(n)
cat(n,":", sum(x^2),"\n")
}
## 2 : 0.5667946 
## 5 : 3.720305 
## 10 : 9.884847 
## 20 : 23.43603 
## 50 : 47.06266

Ejemplo

Del listado de números del-5 a 5, elevar a la tres cada término.

for(i in -5:5)
{cat(i,"\t", i^3,"\n")
}
## -5    -125 
## -4    -64 
## -3    -27 
## -2    -8 
## -1    -1 
## 0     0 
## 1     1 
## 2     8 
## 3     27 
## 4     64 
## 5     125

Ejemplo

De un vector de valores aleatorios normales asignarles 1 aquellos valores mayores que 0.

set.seed(12082015)
x <- rnorm(10)
x2 <- as.numeric(x>0)
for (i in 1:length(x2)){
if (x[i]<0) x2[i] <- 0 else x2[i] <- 1
}
cbind(x, x2)
##                 x x2
##  [1,]  0.05583942  1
##  [2,] -1.03448585  0
##  [3,] -0.23856100  0
##  [4,]  0.73465696  1
##  [5,]  0.12637545  1
##  [6,] -0.70524819  0
##  [7,]  0.09321508  1
##  [8,]  0.70260777  1
##  [9,]  0.33520915  1
## [10,]  1.45958329  1

Otra forma

set.seed(12082015)
x <- rnorm(10)
ifelse(x>0, 1, 0)
##  [1] 1 0 0 1 1 0 1 1 1 1

Ejemplo

Empezando con \(i = 4\) elevar al cuadrado cada valor hasta que \(i\leq 10\)

i<-4
while(i<=10){
  print(i^2);i=i+1
}
## [1] 16
## [1] 25
## [1] 36
## [1] 49
## [1] 64
## [1] 81
## [1] 100

Otra forma

i<-4
repeat{ print(i^2)
  i=i+1
  if(i > 10) break
}
## [1] 16
## [1] 25
## [1] 36
## [1] 49
## [1] 64
## [1] 81
## [1] 100

Operadores lógicos

! x
x & y
x && y
x | y
x || y
xor(x, y)

Operadores de sintaxis

:: :::   #access variables in a namespace
$ @  #component / slot extraction
[ [[     #indexing
^    #exponentiation (right to left)
- +  #unary minus and plus
:    #sequence operator
%any%    #special operators (including %% and %/%)
* /  #multiply, divide
+ -  #(binary) add, subtract
< > <= >= == !=  #ordering and comparison
!    #negation
& &&     #and
| ||     #or
~    #as in formulae
-> ->>   #rightwards assignment
<- <<-   #assignment (right to left)
=    #assignment (right to left)
?    #help (unary and binary)

Ejemplo

Creamos una función que calcule el logaritmo de un número

logaritmo<-function(x){
  if(is.numeric(x)&& min(x)!=0){
    log(x)
  }
  else{
    stop("x no es numérico o es cero")
  }
}

logaritmo(3)
## [1] 1.098612
logaritmo(10)
## [1] 2.302585
logaritmo(exp(1))
## [1] 1

Ejemplo

Creamos una función que calcule el inverso de un número

Inverso<-function(x) ifelse(x==0,NA,1/x)

Inverso(-2:3)
## [1] -0.5000000 -1.0000000         NA  1.0000000  0.5000000  0.3333333
Inverso(-10:10)
##  [1] -0.1000000 -0.1111111 -0.1250000 -0.1428571 -0.1666667 -0.2000000
##  [7] -0.2500000 -0.3333333 -0.5000000 -1.0000000         NA  1.0000000
## [13]  0.5000000  0.3333333  0.2500000  0.2000000  0.1666667  0.1428571
## [19]  0.1250000  0.1111111  0.1000000

Ejemplo

Factorial de un número

factorial<-function(n){
  f<-1
  if(n>1){
    for(i in 1:n){
      f<-f*i
    }
  }
  return(f)
}

factorial(3)
## [1] 6
factorial(25)
## [1] 1.551121e+25
factorial(0)
## [1] 1

Ejemplo

Progresión aritmética \[ a_n=a_1+(n-1)d \]

#Formula explícita
arit.1<-function(n=1,a1=1,d=1){
  a1+d*(n-1)
}

#Formula recursiva
arit.2<-function(n=1,a1=1,d=1){
  a<-numeric(n)
  a[1]<-a1
  if(n>1){
    for(i in 2:n){
      a[i]=a[i-1]+d
    }
  }
  return(a[n])
}

#Formula vectorial
arit.3<-function(n=1,a1=1,d=1){
  A1<-rep(a1,n)
  D<-rep(d,n)
  N<-0:(n-1)
  A<-A1+N*D
  return(A[n])
}

arit.1(n=5,a1=2,d=2)
## [1] 10
arit.2(n=5,a1=2,d=2)
## [1] 10
arit.3(n=5,a1=2,d=2)
## [1] 10

Actividad

Realice las siguientes actividades

Gráficos

summary(mtcars)
##       mpg             cyl             disp             hp       
##  Min.   :10.40   Min.   :4.000   Min.   : 71.1   Min.   : 52.0  
##  1st Qu.:15.43   1st Qu.:4.000   1st Qu.:120.8   1st Qu.: 96.5  
##  Median :19.20   Median :6.000   Median :196.3   Median :123.0  
##  Mean   :20.09   Mean   :6.188   Mean   :230.7   Mean   :146.7  
##  3rd Qu.:22.80   3rd Qu.:8.000   3rd Qu.:326.0   3rd Qu.:180.0  
##  Max.   :33.90   Max.   :8.000   Max.   :472.0   Max.   :335.0  
##       drat             wt             qsec             vs        
##  Min.   :2.760   Min.   :1.513   Min.   :14.50   Min.   :0.0000  
##  1st Qu.:3.080   1st Qu.:2.581   1st Qu.:16.89   1st Qu.:0.0000  
##  Median :3.695   Median :3.325   Median :17.71   Median :0.0000  
##  Mean   :3.597   Mean   :3.217   Mean   :17.85   Mean   :0.4375  
##  3rd Qu.:3.920   3rd Qu.:3.610   3rd Qu.:18.90   3rd Qu.:1.0000  
##  Max.   :4.930   Max.   :5.424   Max.   :22.90   Max.   :1.0000  
##        am              gear            carb      
##  Min.   :0.0000   Min.   :3.000   Min.   :1.000  
##  1st Qu.:0.0000   1st Qu.:3.000   1st Qu.:2.000  
##  Median :0.0000   Median :4.000   Median :2.000  
##  Mean   :0.4062   Mean   :3.688   Mean   :2.812  
##  3rd Qu.:1.0000   3rd Qu.:4.000   3rd Qu.:4.000  
##  Max.   :1.0000   Max.   :5.000   Max.   :8.000
attach(mtcars)
plot(wt, mpg) 
abline(lm(mpg~wt))
title("Regresión de MPG con Weight")

Histograma

hist(mtcars$mpg)

#Histograma con barras rojas
hist(mtcars$mpg, breaks=12, col="red")

Gráfico de densidad

d <- density(mtcars$mpg) 
plot(d)

Diagrama de puntos

dotchart(mtcars$mpg,labels=row.names(mtcars),cex=.7, main="Millas por Galón según modelo de carro",
         xlab="Millas por Galón")

Gráfico de Barras

conteos <- table(mtcars$gear)
barplot(conteos, main="Distribución de Carros", xlab="Número de Velocidades")

Gráfico de Barras

counts <- table(mtcars$am, mtcars$gear)
barplot(counts, main="Distribución de carros según Velocides y Tipo de Transmisión",
  xlab="Número de Velocidades", col=c("darkblue","red"),
     legend = rownames(counts))

Gráfico de Barras

counts <- table(mtcars$am, mtcars$gear)
barplot(counts, main="Distribución de carros según Velocides y Tipo de Transmisión",
  xlab="Número de Velocidades", col=c("darkblue","red"),
     legend = rownames(counts), beside=TRUE)

Gráfico de lineas

\begin{tabular}{rrr} \hline Dosis & Respuesta a la droga A & Respuesta a la droga B \\ \hline 20 & 16 & 15 \\ 30 & 20 & 18 \\ 40 & 27 & 25 \\ 45 & 40 & 31 \\ 60 & 60 & 40 \\ \hline \end{tabular}

Realice un gráfico de lineas de cada una de las respuesta a cada droga.

dosis <- c(20, 30, 40, 45, 60)
drogA <- c(16, 20, 27, 40, 60)
drogB <- c(15, 18, 25, 31, 40)
plot(dosis, drogA, type="b", ylab="Respuestas",col=1)
lines(dosis,drogB, type="b",lty=2, pch=17,col=2)
legend(x=min(dosis), y=max(drogA,drogB), legend=c("DrogA","DrogB"), cex=0.8, col=c(1,2), pch=c(1,17), lty=c(1,2), title="Drogas")

Diagrama Circular

\begin{tabular}{rr} \hline Ciudad & Frecuencia \\ \hline US & 10 \\ UK & 12 \\ Australia & 4 \\ Alemania & 16 \\ Francia & 8 \\ \hline \end{tabular}

% Realice un diagrama circular

frecuencia <- c(10, 12,4, 16, 8)
ciudades <- c("US", "UK", "Australia", "Germany", "France")
pie(frecuencia, labels = ciudades, main="Diagrama circular de las ciudades")

Diagrama Boxplot

boxplot(mpg~cyl,data=mtcars, main="Kilometraje de los carros", 
     xlab="Número de Cilindros", ylab="Millas por Galón")

Diagrama de Dispersión

plot(wt, mpg,main="Diagrama de Dispersión", xlab="Peso del Carro ", ylab="Millas por Galón", pch=19)

Diagrama de Dispesión

library(car) 
scatterplot(mpg ~ wt, data=mtcars, xlab="Peso del Carro", ylab="Millas por Galón",main="Gráfico de dispersión mejorado",labels=row.names(mtcars))

Matriz de Dispersión

library(car)
scatterplot.matrix(~mpg+disp+drat+wt, data=mtcars)
## Warning: 'scatterplot.matrix' is deprecated.
## Use 'scatterplotMatrix' instead.
## See help("Deprecated") and help("car-deprecated").

Gráfico de Densidad

library(lattice)

densityplot(~mpg, main="Gráfico de Densidad", xlab="Millas por Galon")

Gráfico de Densidad

attach(mtcars)
## The following objects are masked from mtcars (pos = 5):
## 
##     am, carb, cyl, disp, drat, gear, hp, mpg, qsec, vs, wt
cyl <- factor(cyl, levels=c(4, 6, 8), labels=c("4 cilindros", "6 cilindros", "8 cilindros"))
densityplot(~mpg | cyl,main="Gráfico de Densidad por Número de Cilindros",xlab="Millas por Galon")

Para mayor información

Para mayor información pueden investigar los siguientes links: Lattice: Multivariate Data Visualization with R http://lmdvr.r-forge.r-project.org/figures/figures.html

Trellis Graphics User’s Manual http://cm.bell-labs.com/cm/ms/departments/sia/doc/trellis.user.pdf

ggplot2

Histograma

library(ggplot2)
## 
## Attaching package: 'ggplot2'
## 
## The following object is masked from 'mtcars':
## 
##     mpg
## 
## The following object is masked from 'mtcars':
## 
##     mpg
m <- ggplot(mtcars, aes(x = mpg))
m + geom_histogram(binwidth =5)

Gráfico de Densidad

m <- ggplot(mtcars, aes(x = mpg))
m + geom_density()

Gráfico de Densidad

m <- ggplot(mtcars, aes(x = mpg,colour=cyl,group=cyl))
m + geom_density()

Diagrama de puntos

m<-ggplot(mtcars, aes(x = factor(vs), fill = factor(cyl), y = mpg)) 
m + geom_dotplot(binaxis = "y", stackdir = "center", position = "dodge")
## stat_bindot: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.

Gráfico de Barras

r <- ggplot(mtcars, aes(factor(cyl)))
r + geom_bar()

Gráfico de Barras

qplot(factor(cyl), data=mtcars, geom="bar", fill=factor(gear))

Diagrama Boxplot

m <- ggplot(mtcars, aes(factor(cyl),mpg))
m + geom_boxplot()

Diagrama de Dispesión

qplot(wt,mpg, data=mtcars, facets=am~cyl, size=hp)

Documentación

Para mayor información pueden investigar en ggplot2 http://had.co.nz/ggplot2/