13
Introducci´ on al manejo de R-CRANThe Comprehensive R Archive Network Introducci´on al manejo de R-CRAN The Comprehensive R Archive Network Alvaro Limber Chirino Gutierrez Fundaci´ on ARU 2 de agosto de 2011

Introducción al manejo de R-CRAN The Comprehensive R ... · Introducci on al manejo de R-CRANThe Comprehensive R Archive Network Introducci on Porque usar R? 1 Es gratuito GNU GPL

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introducción al manejo de R-CRAN The Comprehensive R ... · Introducci on al manejo de R-CRANThe Comprehensive R Archive Network Introducci on Porque usar R? 1 Es gratuito GNU GPL

Introduccion al manejo de R-CRANThe Comprehensive R Archive Network

Introduccion al manejo de R-CRANThe Comprehensive R Archive Network

Alvaro Limber Chirino GutierrezFundacion ARU

2 de agosto de 2011

Page 2: Introducción al manejo de R-CRAN The Comprehensive R ... · Introducci on al manejo de R-CRANThe Comprehensive R Archive Network Introducci on Porque usar R? 1 Es gratuito GNU GPL

Introduccion al manejo de R-CRANThe Comprehensive R Archive Network

Introduccion

Porque usar R?

1 Es gratuito GNU GPL General Public Licence

2 Es ejecutable en una variedad de plataformas incluyendoWindows, Unix y MacOS

3 Un lenguaje orientado a objetos.

4 Muy parecido a Matlab y Octave, y con sintaxis que recuerdaa C/C++.

R se obtiene en:

http://cran.r-project.org

Tinn-R un editor externoScript un editor propio de R

Page 3: Introducción al manejo de R-CRAN The Comprehensive R ... · Introducci on al manejo de R-CRANThe Comprehensive R Archive Network Introducci on Porque usar R? 1 Es gratuito GNU GPL

Introduccion al manejo de R-CRANThe Comprehensive R Archive Network

Introduccion

Comandos Iniciales

>setwd()

>?setwd()

>getwd(‘‘Z:/...’’) # Es para abrir un comentario

>dir()

>x<-c(1,2,3,4)

>ls()

>rm(x)

>rm(list=ls())

>help(‘‘for’’)

Page 4: Introducción al manejo de R-CRAN The Comprehensive R ... · Introducci on al manejo de R-CRANThe Comprehensive R Archive Network Introducci on Porque usar R? 1 Es gratuito GNU GPL

Introduccion al manejo de R-CRANThe Comprehensive R Archive Network

Introduccion

Lectura de datos

Se pueden leer los formatos, .dta, .sav, .txt, .csv, .xls, etc.

Ejemplo

>x1<-rnorm(10)

>x2<-rnorm(10,2,3) #Tambien rnorm(100, mean=2,sd=3)

>x3<-scan() #lectura desde el teclado

>x4<-read.delim(‘‘clipboard’’)

>z<-cbind(x1,x2,x3,x4)

>z1<-matrix(c(1,2,3,4,5,6),3,2)

>library(foreign)

>?read.dta

>apropos(‘‘read’’)

Page 5: Introducción al manejo de R-CRAN The Comprehensive R ... · Introducci on al manejo de R-CRANThe Comprehensive R Archive Network Introducci on Porque usar R? 1 Es gratuito GNU GPL

Introduccion al manejo de R-CRANThe Comprehensive R Archive Network

Funciones

Comandos Basicos

x<-rnorm(100)

summary(x)

mean(x)

sd(x)

cor(x)

cov(x)

median(x)

quantile(x,0.99)

quantile(x,c(0.01,0.1,0.5))

y<-rbinom(100,1,0.7)

mean(y)

sd(y)

.

.

Page 6: Introducción al manejo de R-CRAN The Comprehensive R ... · Introducci on al manejo de R-CRANThe Comprehensive R Archive Network Introducci on Porque usar R? 1 Es gratuito GNU GPL

Introduccion al manejo de R-CRANThe Comprehensive R Archive Network

Funciones

Familia apply

Estas funciones sirven para resumir y empaquetar informacion

apply()

tapply()

lapply()

aggregate()

by()

table()

Page 7: Introducción al manejo de R-CRAN The Comprehensive R ... · Introducci on al manejo de R-CRANThe Comprehensive R Archive Network Introducci on Porque usar R? 1 Es gratuito GNU GPL

Introduccion al manejo de R-CRANThe Comprehensive R Archive Network

Librerias

Cargar librerıas

R se alimenta de librerias externas para mejorar e implementarnuevos procesosExisten dos formas para cargar una libreria.

1 Si la libreria no se encuentra almacenada en R, se la debeinstalar y cargar.

2 Si la libreria se encuentra almacenada, se la debe cargar

Ejemplo

?faces

library()

library(TeachingDemos)

update.packages()

Page 8: Introducción al manejo de R-CRAN The Comprehensive R ... · Introducci on al manejo de R-CRANThe Comprehensive R Archive Network Introducci on Porque usar R? 1 Es gratuito GNU GPL

Introduccion al manejo de R-CRANThe Comprehensive R Archive Network

Graficos

Graficos Univariados

Histogram of rnorm(100, 2, 3)

rnorm(100, 2, 3)

Freque

ncy

−6 −2 0 2 4 6 8

05

1015

2025

−50

510

hist(rnorm(100,2,3)) boxplot(rnorm(100,2,3))

Page 9: Introducción al manejo de R-CRAN The Comprehensive R ... · Introducci on al manejo de R-CRANThe Comprehensive R Archive Network Introducci on Porque usar R? 1 Es gratuito GNU GPL

Introduccion al manejo de R-CRANThe Comprehensive R Archive Network

Graficos

Graficos Multivariados

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

11

1

11

1

1

11

1

1111

1

1

1

1

11

1

11

1

1

1

1

1

1

1

1

1

111

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

1

11

11

1

1

1

1

1

11

1

1

1

1

1

1

1

1

1

11

1

1

1

11

1

11

11

1

0 20 40 60 80 100

−3−2

−10

12

z 2

2

2

2

2

2

2

2

2

2

2

2

2

2

22

2

2

2

2

2

2

2

2

2

2

2

2

2

2

22

2222

2

22

2

2

2

2

2

2

2

22

2

2

2

2

2

2

2

2

2

2

2

2

2

2

2

2

2

2

2

2

2

2

2

2

2

2

2

2

2

22222

2

2

2

2

2

2

2

2

2

2

2

2

2

2

2

2

22

33

33

3

3

3

3

3

3

3

33

33

3

33

3

3

3

3

3

3

3

3

3

3

3

3

3

3

3

3

3

3

3

3

3

3

3

3

3333

33

3

3

3

3

3

3

3

3

3

33

3

3

3

3

3

3

33

3

33

3

3

3

3

3

3

3

3

33

3

3

3

33

3

3

33

3

3

3

3

33

3

3

3

3

34

4

4

4

4

4

4

4

4

4

4

4

4

4

44

4

4

4

4

4

44

4

4

4

44

4

4

444

4

4

4

44

4

4

4

4

4

44

4

4

4

4

4

4

4

4

4

4

4

4

44

444

4

4

44

4

4

44

4

4

4

4

44

4

4

4

4

4

4

4

4

4

4

4

4

4

4

4

44

4

4

4

44

4

4

5

55

5

55

5

5

5

5

5

55

5

5

5

5

5

5

5

5

5

5

5

5

5

5

55

5

5

5

55

55

5

5

5

5

5

5

5

55

5

5

5

55

5

5

5

5

5

5

55

55

55

5

5

5

55

5

5

5

55

5

5

5

5

5

5

5

5

5

5

5

5

5

5

55

55

55

5

5

5

55

5

5

5

6

6

6

6

6

6

6

66

6

6

6

6

6

6

6

66

6

6

6

6

66

6

6

6

6

6

6

6

6

6

6

6

6

6

66

6

6

6

6

6

6

6

6

6

6

6

6

66

6

6

66

6

6

66

6

6

6

6

6

6

6

6

6

6

6

6

6

666

66

6

6

6

66

6

6

66

6

6

66

6

6

6

66666

7

7

7

7

7

7

7

7

777

777

7

77

7

7

7

77

7

7

7

7

7

7

7

7

7

77

7

77

7

7

7

77

7

7

7

77

7

7

7

7

7

7

7

7

777

7

7

7

7

7

77

7

7

7

7

7

7

7

7

7

7

7

7

7

77

7

7

7

7

7

7

7

7

7

777

7

7

7

77

7

7

7

78

8

8

8

8

8

88

8

8

8

88

8

8

8

8

8

8

8

8

8

8

8

8

8

8

8

8

8

8

888

88

8

8

8

8

88

8

8

8

8

888888

8

8

8

8

8

8

8

8

8

8

8

8

8

8

8

8

8

8

8

8

8

8

8

8

8

888

8

88

8

8

8

8

8

8

8

8

8

8

8

8

8

8

8

8

8

9

9999

9

9

9

9

9

99

9

9

9

9

9

9

9

9

9

9

9

9

99

9

9

9

9

9

9

9

9

9

9

9

9

9

9

9

9

9

9

9

9

9

9

99

99

9

99

9

999

9

9

9

9

99

9

9

9

9

9

9

99

9

9

9

9

9

9

9

9

9

9

9

9

9

9

9

9

9

9

9999

9

9

9

9

90

0

0

00

00

0

0

0

0

0

0

00

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

00

0

0

0

0

0

0

00

0

0

0

00

0

000

0

0

0

0

0

0

0

0

00

0

0

0

00

0

00

0

0

0

00

0

00

0

0

0

0

0

0

0

00

0

0

0

0

00

0

00

0

1111111111111111111111

111111111111111111111111111111

11111111111111111111111

111111111111111

1111111111

0 20 40 60 80 100

020

4060

80100

z22222

222222222222

2222222222

222222222222222

2222222222222

222222222222222222

222222222

22222222222222222233333333

333333333333333333

3333333333

333333333333333333333333333

3333333333

33333333333333333

3333333333

4

444444444444444444

4444444444444444

444444444444

444444444444444

44444444444444444

444444444444444444444555

555555555555555

55555555555

555555555555555555555555555555555

5555555

555555555555555555555

5555555555

666666666666

666666666666666666

6666666666666666

6666666666666666

666666666666666

666666666666

666666666667

7777777777777

77777777777

77777777777777777777777777

7777777777777777777777777777777777

777777777777777888888888888888

8888888888888888

888888888888888888888888888888

8888888888888888888888

8888888888888888899999

9999999999999999999999

999999999999

99999999999

999999999999

9999999999999999999999999

99999999999990

0000000000000000

000000000000000

00000000000000000000000000

000000000000000000000

0000000000000000

00000

z<-data.frame(matrix(rnorm(1000),100,10))

matplot(z)

z[2,4]<-100

matplot(z)

Page 10: Introducción al manejo de R-CRAN The Comprehensive R ... · Introducci on al manejo de R-CRANThe Comprehensive R Archive Network Introducci on Porque usar R? 1 Es gratuito GNU GPL

Introduccion al manejo de R-CRANThe Comprehensive R Archive Network

Graficos

Mas Graficos

1998

2000

2002

2004

2006

2008

2010

1998

2000

2002

2004

2006

2008

2010

Hombre

Mujer

36.5 37.0 37.5

Lapob media de edad

dotchart(tapply(b1$q2,list(b1$year,b1$q1),mean+

+,na.rm=T),main="Lapob media de edad")

Page 11: Introducción al manejo de R-CRAN The Comprehensive R ... · Introducci on al manejo de R-CRANThe Comprehensive R Archive Network Introducci on Porque usar R? 1 Es gratuito GNU GPL

Introduccion al manejo de R-CRANThe Comprehensive R Archive Network

Graficos

Mas Graficos

X4

−3 −2 −1 0 1 2

●●

●●

●●

●●

●●

●●

●●

−2−1

01

2

●●

●●

●●

●●

●●

●●

●●

−3−2

−10

12

●●

● ●

●●

●●

●●

●●

●●

●●

● ●

●●

●●●

●●

X5

●●

● ●

●●

●●

●●

●●

●●

●●

● ●

●●

●●●

●●

−2 −1 0 1 2

●●

● ●

● ●

●●

●●

●●

●●

●●●

●●

●●

● ●●●

●●

●●

●●

● ●

● ●

●●

●●

●●

●●

● ●●

●●

●●

●●●●

●●

●●

−3 −2 −1 0 1 2 3

−3−2

−10

12

3

X6

pairs(x[,4:6],panel=panel.smooth)

Page 12: Introducción al manejo de R-CRAN The Comprehensive R ... · Introducci on al manejo de R-CRANThe Comprehensive R Archive Network Introducci on Porque usar R? 1 Es gratuito GNU GPL

Introduccion al manejo de R-CRANThe Comprehensive R Archive Network

Programacion

Definicion de funciones

Con la creacion de funciones, pueden automatizarse procesos.

if(cond) expr

if(cond) cons.expr else alt.expr

for(var in seq) expr

while(cond) expr

Ejemplo

aru<-function(x){

x<-data.frame(x)

for(i in 1:dim(x)[2]){

print(quantile(x[,i]))}

print(summary(x))

}

Page 13: Introducción al manejo de R-CRAN The Comprehensive R ... · Introducci on al manejo de R-CRANThe Comprehensive R Archive Network Introducci on Porque usar R? 1 Es gratuito GNU GPL

Introduccion al manejo de R-CRANThe Comprehensive R Archive Network

Programacion

Otro ejemplo

norm<-function(x)

{

y<-(1/sqrt(2*pi))*(exp(-(x^2)/2))

return(y)

}

plot(norm,xlim=c(-5,5),ylim=c(0,0.5))

−4 −2 0 2 4

0.00.1

0.20.3

0.4

x

norm (x)