5+3
## [1] 8
15.3*23.4
## [1] 358.02
sqrt(16)# saca la raíz cuadrada
## [1] 4
producto = 15.3*23.4 #guarda los valores de las variables con un nombre
producto <- 15.3*23.4
log(producto)
## [1] 5.880589
log10(producto)
## [1] 2.553907
log(producto, base=2)
## [1] 8.483896
x<-1:5#mal estilo
x <- 1:5#mejor presentación
x <- 1:10 #se utiliza el operador":"
seq(1, 5, 0.5)
## [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
z <- scan()
rep(1, 20)
## [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
x <- (1:15)
sum(x) #suma los elementos de x
## [1] 120
prod(x)#producto de los elementos de x
## [1] 1.307674e+12
max(x)#devuelve el máximo en el objeto x
## [1] 15
min(x)# devuelve el mínimo en el objeto x
## [1] 1
which.max(x)#devuelve el índice del elemento máximo
## [1] 15
which.min(x)# devuelve el índice del elemento mínimo
## [1] 1
f_x <- function(x){
y <- x^2
y
}
getwd()# devuelve el directorio actual
## [1] "G:/CURSO R/Deberes_R/Modulo 3"
setwd("G:/CURSO R/Deberes_R/Modulo 3")
Mundo <- read.csv("Mundo.csv", sep = ";", header = TRUE)# Para leer los datos en excel
# header=TR se utiliza para que R lea a la primera fila con etiquetas
#se pone el nombre del archivo
str(Mundo)# describe las variables del dataframe
## 'data.frame': 146 obs. of 13 variables:
## $ NOMBRE : Factor w/ 146 levels "AFGANISTAN ",..: 4 6 14 16 19 20 22 23 31 32 ...
## $ PNB : int NA 60728 1315 1202 1589 1205 170 10494 159 1761 ...
## $ REGION : int 1 1 1 1 1 1 1 1 1 1 ...
## $ Pob_Urbana: Factor w/ 129 levels "","1,1","10,4",..: 29 54 45 21 122 92 83 61 27 47 ...
## $ poblacion : Factor w/ 118 levels "0,3","0,4","0,5",..: 14 57 80 10 114 87 2 18 3 42 ...
## $ natalidad : Factor w/ 122 levels "","10,4","10,7",..: 109 80 116 110 109 102 74 84 101 97 ...
## $ exp_vida : Factor w/ 110 levels "","36","37","39",..: 7 48 11 38 13 17 46 28 25 17 ...
## $ tasaM_inf : int 137 74 110 67 138 112 66 94 80 73 ...
## $ PNB_PC : int NA 2629 305 1059 191 241 494 966 372 873 ...
## $ tasa_ferti: Factor w/ 57 levels "","1,4","1,5",..: 46 43 52 45 47 45 35 40 44 42 ...
## $ tasa_crec : Factor w/ 124 levels "-0,01","-0,02",..: 83 99 101 114 81 90 87 75 98 84 ...
## $ tasa_mort : int 20 9 19 12 19 17 10 16 15 17 ...
## $ calorias : int NA 113 93 93 83 95 111 89 89 114 ...
Vector: Es un conjunto de elementos del mismo tipo “<- c()” se utiliza para crear vectores
x <- c(1, 2, 3); y <- c("a" ,"b" ,"Hola")
z1 <- c(TRUE, TRUE, FALSE)
X <- 9:29
X <- c(1+0i, 2+4i)
y <- c(1,7, "a")
x <- 0:6
class (x)#sirve para ver que tipos de datos tiene la variable
## [1] "integer"
as.numeric(x)
## [1] 0 1 2 3 4 5 6
as.logical(x)
## [1] FALSE TRUE TRUE TRUE TRUE TRUE TRUE
as.complex(x)# imaginario
## [1] 0+0i 1+0i 2+0i 3+0i 4+0i 5+0i 6+0i
a <- c(1,3,2)
a
## [1] 1 3 2
t(a)
## [,1] [,2] [,3]
## [1,] 1 3 2
7*a
## [1] 7 21 14
a <- c(1,3,2)
b <- c(2,8,9)
a+b
## [1] 3 11 11
sum(a*b)
## [1] 44
sqrt(sum(a*a))
## [1] 3.741657
y <- c(1.2, 3.9, 0.4, 0.12)
y[c(1,3)]
## [1] 1.2 0.4
v <- 3:4
y[v]
## [1] 0.40 0.12
x <- c(4, 2, 17, 5)
y <- x[c(1, 1, 3)]
y
## [1] 4 4 17
z <- c(5, 12, 13)
z[-1]
## [1] 12 13
z[-1:-2]
## [1] 13
z[1:length(z)-1]
## [1] 5 12
z <- seq(1, 5, 0.5)
z [1:length(z)-1]
## [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5
length(z)
## [1] 9
1:length(z)-1
## [1] 0 1 2 3 4 5 6 7 8
z[c(1,3)]
## [1] 1 2
x = c(1, 2, 3)
y = c(3, 4)
my_vector <- c(1:20)
dim(my_vector)# le hago al vector matriz
## NULL
dim(my_vector) <- c(4, 5)#puedo darle la caracteristica a un vector para que se haga matriz y medir su dimensión
my_vector
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 5 9 13 17
## [2,] 2 6 10 14 18
## [3,] 3 7 11 15 19
## [4,] 4 8 12 16 20
class(my_vector)
## [1] "matrix"
my_matrix <- my_vector
my_matrix <- my_vector
my_matrix2 <- matrix(1:20, nrow = 4, ncol = 5)
identical(my_matrix, my_matrix2)# se utiliza para poder ver la similitud de los datos
## [1] TRUE
dim(my_matrix2)
## [1] 4 5
patients <- c("Bill", "Gina", "Kelly", "Sean")
cbind(patients, my_matrix)# une matrices con respecto a columas
## patients
## [1,] "Bill" "1" "5" "9" "13" "17"
## [2,] "Gina" "2" "6" "10" "14" "18"
## [3,] "Kelly" "3" "7" "11" "15" "19"
## [4,] "Sean" "4" "8" "12" "16" "20"
my_data <- data.frame(patients, my_matrix)
#data.frame= combina vectores de diferente tipo
class(my_data)
## [1] "data.frame"
cnames <- c("patient", "age", "weight", "bp", "rating", "test")
colnames(my_data) <- cnames
my_data
## patient age weight bp rating test
## 1 Bill 1 5 9 13 17
## 2 Gina 2 6 10 14 18
## 3 Kelly 3 7 11 15 19
## 4 Sean 4 8 12 16 20
my.data.frame <- data.frame(
ID = c("Carla", "Predro", "Laura"),
Edad = c(10, 25, 33),
Ingreso = c(NA, 34, 15),
Sexo = c(TRUE, FALSE, TRUE),
Etnia = c("Mestizo", "Afroecuatoriano", "Indígena")
)
my.data.frame
## ID Edad Ingreso Sexo Etnia
## 1 Carla 10 NA TRUE Mestizo
## 2 Predro 25 34 FALSE Afroecuatoriano
## 3 Laura 33 15 TRUE Indígena
A <- list("ID", "Edad", "Ingreso", "Sexo", "Etnia")# como crear una lista
my.data.frame
## ID Edad Ingreso Sexo Etnia
## 1 Carla 10 NA TRUE Mestizo
## 2 Predro 25 34 FALSE Afroecuatoriano
## 3 Laura 33 15 TRUE Indígena
A
## [[1]]
## [1] "ID"
##
## [[2]]
## [1] "Edad"
##
## [[3]]
## [1] "Ingreso"
##
## [[4]]
## [1] "Sexo"
##
## [[5]]
## [1] "Etnia"
L1 <- list(c(1,2), "A", c(1,2,3))
my.data.frame[2,3]
## [1] 34
my.data.frame[2, "Ingreso"]
## [1] 34
my.data.frame[[1]]
## [1] Carla Predro Laura
## Levels: Carla Laura Predro
my.data.frame[1]
## ID
## 1 Carla
## 2 Predro
## 3 Laura
str(my.data.frame[[1]])
## Factor w/ 3 levels "Carla","Laura",..: 1 3 2
L1[2]; L1[3]
## [[1]]
## [1] "A"
## [[1]]
## [1] 1 2 3
m1 <- matrix(1, nr = 2, nc = 2)
m2 <- matrix(2, nr = 2, nc = 2)
rbind(m1, m2)# une filas a la matriz
## [,1] [,2]
## [1,] 1 1
## [2,] 1 1
## [3,] 2 2
## [4,] 2 2
cbind(m1, m2)# une columnas a la matriz
## [,1] [,2] [,3] [,4]
## [1,] 1 1 2 2
## [2,] 1 1 2 2
ma <- rbind(m1, m2) %*% cbind(m1, m2)
ma
## [,1] [,2] [,3] [,4]
## [1,] 2 2 4 4
## [2,] 2 2 4 4
## [3,] 4 4 8 8
## [4,] 4 4 8 8
t(ma)
## [,1] [,2] [,3] [,4]
## [1,] 2 2 4 4
## [2,] 2 2 4 4
## [3,] 4 4 8 8
## [4,] 4 4 8 8
det(ma)
## [1] 0
A <- matrix(c(3,5,1,2,3,1,1,4,-1), ncol=3)
b <- c(1,2,1)
solve(A,b)
## [1] -4 6 1
x <- array(1:20, dim = c(4,5))
x
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 5 9 13 17
## [2,] 2 6 10 14 18
## [3,] 3 7 11 15 19
## [4,] 4 8 12 16 20
z <- factor(LETTERS[1:3], ordered = TRUE)
z
## [1] A B C
## Levels: A < B < C
x <- factor(c("a", "b", "b", "a"))
x
## [1] a b b a
## Levels: a b
sex_char <- c("m", "m", "m")
sex_factor <- factor(sex_char, levels = c("m", "f"))
table(sex_char)
## sex_char
## m
## 3
table(sex_factor)
## sex_factor
## m f
## 3 0
x <- c(1, 2, NA, 10, 3)
is.na(x)#Reconoce si un onbejeto es un valor perdido
## [1] FALSE FALSE TRUE FALSE FALSE
x <- c(1, 2, NA, 10, 3)
is.nan(x)# se usa para comprobar si un objeto es NaN
## [1] FALSE FALSE FALSE FALSE FALSE
x <- 1:5000
y <- x^2
z=0
for(i in 1:5000) z[i] <- x[i]^2
for (i in 1:5)
{print(i)}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
for(i in c(3, 2, 9, 6)){print(i^2)}
## [1] 9
## [1] 4
## [1] 81
## [1] 36
medios.transporte <- c("carro", "camion", "metro", "moto")
for(vehiculo in medios.transporte){print(vehiculo)}
## [1] "carro"
## [1] "camion"
## [1] "metro"
## [1] "moto"
i <- 1
while(i<=10) i <- i+4
i
## [1] 13
i <- 1
while (TRUE){
i <- i+4
if (i>=10) break
}
i
## [1] 13
r=5
if (r==4){
x <- 1
}else{
x <- 3
y <- 4
}
r
## [1] 5
oddcount <- function(x)# cuenta el número de elementos impares
{
k <- 0#se asigna 0 a k
for(n in x)
{
if(n%%2 ==1) k <- k+1
}
return(k)
}
x <- seq(1:3)
oddcount(x)
## [1] 2
VAN <- function(I0,n,K,V)
{
for (i in 1:n)
{
y[i] <- V/(1+K)^i
}
sum(y) - I0
}
set.seed(314)#para obtener los mismos resultados
x <- list(a = 1:5, b = rnorm(10))
x
## $a
## [1] 1 2 3 4 5
##
## $b
## [1] -1.28823580 0.72746102 -0.83292490 -0.70367651 0.12724812
## [6] -0.33494136 -0.69869011 0.01267248 0.58898930 -1.76808543
lapply(x, mean)
## $a
## [1] 3
##
## $b
## [1] -0.4170183
x <- list(a =1:4, b = rnorm(10), c = rnorm(20, 1), d = rnorm(100, 5))
lapply(x, mean)
## $a
## [1] 2.5
##
## $b
## [1] -0.4172353
##
## $c
## [1] 0.6981742
##
## $d
## [1] 5.136675
sapply(x, mean)# simplifica el resultado del lapply
## a b c d
## 2.5000000 -0.4172353 0.6981742 5.1366750
set.seed(314)
x <- matrix(rnorm(200), 20, 10)
apply(x, 2, mean)
## [1] -0.41712683 -0.30182581 0.26239167 0.15806333 0.18902125
## [6] -0.27800742 0.35190633 -0.02281625 0.08226070 -0.33599056
x<- c(rnorm(10), runif(10), rnorm(10, 1))
f <- gl(3,10)
f
## [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
## Levels: 1 2 3
tapply(x, f, mean)
## 1 2 3
## -0.2644185 0.2945518 1.1587734
data(InsectSprays)
InsectSprays$x <- rnorm(length(InsectSprays$count))
by(InsectSprays, InsectSprays$spray, summary)
## InsectSprays$spray: A
## count spray x
## Min. : 7.00 A:12 Min. :-1.447362
## 1st Qu.:11.50 B: 0 1st Qu.:-0.241981
## Median :14.00 C: 0 Median :-0.197409
## Mean :14.50 D: 0 Mean :-0.138344
## 3rd Qu.:17.75 E: 0 3rd Qu.:-0.008667
## Max. :23.00 F: 0 Max. : 1.202354
## --------------------------------------------------------
## InsectSprays$spray: B
## count spray x
## Min. : 7.00 A: 0 Min. :-2.2355
## 1st Qu.:12.50 B:12 1st Qu.:-0.8229
## Median :16.50 C: 0 Median :-0.3112
## Mean :15.33 D: 0 Mean :-0.2917
## 3rd Qu.:17.50 E: 0 3rd Qu.: 0.2787
## Max. :21.00 F: 0 Max. : 1.8732
## --------------------------------------------------------
## InsectSprays$spray: C
## count spray x
## Min. :0.000 A: 0 Min. :-2.2830
## 1st Qu.:1.000 B: 0 1st Qu.:-0.9402
## Median :1.500 C:12 Median :-0.4979
## Mean :2.083 D: 0 Mean :-0.2546
## 3rd Qu.:3.000 E: 0 3rd Qu.: 0.7602
## Max. :7.000 F: 0 Max. : 1.4756
## --------------------------------------------------------
## InsectSprays$spray: D
## count spray x
## Min. : 2.000 A: 0 Min. :-2.44272
## 1st Qu.: 3.750 B: 0 1st Qu.:-0.52753
## Median : 5.000 C: 0 Median :-0.05095
## Mean : 4.917 D:12 Mean :-0.16395
## 3rd Qu.: 5.000 E: 0 3rd Qu.: 0.33320
## Max. :12.000 F: 0 Max. : 1.29893
## --------------------------------------------------------
## InsectSprays$spray: E
## count spray x
## Min. :1.00 A: 0 Min. :-2.1626
## 1st Qu.:2.75 B: 0 1st Qu.:-0.6898
## Median :3.00 C: 0 Median : 0.2808
## Mean :3.50 D: 0 Mean :-0.1063
## 3rd Qu.:5.00 E:12 3rd Qu.: 0.5652
## Max. :6.00 F: 0 Max. : 1.2611
## --------------------------------------------------------
## InsectSprays$spray: F
## count spray x
## Min. : 9.00 A: 0 Min. :-2.07647
## 1st Qu.:12.50 B: 0 1st Qu.:-0.39270
## Median :15.00 C: 0 Median :-0.08189
## Mean :16.67 D: 0 Mean :-0.17220
## 3rd Qu.:22.50 E: 0 3rd Qu.: 0.38429
## Max. :26.00 F:12 Max. : 1.35924
# ?InsectSprays permite hacer un help
#summary ---genera un resumen de indicadores estadísticos
aggregate(InsectSprays[, -2], list(InsectSprays$spray), median)
## Group.1 count x
## 1 A 14.0 -0.19740908
## 2 B 16.5 -0.31119883
## 3 C 1.5 -0.49785168
## 4 D 5.0 -0.05095183
## 5 E 3.0 0.28083517
## 6 F 15.0 -0.08188617
InsectSprays$spray=="A"
## [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [12] TRUE 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
InsectSprays[InsectSprays$spray=="A",]
## count spray x
## 1 10 A -0.23610173
## 2 7 A 0.08748844
## 3 20 A 0.58000985
## 4 14 A -0.08337668
## 5 14 A -0.04071951
## 6 12 A -0.17655186
## 7 10 A -0.23481536
## 8 23 A 1.20235428
## 9 17 A -0.21826629
## 10 20 A -1.44736210
## 11 14 A -0.25962040
## 12 13 A -0.83316491
subset(InsectSprays, InsectSprays$spray=="A")#sacar un subgrupo
## count spray x
## 1 10 A -0.23610173
## 2 7 A 0.08748844
## 3 20 A 0.58000985
## 4 14 A -0.08337668
## 5 14 A -0.04071951
## 6 12 A -0.17655186
## 7 10 A -0.23481536
## 8 23 A 1.20235428
## 9 17 A -0.21826629
## 10 20 A -1.44736210
## 11 14 A -0.25962040
## 12 13 A -0.83316491
x <- seq(10,20,1)
plot(x)
y <- seq(30, 40, 1)
plot(x,y)
boxplot(Mundo$PNB_PC)
attach(Mundo)# sirve para fijar la base luego de abrirla
boxplot(PNB_PC)
boxplot(log(PNB_PC))
boxplot(log(PNB_PC)~REGION, col=rainbow(5))
fregion=factor(REGION, labels = c("Africa", "América", "Asia", "Europa", "Oceania"))
boxplot(log(PNB_PC)~fregion, col=rainbow(5))
z.pie <- c(20,40,10,30)
pie(z.pie)
names(z.pie) <- c("Soltero", "Casado", "Viudo", "Divorciado")
pie(z.pie)
pie(z.pie, col = c("purple", "violetred1", "green3", "cornsilk"))
curve(x^3-3*x,-2,2)
curve(cos(3*x), 0, 3, col = "blue")
curve(sin(2*x), 0,3, col = "red", add = TRUE)
library(rgl)#instalar
r <- 1 # radio de las esfera
a <- runif(1000, 0, 2*pi)
u <- runif(1000, -r, r)#diametro de la esfera
x <- cos(a)*sqrt(1-u^2)
y <- sin(a)*sqrt(1-u^2)
z <- u
plot3d(x, y, z, col = "blue")
library(ggplot2)
data(diamonds)
plot(diamonds$carat, diamonds$price, col = diamonds$color, pch = as.numeric(diamonds$cut))
ggplot(diamonds, aes(carat, price, col = color , shape = cut)) +
geom_point()
U <- c("riky", "caro", "camilo", "rosa", "santy")
N <- length(U)
n <- 2
Support(N,n)
## [,1] [,2]
## [1,] 1 2
## [2,] 1 3
## [3,] 1 4
## [4,] 1 5
## [5,] 2 3
## [6,] 2 4
## [7,] 2 5
## [8,] 3 4
## [9,] 3 5
## [10,] 4 5
Support(N, n, U)
## Warning in if (ID == FALSE) {: la condición tiene longitud > 1 y sólo el
## primer elemento será usado
## [,1] [,2]
## [1,] "riky" "caro"
## [2,] "riky" "camilo"
## [3,] "riky" "rosa"
## [4,] "riky" "santy"
## [5,] "caro" "camilo"
## [6,] "caro" "rosa"
## [7,] "caro" "santy"
## [8,] "camilo" "rosa"
## [9,] "camilo" "santy"
## [10,] "rosa" "santy"
p <- c(0.13, 0.2, 0.15, 0.1, 0.15, 0.04, 0.02, 0.06, 0.07, 0.08)
#Note que los elementos suman 1 y ninguno es negativo
sum(p)
## [1] 1
Ind <- Ik(N,n)
Q <- Support(N, n, U)
## Warning in if (ID == FALSE) {: la condición tiene longitud > 1 y sólo el
## primer elemento será usado
data.frame (Q, p, Ind)
## X1 X2 p X1.1 X2.1 X3 X4 X5
## 1 riky caro 0.13 1 1 0 0 0
## 2 riky camilo 0.20 1 0 1 0 0
## 3 riky rosa 0.15 1 0 0 1 0
## 4 riky santy 0.10 1 0 0 0 1
## 5 caro camilo 0.15 0 1 1 0 0
## 6 caro rosa 0.04 0 1 0 1 0
## 7 caro santy 0.02 0 1 0 0 1
## 8 camilo rosa 0.06 0 0 1 1 0
## 9 camilo santy 0.07 0 0 1 0 1
## 10 rosa santy 0.08 0 0 0 1 1
multip <- p*Ind
colSums(multip)
## [1] 0.58 0.34 0.48 0.33 0.27
pik <- Pik(p, Ind)
sum(pik)
## [1] 2