Вы находитесь на странице: 1из 5

GUIA 1 - ESTRUCTURA Y BASES DE DATOS EN R STUDIO

ING. CARLOS ALBERTO PUENTES MORALES

SECUENCIAS DE OBJETOS
1:5
[1] 1 2 3 4 5
> 5:1
[1] 5 4 3 2 1

Seq secuencias
> seq (1,5,by=0.2)
[1] 1.0 1.2 1.4 1.6 1.8 2.0 2.2
[8] 2.4 2.6 2.8 3.0 3.2 3.4 3.6
[15] 3.8 4.0 4.2 4.4 4.6 4.8 5.0

Seq con tamaño definido

> seq (1,5,length.out = 20)


[1] 1.000000 1.210526 1.421053
[4] 1.631579 1.842105 2.052632
[7] 2.263158 2.473684 2.684211
[10] 2.894737 3.105263 3.315789
[13] 3.526316 3.736842 3.947368
[16] 4.157895 4.368421 4.578947
[19] 4.789474 5.000000

> curve(x^2,from=-3, to =3)


> curve(sin,from=-2*pi, to =2*pi)
> curve(tan,from=-2*pi, to =2*pi)
> curve(x^2-sqrt(x)+x-3,from=-0,
to =3)

Creando Grafica sin function curve

x=seq(0,3,length=100)
> y=x^2-sqrt(x)+x-3
> plot(y~x)
> plot(y~x,type="l")

Modificando Grafico

> history()
> text(0.5,6, "hola")
> text(0.5,4, expression(Y==x^2-s
qrt(x)+x-3))

Letras Griegas
> text(0.5,2, expression(Y==x^2-s
qrt(x)+x-3+alpha))
Curva de Regresion Logistica
> curve(plogis,from = -3, to=3)
> ?plotmath
> text(-2,0.8,expression(frac(e^x
,1+e^x)))
Varianza
> text(2,0.2,expression(s^2==frac
(sum((x[i]-bar(x))^2,i==1,n),n-1)
))

Vectores Factores categoricos


x=factor(c(1,2,3,3,2,1,1,2),label
s=c("a","b","c"))
> x
[1] a b c c b a a b
Levels: a b c

> x=factor(c(1,2,3,3,2,1,1,2),lab
els=c("desacuerdo","neutral","acu
erdo"))
> x
[1] desacuerdo neutral
[3] acuerdo acuerdo
[5] neutral desacuerdo
[7] desacuerdo neutral
3 Levels: desacuerdo ...

> x=ordered(c(1,2,3,3,2,1,1,2),la
bels=c("desacuerdo","neutral","ac
uerdo"))
> x
[1] desacuerdo neutral acuerdo
acuerdo neutral desacuerdo
desacuerdo
[8] neutral
Levels: desacuerdo < neutral < ac
uerdo

Arreglos y Matrices
DATAFRAME = Lista disfrasada de table,
permite guardar datos int , string, entre otros

La Matriz permite guardar datos de la misma


naturaleza

> a=matrix(1:9)
> a
[,1]
[1,] 1
[2,] 2
[3,] 3
[4,] 4
[5,] 5
[6,] 6
[7,] 7
[8,] 8
[9,] 9

> a=matrix(1:9, ncol = 3)


> a
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9

Acceder a una fila o columna de la Matriz


> a[2,]
[1] 2 5 8
> a[,3]
[1] 7 8 9
> a[-1,]
[,1] [,2] [,3]
[1,] 2 5 8
[2,] 3 6 9

> a=matrix(1:9, ncol = 3)


> a
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9

Determinar al Inversa de la Matriz a


> a=matrix(c(1,0,5,1,2,5,1,3,1),n
col=3)
> a
[,1] [,2] [,3]
[1,] 1 1 1
[2,] 0 2 3
[3,] 5 5 1

> solve(a)
[,1] [,2]
[1,] 1.625 -5.000000e-01
[2,] -1.875 5.000000e-01
[3,] 1.250 3.469447e-17
[,3]
[1,] -0.125
[2,] 0.375
[3,] -0.250
>
Multiplicar Matrices para la identidad
> b=solve(a)
> a%*%b
[,1] [,2]
[1,] 1.000000e+00 9.020562e-17
[2,] 2.220446e-16 1.000000e+00
[3,] 2.220446e-16 2.012279e-16
[,3]
[1,] 2.775558e-17
[2,] -2.775558e-17
[3,] 1.000000e+00

Dimensiones de la Matriz
> str(a)
num [1:3, 1:3] 1 0 5 1 2 5 1 3 1
> attributes(a)
$dim
[1] 3 3

> dim(a)
[1] 3 3

Encuestas Categorizadas en Vector


> genero=factor(c(1,1,1,1,1,1,2,2,2,2,2,2),labels=c("masculino", "femenino"))
> region=factor(c(1,1,1,1,2,2,2,2,3,3,3,3),labels=c("costa","sierra","oriente
"))
> opinion=factor(c(1,2,1,2,1,2,1,2,1,2,1,2),labels=c("si","no"))
> genero
[1] masculino masculino
[3] masculino masculino
[5] masculino masculino
[7] femenino femenino
[9] femenino femenino
[11] femenino femenino
2 Levels: masculino ...
> region
[1] costa costa costa
[4] sierra sierra sierra
[7] oriente oriente oriente
3 Levels: costa ... oriente
> opinion
[1] si no si no si no si no si
[10] no si no
Levels: si no

> length(genero)
[1] 12
> length(region)
[1] 12
> length(opinion)
[1] 12

> table(genero)
genero
masculino femenino
6 6
> table(genero,region)
region
genero costa sierra oriente
masculino 4 2 0
femenino 0 2 4
Consulta de categorías cruzadas
> tabla=table(genero,region,opinion)
> tabla ["masculino","costa","si"]
[1] 2
> tabla ["masculino","costa","no"]
[1] 2
> tabla ["femenino","costa","no"]
[1] 0

Función Suma de datos por categoría


> apply(tabla,1,sum)
masculino femenino
6 6
> apply(tabla,2,sum)
costa sierra oriente
4 4 4
> apply(tabla,3,sum)
si no
6 6

Funcion Suma por dimensiones 1 y 3 (Genero y Opinion)


> apply(tabla,c(1,3),sum)
opinion
genero si no
masculino 3 3
femenino 3 3
> apply(tabla,c(2,3),sum)
opinion
region si no
costa 2 2
sierra 2 2
oriente 2 2

Funcion Media por dimensiones 1 y 3 (Genero y Opinion)


> apply(tabla,c(1,2),mean)
region
genero costa sierra oriente
masculino 2 1 0
femenino 0 1 2

>

>