Docsity
Docsity

Prepara tus exámenes
Prepara tus exámenes

Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity


Consigue puntos base para descargar
Consigue puntos base para descargar

Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium


Orientación Universidad
Orientación Universidad


Aplicación de funciones en R: Función suma y listas, Guías, Proyectos, Investigaciones de Probabilidad

La aplicación de una función suma en R, así como el uso de listas. Se muestra cómo definir una función suma con valores predeterminados, aplicarla a vectores y matrices, y crear listas con diferentes tipos de datos. Además, se explica cómo acceder a los elementos de las listas por nombre y por índice.

Tipo: Guías, Proyectos, Investigaciones

2020/2021

Subido el 11/04/2021

alejomejia1128
alejomejia1128 🇨🇴

3 documentos

1 / 35

Toggle sidebar

Esta página no es visible en la vista previa

¡No te pierdas las partes importantes!

bg1
POLITÉCNICO GRANCOLOMBIANO
POLITÉCNICO GRANCOLOMBIANO 1
POLITÉCNICO GRANCOLOMBIANO 1
Complemento nociones básicas de R
Variables
R no requiere ningún tipo de comando para declarar variables. Sencillamente crea la variable
mediante asignación de su valor
x <- 3
x
## [1] 3
Una vez declarada la variable podemos utilizar en cálculos
x^3
## [1] 27
x+5
## [1] 8
Si deseamos cambiar el valor de la variable, solo debemos asignarle un nuevo valor
x <- 3+7
x
## [1] 10
x^4
## [1] 10000
Vectores
Para representar un nuevo vector de elemento debemos concatenar el vector de la siguiente manera
x <- c(1, 4, 9, 2.25, 1/4)
x
## [1] 1.00 4.00 9.00 2.25 0.25
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23

Vista previa parcial del texto

¡Descarga Aplicación de funciones en R: Función suma y listas y más Guías, Proyectos, Investigaciones en PDF de Probabilidad solo en Docsity!

Complemento nociones básicas de R

Variables

R no requiere ningún tipo de comando para declarar variables. Sencillamente crea la variable

mediante asignación de su valor

x <- 3 x

[1] 3

Una vez declarada la variable podemos utilizar en cálculos

x ^ 3

[1] 27

x + 5

## [1] 8

Si deseamos cambiar el valor de la variable, solo debemos asignarle un nuevo valor

x <- 3 + 7

x

[1] 10

x ^ 4

## [1] 10000

Vectores

Para representar un nuevo vector de elemento debemos concatenar el vector de la siguiente manera

x <- c ( 1 , 4 , 9 , 2.25, 1 / 4 )

x

[1] 1.00 4.00 9.00 2.25 0.

length (x)

[1] 5

class (x)

[1] “numeric”

sqrt (x)

[1] 1.0 2.0 3.0 1.5 0.

Primeras funciones class (c)

[1] “function”

class (length)

[1] “function”

length

function (x) .Primitive(“length”)

Operaciones sencillas con vectores

x + 1

## [1] 2.00 5.00 10.00 3.25 1.

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.

x ***** y

[1] 1.00 8.00 27.00 9.00 1.25 6.00 28.00 72.00 20.25 2.

x ^ 2

## [1] 1.0000 16.0000 81.0000 5.0625 0.

## [47] 93 95 97 99

seq ( 1 , 100 , 10 )

## [1] 1 11 21 31 41 51 61 71 81 91

seq ( 1 , 100 , length= 10 )

## [1] 1 12 23 34 45 56 67 78 89 100

x <- seq ( 1 , 100 , length= 10 )

x

[1] 1 12 23 34 45 56 67 78 89 100

length (x)

[1] 10

y <- seq ( 2 , 100 , length= 50 )

y

[1] 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32

34

[18] 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66

68

[35] 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100

length (y)

[1] 50

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

## [18] 18 19 22 23 26 27 30 31 34 35 38 39 42 43 46 47

## [35] 51 54 55 58 59 62 63 66 67 70 71 74 75 78 79 82

## [52] 86 87 90 91 94 95 98 99 102

z <- c (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

length (z)

[1] 180

Generar vectores con rep

Para ver la ayuda digite help (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

## [24] 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10

## [1] 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73

## [24] 77 79 81 83 85 87 89 91 93 95 97 99

x[x < 20 ]

## [1] 1 3 5 7 9 11 13 15 17 19

x[x == 9 ]

## [1] 9

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

## [24] 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91

## [47] 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

## [18] 135 137 139 141 143 145 147 149 151 153 155 157 159 161 163 165

## [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

## [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

## [18] 155 157 159 161 163 165 167 169 171 173 175 177 179 181 183 185

## [35] 189 191 193 195 197 199

z[z >= 30 & z <= 150 ]

## [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.

mean (x)

[1] 50

sd (x)

[1] 29.

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.

Matrices Construir una matriz

Para la ayuda de la función escriba help (matrix)

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 V

Min. :1.0 Min. :4.0 Min. :7.0 Min. :10.

1st Qu.:1.5 1st Qu.:4.5 1st Qu.:7.5 1st Qu.:10.

Median :2.0 Median :5.0 Median :8.0 Median :11.

Mean :2.0 Mean :5.0 Mean :8.0 Mean :11.

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 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

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

Multiplicación entre matrices

Para multiplicar entre matrices utilice lo siguiente %*%:

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 %%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.

colMeans (M)

[1] 1 2 3 4 5 6 7 8 9 10

M[, 1 ]

x y z

1 1 1

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

Valores Perdidos

Un valor perdido se denota como NA (‘Not Available’ / Missing Values)

class (NA)

[1] “logical”

x <- rnorm ( 100 )

idx <- sample ( length (x), 10 )

idx

x[idx]

x2 <- x NA en las funciones

Cuando en los objetos hay valores NA las funciones no trabajan de forma adecuada.

summary (x)

  • [1]

  • [1] 0.48140246 -1.25438824 0.43637005 0.10170100 -0.

  • [6] 0.51871286 -0.04006789 -0.34833641 -1.02026599 -0.

  • x x2[idx] <- NA
  • [1] 2.460491400 -0.989672187 -0.733562158 0.446869438 -0.

  • [6] -2.089056325 NA 2.003798474 2.879552568 0.

  • [11] -0.146436980 0.293767454 2.376869437 -0.709277219 -0.

  • [16] -0.552640828 1.114521816 0.275690979 -0.476080386 -0.

  • [21] -0.055346313 -1.220338566 NA 0.203108405 -0.

  • [26] 0.414756468 -0.674746617 0.968628374 0.391101042 0.

  • [31] 0.148390378 -0.724031294 1.584410433 NA -1.

  • [36] 0.312395375 -0.550137672 0.078965341 0.226975622 -0.

  • [41] NA NA -0.468640864 0.132528834 0.

  • [46] -0.109778264 0.462969932 0.198948808 -0.914050237 -0.

  • [51] -0.925615008 0.445396509 -0.067783435 0.479883253 -0.

  • [56] NA -1.727086193 1.339306531 1.148439872 -0.

  • [61] -1.089754411 0.430632420 0.098041925 -0.607280524 1.

  • [66] NA NA 0.432061797 0.040583975 0.

  • [71] -1.087568794 0.236805285 -1.297783394 -0.127382414 0.

  • [76] -1.845351783 0.211207141 0.061888953 NA -1.

  • [81] 1.707006915 -0.610353010 0.544673138 1.267711209 -0.

  • [86] 2.123144597 0.835974335 -1.798179950 0.512522698 -0.

  • [91] -2.534751466 0.305806101 1.890898979 -0.123456187 -0.

  • [96] 1.424617821 -0.224555669 -0.971919271 NA -0.

Forma general

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

Ejemplo myFun <- function (x, y) x + y

myFun ( 3 , 4 )

## [1] 7

class (myFun)

[1] “function”

También se puede definir una función a partir de otras funciones

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

foo ( 1 : 10 ) # Función que calcula la media, mediana y la desviación estándar

## [1] 5.50000 5.50000 3.

Lo anterior también funciona con matrices

M <- matrix ( c ( 1 : 30 ),nrow = 3 ,byrow = TRUE)

M

## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]

## [1,] 1 2 3 4 5 6 7 8 9 10

## [2,] 11 12 13 14 15 16 17 18 19 20

## [3,] 21 22 23 24 25 26 27 28 29 30

apply (M, 1 , foo) # Aplicando la función foo en forma fila

## [,1] [,2] [,3]

## [1,] 5.50000 15.50000 25.

## [2,] 5.50000 15.50000 25.

## [3,] 3.02765 3.02765 3.

apply (M, 2 , foo) # Aplicando la función foo en forma columna

## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]

## [1,] 11 12 13 14 15 16 17 18 19 20

## [2,] 11 12 13 14 15 16 17 18 19 20

## [3,] 10 10 10 10 10 10 10 10 10 10

Funciones con valores predeterminados

function.suma<- function (A= 10 ,B= 5 ) A + B

function.suma ()

[1] 15

function.suma ( 10 , 3 )

## [1] 13

x

[1] 2.460491400 -0.989672187 -0.733562158 0.446869438 -0.

[6] -2.089056325 -1.254388240 2.003798474 2.879552568 0.

[11] -0.146436980 0.293767454 2.376869437 -0.709277219 -0.

[16] -0.552640828 1.114521816 0.275690979 -0.476080386 -0.

[21] -0.055346313 -1.220338566 0.481402461 0.203108405 -0.

[26] 0.414756468 -0.674746617 0.968628374 0.391101042 0.

[31] 0.148390378 -0.724031294 1.584410433 0.436370045 -1.

[36] 0.312395375 -0.550137672 0.078965341 0.226975622 -0.

[41] 0.101701005 -0.348336413 -0.468640864 0.132528834 0.

[46] -0.109778264 0.462969932 0.198948808 -0.914050237 -0.

[51] -0.925615008 0.445396509 -0.067783435 0.479883253 -0.

[56] 0.518712855 -1.727086193 1.339306531 1.148439872 -0.

[61] -1.089754411 0.430632420 0.098041925 -0.607280524 1.

[66] -0.767561732 -0.348508327 0.432061797 0.040583975 0.

[71] -1.087568794 0.236805285 -1.297783394 -0.127382414 0.

[76] -1.845351783 0.211207141 0.061888953 -0.040067888 -1.