En esta primera clase se pretende mostrar
algunas características básicas de R.
options(digits = 5)
R como una calculadora
5 + 4
## [1] 9
6 - 4 * 8
## [1] -26
7/6 + 1
## [1] 2.1667
1/2 * 3
## [1] 1.5
4%/%3
## [1] 1
Algunas funciones y constantes
raiz cuadrada
sqrt(3)
## [1] 1.7321
pi
pi
## [1] 3.1416
ex
exp(1)
## [1] 2.7183
logaritmo natural
log(10)
## [1] 2.3026
factorial
factorial(4)
## [1] 24
sucesiones sencillas
1:10
## [1] 1 2 3 4 5 6 7 8 9 10
99:1
## [1] 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77
## [24] 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54
## [47] 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31
## [70] 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8
## [93] 7 6 5 4 3 2 1
sucesiones con incremento dado
seq(1, 100, 5)
## [1] 1 6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 96
obtener una muestra de tamaño 6 de los datos de 1 a 45
sample(1:45, 6)
## [1] 8 43 24 19 34 42
una combinatoria
choose(40, 5)
## [1] 658008
suma
sum(2:8)
## [1] 35
producto
prod(5:1)
## [1] 120
trigonométricas
sin(pi)
## [1] 1.2246e-16
cos(2 * pi)
## [1] 1
tan(pi/4)
## [1] 1
creación y manejo de vectores
x <- c(1, 2, 3, 4, 5, 3, 2, 3, 4, 3, 2, 1, 2, 3, 4, 5)
y <- c(4, 5, 7, 3, 5, 1, 2, 3, 2, 4, 5, 6, 5, 4, 3, 2)
length(x)
## [1] 16
z = 4
z * x
## [1] 4 8 12 16 20 12 8 12 16 12 8 4 8 12 16 20
x
## [1] 1 2 3 4 5 3 2 3 4 3 2 1 2 3 4 5
y
## [1] 4 5 7 3 5 1 2 3 2 4 5 6 5 4 3 2
(w <- c(1, 2, 3))
## [1] 1 2 3
funciones que se aplican a vectores
min(x)
## [1] 1
max(y)
## [1] 7
median(x)
## [1] 3
range(y)
## [1] 1 7
sort(y)
## [1] 1 2 2 2 3 3 3 4 4 4 5 5 5 5 6 7
mean(x)
## [1] 2.9375
mean(y)
## [1] 3.8125
sum(x)
## [1] 47
sd(y)
## [1] 1.6419
var(x)
## [1] 1.5292
summary(x)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.00 2.00 3.00 2.94 4.00 5.00
un diagrama de taloo y hoja
stem(x)
##
## The decimal point is at the |
##
## 1 | 00
## 2 | 0000
## 3 | 00000
## 4 | 000
## 5 | 00
cor(x, y)
## [1] -0.36734
2 * x + 3 * y
## [1] 14 19 27 17 25 9 10 15 14 18 19 20 19 18 17 16
x/y^2
## [1] 0.062500 0.080000 0.061224 0.444444 0.200000 3.000000 0.500000
## [8] 0.333333 1.000000 0.187500 0.080000 0.027778 0.080000 0.187500
## [15] 0.444444 1.250000
options(digits = 4)
x/y^2
## [1] 0.06250 0.08000 0.06122 0.44444 0.20000 3.00000 0.50000 0.33333
## [9] 1.00000 0.18750 0.08000 0.02778 0.08000 0.18750 0.44444 1.25000
rep(x, 3)
## [1] 1 2 3 4 5 3 2 3 4 3 2 1 2 3 4 5 1 2 3 4 5 3 2 3 4 3 2 1 2 3 4 5 1 2 3
## [36] 4 5 3 2 3 4 3 2 1 2 3 4 5
gráficas no elaboradas, sólo ejemplos sencillos
par(mfrow = c(2, 3))
boxplot(x)
plot(x, y)
boxplot(y)
boxplot(x, y)
hist(x)
distribuciones de frecuencia
table(y)
## y
## 1 2 3 4 5 6 7
## 1 3 3 3 4 1 1
table(x, y)
## y
## x 1 2 3 4 5 6 7
## 1 0 0 0 1 0 1 0
## 2 0 1 0 0 3 0 0
## 3 1 0 1 2 0 0 1
## 4 0 1 2 0 0 0 0
## 5 0 1 0 0 1 0 0
otros comandos:
ls lista de asignaciones hechas
ls()
## [1] "w" "x" "y" "z"
borrar la variable z
rm(z)
ls()
## [1] "w" "x" "y"