
####################
# Arreglos
####################

x <- array (1:24, c(3,4,2))
x

x <- 1:24
dim(x) <- c(3,4,2)
x

x[ ,2, ]
x[,2,1]
x[1,,]

###################
## La funcion aperm
###################

aperm(iris3, c(2,3,1))

###################
## La funcion apply
###################

apply( petro.datos, 2, max)

apply(iris3,2,mean)
apply(iris3,c(2,3),mean) 
# apply(iris3,c(2,3),mean, trim=0.1)
apply(iris3,c(2,3),var)

###################
## La funcion sweep
###################

iris.medias <- apply(iris3, c(2,3), mean)
sweep(iris3, c(2,3), iris.medias,'-')

###################
## La funcion outer
###################

(aa <- 1:4)
(bb <- c(2,4,6))
(ab <- aa %o% bb)

(A  <- matrix(1:12, ncol=3))
(B <- 9:10)
(AB <- A %o% B)

(AB <- outer(A,B))


x <- y <- seq(-3,3,length=100)
f <- function(x,y)
 {
	exp(-(x^2+y^2)/2)/(2*pi)
 }
z <- outer(x,y,f)



outer(1:9,1:9)





####################
# Cuadros de datos
####################

prueba.df <- data.frame(7:10, c('a','b','c','d'))
prueba.df
prueba.df <- data.frame(prueba.df, 20+(1:4)/4)
prueba.df

colnames(prueba.df) <- c('V1','V2','V3')
rownames(prueba.df) <- c('S1','S2','S3','S4')
prueba.df


prueba.df$V1
prueba.df['V1']

attach(prueba.df)
V1
S2


attach(prueba.df)
search()
detach()
search()

attributes(prueba.df)

str(prueba.df)




# Fusion <- c("F", "NF", "F", "F", "NF")
# petro.datos1 <- cbind (petro.datos, Fusion)
# petro.datos1
# apply(petro.datos1, 2, max)

# petro.frame <- data.frame (petro.datos, Fusion)
# petro.frame

# petro.frame$Fusion
# petro.frame["Fusion"]

# attach (petro.frame)
# Ventas
# Fusion

# detach()
# rm(Fusion)
# attach (petro.frame)
# Fusion
# detach()
# Fusion

# attach(petro.frame)
# search()
# detach()
# search()

# attributes(petro.frame)
# str(petro.frame)



###################
## La funcion reshape
###################

# str(Indometh) 
# summary(Indometh) 
# Indowide <- reshape(Indometh, v.names="conc", idvar="Subject", timevar="time", 
# direction="wide") 
# Indowide 
# reshape(Indowide, direction="long") 
# reshape(Indowide, idvar="Subject", varying=list(names(wide)[2:12]), v.names="conc", direction="long") 


###################
## La funcion merge
###################

library(MASS)
Animals
mammals
merge(Animals, mammals, by='row.names')

###################
## La funcion aggregate
###################

# str(crabs)
# aggregate(crabs[, 4:8], list(sp=crabs$sp, sex=crabs$sex), median)

###################
## La funcion by
###################

# by(crabs[, 4:8], list(sp=crabs$sp, sex=crabs$sex), mean)


###################
## La funcion order
###################

# (x <- c(4:6,2,1,3,7))
# order(x)
# x[order(x)]

# y <- 7:1
# z <- 1:7
# rbind(x,y,z)[,order(x)]

# (iris10 <- iris[1:10,1:4])
# ordiris <- order(iris10[,1])
# iris10[ordiris,]

as.matrix(A1) * as.matrix(B2)


####################
# Listas
####################

Telefonos <- c('(473) 7329900', '(477) 7726543')

prueba.list <- list(prueba.df,Telefonos)
prueba.list

prueba.list$Telefonos
prueba.list[[1]]['S1','V3']
prueba.list[[1]][,'V1']
prueba.list[[2]]

prueba.list[[1]][2,3]



L <- list(x, c(T,F), c('Buenos','Dias'))
L
L[[2]]
L[[3]][1]

L[[4]] <- c('El', 'nuevo', 'elemento')

L[[2]] <- NULL
L

L <- list(numero = 1:10, boole =c(T,F), mensaje =c('Buenos', 'Dias'))
L
L <- list( 1:10, c(T,F), c('Buenos', 'Dias'))
names(L) <- c('numero', 'boole', 'mensaje')
L$boole
L$nuevo <- 'el nuevo elemento'
names(L)
names(L)[2] <- 'valores logicos'
names(L)

L <- list(vec=1:10, mat=matrix(99:88, 3,4))
L
lapply(L, mean)
lapply(L, '-', 1)

sapply(mtcars, range)
apply(mtcars, 2, range)

# unlist(lapply(L,mean))

round(mean(mtcars),1)




#####################################
###		GRAFICOS		    ###
#####################################

demo(graphics)
demo(persp)
demo(image)

########
## plot
########

attach(iris)
plot(Sepal.Length, Sepal.Width)

plot(Sepal.Width~Sepal.Length)
plot(~Sepal.Length + Sepal.Width)

plot(Species)

plot(Species,Sepal.Length)

plot(iris)

plot(Petal.Length ~ Sepal.Width + Sepal.Length)

plot(~ Petal.Length + Sepal.Width + Sepal.Length)

plot(cars)

plot(nhtemp)

#############
## Argumentos
#############

plot(cars, type='p')
plot(cars, type='l')
plot(cars, type='b')
plot(cars, type='o')
plot(cars, type='h')
plot(cars, type='n')

par(mfrow=c(1,2))
plot(pressure, type='s')
points(pressure)
plot(pressure, type='S')
points(pressure)
par(mfrow=c(1,1))

plot(cars, type='l', xlab='Velocidad', ylab='Distancia',
main='Distancia de Frenado vs. Velocidad', sub='Datos tomados en 1920')


plot(cars, type='l', xlab='Velocidad', ylab='Distancia',
main='Distancia de Frenado vs. Velocidad', sub='Datos tomados en 1920',
lty=2, lwd=2, col=3)

plot(cars, pch=8, cex=1.2, col=4)
plot(cars, pch=15, cex=2, col=5)
plot(cars, pch=4, cex=.5, col=6)

plot(cars, pch='r', cex=1.2, col=6)
plot(cars, pch='~', cex=1.5, col=6)

plot(cars,xlim=c(0,30), ylim=c(0,150))


################
## curve
################

curve(x^3-3*x,-2,2)
curve(x^2-2, add=TRUE, col='blue')


########
## hist
########

par(mfrow=c(2,2))
hist(Sepal.Width)
hist(Sepal.Width,breaks='Scott')
hist(Sepal.Width,breaks='FD')
hist(Sepal.Width,breaks=18,freq=FALSE)

hist(Petal.Length)
hist(Petal.Length,breaks='Scott')
hist(Petal.Length,breaks='FD')
hist(Petal.Length,breaks=18,freq=FALSE)

##########
## Densidades
##########

par(mfrow=c(1,1))
plot(density(Sepal.Length))
plot(density(Sepal.Length,adjust=2))
plot(density(Sepal.Length,adjust=0.5))

par(mfrow=c(2,3))
plot(density(Sepal.Length,kernel='epa'))
plot(density(Sepal.Length,kernel='rec'))
plot(density(Sepal.Length,kernel='tri'))
plot(density(Sepal.Length,kernel='biw'))
plot(density(Sepal.Length,kernel='cos'))
plot(density(Sepal.Length,kernel='opt'))



##########
## boxplot
##########
par(mfrow=c(1,1))

normat <- matrix(rnorm(1000),ncol=10)
boxplot(data.frame(normat))
boxplot(normat)


boxplot(iris[,1:4])
boxplot(iris)

##########
## qqnorm
##########

qqnorm(normat)
qqline(normat)

##########
## qqplot
##########
par(mfrow=c(2,2))

muestra.1 <- rnorm(100)
muestra.2 <- rnorm(100)
qqplot(muestra.1,muestra.2)

cuantiles.t <- qt(1:100/100,df=2)
qqplot(muestra.1,cuantiles.t)
qqplot(muestra.2,cuantiles.t)

##########
## stem
##########

stem(trees$Girth, scale=2)
stem(trees$Girth)

##########
## barplot
##########

tN <- table(Ni <- rpois(100, lambda=5))
barplot(tN)
r <- barplot(tN, col=rainbow(20))

barplot(table(esoph$agegp))
barplot(table(esoph$agegp,esoph$tobgp))
barplot(table(esoph$agegp,esoph$alcgp))
barplot(table(esoph$tobgp,esoph$alcgp),beside=TRUE)


##########
## pairs
##########

pairs(iris)
pairs( ~ Sepal.Length + Petal.Length + Petal.Width)
pairs(iris[1:4], main = "Anderson's Iris Data -- 3 species", pch = 21, bg = c("red", "green3", "blue")[unclass(iris$Species)]) 
pairs(~ Fertility + Education + Catholic, data = swiss, subset = Education < 20, main = "Swiss data, Education < 20") 
pairs(USJudgeRatings)

##########
## coplot
##########

help(quakes)

 coplot(lat ~ long | depth, data = quakes)
 given.depth <- co.intervals(quakes$depth, number=4, overlap=.1) 
 coplot(lat ~ long | depth, data = quakes, given.v=given.depth, rows=1) 

 ll.dm <- lat ~ long | depth * mag 
 coplot(ll.dm, data = quakes) 
 coplot(ll.dm, data = quakes, number=c(4,7), show.given=c(TRUE,FALSE)) 
 coplot(ll.dm, data = quakes, number=c(3,7), overlap=c(-.5,.1)) 

##########
## persp
##########

x <- seq(-10, 10, length=50)
y <- x
f <- function(x,y) {r <- sqrt(x^2+y^2); 10*sin(r)/r}
z <- outer(x,y,f)
z[is.na(z)] <- 1
persp(x,y,z, theta=30, phi=30, expand=0.5, col='lightblue')

###########
## contour
###########

x <- y <- seq(-4*pi, 4*pi, len=27)
r <- sqrt(outer(x^2,y^2,"+"))
opar <- par(mfrow=c(1,1), mar=c(5.1,4.1,4.1,2.1))
par(mfrow=c(2,2), mar=rep(0,4))
for (f in pi^(0:3))
contour(cos(r^2)*exp(-r/f), drawlabels=FALSE, axes=FALSE, frame=TRUE)
par(opar)



