Complicados en ocasiones de manipular
# generar un vector numerico
## 1, 2, 3, 4
# 1= norte, 2=sur, 3=este....
# vector numerico
nombres_fac=sample(c(1,2),10,replace = TRUE)
nombres_fac
[1] 1 2 2 2 2 1 2 2 1 2
# darle nombres
class(nombres_fac)
[1] “numeric”
levels(nombres_fac)=c('alto','bajo')
levels(nombres_fac)=c('rico','pobre')
nombres_fac
[1] 1 2 2 2 2 1 2 2 1 2 attr(,“levels”) [1] “rico” “pobre”
# DE TIEMPO
tiempo=1900:2020
# replicar numeros
# el numero y cuantas veces
rep(1,10)
[1] 1 1 1 1 1 1 1 1 1 1
# replicar numero del 1 al 8 dos veces
rep(1:8,each=2)
[1] 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8
# replicar 0 y 1, del primero 10 y del segundo 5
rep(0:1, c(10,5))
[1] 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1
rep(0:2,c(10,5,5))
[1] 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 2 2 2 2 2
#
rep(c(1:2,5),c(10,5,5))
[1] 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 5 5 5 5 5
# replicar el 1 y el 8, el 1 ocho veces y el 8 12 veces
rep(c(1,8),c(8,12))
[1] 1 1 1 1 1 1 1 1 8 8 8 8 8 8 8 8 8 8 8 8
# secuencia
# secuencia tiene, donde empieza, el tamano y cada cuanto
# secuencia de 1, del tamano de a1, cada .5
a1=1:15
# seq(10,tamaño,cada cuanto)
seq(5,10,.1)
[1] 5.0 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 6.0 6.1 6.2 6.3 6.4 [16] 6.5 6.6 6.7 6.8 6.9 7.0 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 [31] 8.0 8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 8.9 9.0 9.1 9.2 9.3 9.4 [46] 9.5 9.6 9.7 9.8 9.9 10.0
# secuencia que vaya de alrededor a a1
seq(1,length(a1),1)
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
seq(1,length(a1),.5)
[1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 [16] 8.5 9.0 9.5 10.0 10.5 11.0 11.5 12.0 12.5 13.0 13.5 14.0 14.5 15.0
# secuencias de del, con 10 numeros de 1
seq(2,10,1)
[1] 2 3 4 5 6 7 8 9 10
# que empiece en 5
seq(5,10,1)
[1] 5 6 7 8 9 10
# vector de fechas
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
fecha=190101:190131
#
fecha1=ymd(fecha)
fecha1
[1] “2019-01-01” “2019-01-02” “2019-01-03” “2019-01-04” “2019-01-05” [6] “2019-01-06” “2019-01-07” “2019-01-08” “2019-01-09” “2019-01-10” [11] “2019-01-11” “2019-01-12” “2019-01-13” “2019-01-14” “2019-01-15” [16] “2019-01-16” “2019-01-17” “2019-01-18” “2019-01-19” “2019-01-20” [21] “2019-01-21” “2019-01-22” “2019-01-23” “2019-01-24” “2019-01-25” [26] “2019-01-26” “2019-01-27” “2019-01-28” “2019-01-29” “2019-01-30” [31] “2019-01-31”
Realizando una grafica
var1=cumsum(rnorm(31))
library(lubridate)
fecha=190101:190131
fecha1=ymd(fecha)
# serie de tiempo, eje horizontal, seguendo variable
## colores de html en internet
## #115C63
plot(fecha1,var1,type='l',lwd=3,lty=2,
col="#115C63", xlab = "Tiempo", ylab='', main='', cex.axis=.6)
a1=c(1,2,3,1,2)
a2=c(1,2,4,1,2)
a3=c(1,2,1,1,1)
b1=c(1,2)
b2=c(1,1)
# uniendo por columnas
mat1=cbind(a1,a2,a3)
# uniendo por filas
rbind(a1,a2,a3)
## [,1] [,2] [,3] [,4] [,5]
## a1 1 2 3 1 2
## a2 1 2 4 1 2
## a3 1 2 1 1 1
# colocando nombres
matriz=rbind(a1,a2,a3)
matriz
## [,1] [,2] [,3] [,4] [,5]
## a1 1 2 3 1 2
## a2 1 2 4 1 2
## a3 1 2 1 1 1
dim(matriz)
## [1] 3 5
#
colnames(matriz)=c('a','b','c','d','e')
rownames(matriz)=c('azul','rojo','verde')
matriz
## a b c d e
## azul 1 2 3 1 2
## rojo 1 2 4 1 2
## verde 1 2 1 1 1
#
proportions(matriz)# igual a 1
## a b c d e
## azul 0.04 0.08 0.12 0.04 0.08
## rojo 0.04 0.08 0.16 0.04 0.08
## verde 0.04 0.08 0.04 0.04 0.04
proportions(matriz,margin=1)# rows filas igual a 1
## a b c d e
## azul 0.1111111 0.2222222 0.3333333 0.1111111 0.2222222
## rojo 0.1000000 0.2000000 0.4000000 0.1000000 0.2000000
## verde 0.1666667 0.3333333 0.1666667 0.1666667 0.1666667
proportions(matriz,margin=2)
## a b c d e
## azul 0.3333333 0.3333333 0.375 0.3333333 0.4
## rojo 0.3333333 0.3333333 0.500 0.3333333 0.4
## verde 0.3333333 0.3333333 0.125 0.3333333 0.2