4.4 Estructuras de programación

c<-c(1,2,3,4,5,6,7,8,9)
c2<-c(1,2,3,4,3,2,1)

creciente<-function(vec)
{
  for(i in 2: length(vec))
    ifelse(vec[i]<vec[i-1],print("No es estricto creciente"), print("Es estricto creciente"))
}
montecarlo<-function(n)
{
  hits=0
  for(i in 1:n)
  {
    r1<-runif(1,0,1)
    r2<-runif(1,0,1)
    if(r2<r1^2)
      hits=hits+1
  }
   
  return(hits/n)
}

montecarlo(10000)
## [1] 0.3335
testlist<-list(c(10:1),c(1,5,8,3),c(1,9,10,6))
testlist
## [[1]]
##  [1] 10  9  8  7  6  5  4  3  2  1
## 
## [[2]]
## [1] 1 5 8 3
## 
## [[3]]
## [1]  1  9 10  6
lapply(testlist[1:3], sort)
## [[1]]
##  [1]  1  2  3  4  5  6  7  8  9 10
## 
## [[2]]
## [1] 1 3 5 8
## 
## [[3]]
## [1]  1  6  9 10
testmatrix<-matrix(1:25, ncol=5, nrow=5)


min<-c(0,0,0,0,0)
max<-c(0,0,0,0,0)

for(i in 1:5)
{
  for(j in 1:5)
  {
    if(testmatrix[i,j]%%2 == 0)
    {
      if(max[j]<testmatrix[i,j])
        max[j]<-testmatrix[i,j]
    }
    else
    {
      if(min[j]>-testmatrix[i,j])
        min[j]<-testmatrix[i,j]
    }
  }
}
min
## [1]  5  9 15 19 25
max
## [1]  4 10 14 20 24
input<-matrix(1:30, ncol=5, nrow = 5, byrow=TRUE)

input[apply(input, 2, '>',7 )]
##  [1] 11 16 21 12 17 22  8 13 18 23  9 14 19 24 10 15 20 25