

###############
## Iteraciones
###############


x <- 1:50000
y <- x^2
for (i in 1:50000) z[i] <- x[i]^2

##########
## for
##########

for (i in 1:10) {print(i)}

for (i in c(3,2,9,6)) print(i^2)

medios.transporte <- c('carro', 'camion', 'metro', 'moto')
for (vehiculo in medios.transporte) print(vehiculo)

for (x in 1:4) {x}
for (x in 1:4) {print(x)}


s <- 0
for (i in 1:1000)
{
	x <- runif(1,-1,1)
	y <- runif(1,-1,1)
	if(x^2+y^2 < 1) s <- s+1
}
piest <- 4*s/1000
piest
error = abs(pi - piest)
error
 
#########
## while
#########

n <- 0
suma <- 0
while( suma <= 1000)
{
	n <- n+1
	suma <- suma + n
}
suma
n



ss <- 0; nn <- 0; error <- 1  
while (error > .001)
{
	nn <- nn+1
	x <- runif(1,-1,1)
	y <- runif(1,-1,1)
	if(x^2+y^2 < 1) ss <- ss+1
	piest <- 4*ss/nn
	error = abs(pi - piest)
}
piest
error
nn


###########
## repeat
###########

n <- 0
suma <- 0
repeat
{
	n <- n+1
	suma <- suma + n
	if(suma > 1000) break
}
suma
n 


ss <- 0; nn <- 0; error <- 1  
repeat
{
	nn <- nn+1
	x <- runif(1,-1,1)
	y <- runif(1,-1,1)
	if(x^2+y^2 < 1) ss <- ss+1
	piest <- 4*ss/nn
	error = abs(pi - piest)
	if  (error < .001) break
}
piest
error
nn

############
## Vecorizacion
############

n <- 1:1000
su <- cumsum(n)
su[su > 1000][1]
n[su > 1000][1]


x <- runif(1000,-1,1)
y <- runif(1000,-1,1)
z <- x^2+y^2
suma <- sum(as.numeric(z<1))
piest4 <- 4*suma/1000
piest4
error4 = abs(pi-piest4)
error4


cubo <- function (x)
{ return(x^3)}

cubo(2)

x <- 1:5
cubo(x/2)

cubo(cubo(x))

x <- matrix(1:4,2,2)
x
cubo(x)

divide <- function(x,y) {return(x/y)}
divide(8,3)
a <- 1:5
b <- 11:15
divide(b,a)
divide(11:15,3)

###############
## Ambito
###############

z <- ”prueba”
f <- function (x, y) {z <- x/y; return(z)}
a <- 3
b <- 4
z

f(a,b)
z


########################
## Valores automaticos
########################

ff <- function(x=1:10, y=x^2, showgraph=T)
{
if( showgraph) plot(x,y)
else print (cbind(x,y))
return(invisible())
}

ff()

ff(1:10, (1:10)^2, T)
ff(1:10, (1:10)^2)
ff(1:10)
ff(y=(1:10)^2)
ff(showgraph = T)
ff()

ff(11:20)

f1 <- function (x=1:10, y=x^2,
error=(length(x)!= length(y)))
{
if (error) return (”longitudes de x e y no
son iguales.”)
else return (cbind (x,y))
}
f1(2:8)
f1(x=1:5, y=1:9)



x <- runif(10000,-1,1)
t <- Sys.time()
k <- 0
for (a in x) {
  if(a>0) y <- a
  else y <- 0
k <- k+y
}
Sys.time() - t

t <- Sys.time()
k <- sum(x[x>0])
Sys.time() - t

fib1 <- function(n)
	{
	res <- c(0,1)
	for (i in 3:n)
	  res[i] <- res[i-1] + res[i-2]
	return(res)
	}
fib1(10)


fib2 <- function(n)
{
res <- numeric(n)
res[2] <- 1
for (i in 3:n)
res[i] <- res[i-1] + res[i-2]
return(res)
}

fib2(10)

system.time(fib1(10000))

system.time(fib2(10000))

system.time(fib1(10000))/system.time(fib2(10000))


fib3 <- function(nb)
	{
	x <- 0
	x1 0
	x2 <- 1
	while (n>0))
	x <- x1 + x2
	x2 <- x1
	x1 <- x
	n <- n-1
	}

####################
## Instrucciones Condicionales
####################


x <- 1:3
x > 2
if(x > 2) print('a') else print('b')



mg <- function(v)
	{
	exp(sum(log(v))/length(v))
	}
mg(1:20)


mg(c(-2,-1,1,2))

mg <- function(v)
	{
	if(any(v <= 0))
		{
		print('Todos los numeros deben ser positivos'); return(NULL)
		}
	else return(exp(sum(log(v))/length(v)))
	}
mg(c(-2,-1,1,2))

(c <- ifelse(x > 2, 'a', 'b'))

uu <- runif(100)
norm1 <- rnorm(100,mean=-2, sd=sqrt(2))
norm2 <- rnorm(100,mean=1)
vv <- ifelse(uu < 0.4, norm1,norm2)


y <- -1:3
aa <- ifelse(y <= 0, 0, y*log(y))
aa


zz <- runif(100,-1,2)
ab <- ifelse(zz<0,0,ifelse(zz<1,-log(zz),log(zz)))


mg <- function(v)
	{
	if(!is.numeric(v))
		{
		print('Argumento debe ser numerico'); return(NULL)
		}
	if(any(v <= 0))
		{
		print('Todos los numeros deben ser positivos'); return(NULL)
		}
	else return(exp(sum(log(v))/length(v)))
	}
mg(c(-2,-1,1,2))
mg(letters)



centrar <- function(x,type)
{
switch(type, media=mean(x),
mediana = median(x),
recortada = mean(x,trim=.1))
}
(x <- rcauchy(10))
centrar(x,'media')
centrar(x,'mediana')
centrar(x,'recortada')
centrar(x,'algo')


centrar <- function(x,type)
{
switch(type, media=mean(x),
mediana = median(x),
recortada = mean(x,trim=.1),
mean(x))
}

centrar(x,'')
centrar(x,'algo')
centrar(x)


mtr <- rnorm(50)
mean(mtr)
median(mtr)
switch(2, mean(mtr), median(mtr))


plot.f <- function(f,a,b,...)
	{
	xvals <- seq(a,b,length=100)
	plot(xvals,f(xvals),type='l',...)
	}
plot.f(sin,0,2*pi)
plot.f(sin,0,2*pi,lty=4)
plot.f(abs,-1,1,lty=4)


centrar <- function(x,type=mean,...)
{
switch(type, media=mean(x,...),
mediana = median(x),
mean(x,...))
}

centrar(x,”)

centrar(x,’mean’,trim=0.2)


x <- 1:5
y <- 2:4
f <- function(x,y) x^2*y + x*y^2
outer(x,y,f)

outer(x,y,function(x,y) x^2*y + x*y^2)

###########################
## La Funcion mapply
###########################

mapply(rep, 1:4, 4:1)
mapply( seq, 1:6, 6:8)
aa <- lapply(rep(12,4), sample, x=1:100)
mapply(mean, aa, 0:3/10)


############################
## Replicate
############################

replicate(10, sample(1:100, 15))

ff <- function(n, mean, sd)
mean(rnorm(m, mean=mean, sd=sd))
res <- replicate(10000, ff(100, 0, 1))
hist(res)
mean(res)


###############
## cat
###############

x <- 2*(1:5)
cat(x)
cat('Hola')

y <- 7
cat('El valor de y es',y,'.')
cat('El valor de y es',y,'\b.')

cat('Esta es una prueba.\nSegunda linea')


texto <- c()
repeat {
	cat('Introduzca una frase (frase vacia termina)')
	fr <- readLines(n=1)
	if (fr=='') break else texto <- c(texto,fr)
	}




pos <- numeric(0)
repeat {
	cat('Introduzca un numero positivo (cero termina)')
	num <- scan(n=1)
	if (num < 0) next
	if (num == 0) break
	pos <- c(pos, num)
	}























