title: “Complementaria 1” author: “Andrea Velandia” date: ‘2023-02-02’ output: html_document

-``

Complementaria 1

a<-2
jose<-300

# Operacion matematica
b<-1550
c<-4 #Numero a multiplicar
d<-90
rta<-(b/c)*d

#Amo a modelitos


#Tipo de objetos
#Entero
f<-34

#Numericos
h<-98/12

#Carcateres
k<-"amiguito"

#Vectores en R y tipo de objetos

vectorA<-c(1,4,2,3,5,2)

vectorB<-c("hola","bad bunny","Amigo")

vectorC<-vector(mode="logical",length=7)

vectorD<-3:20

vectorF<-c(TRUE, FALSE, FALSE, TRUE)

#Llamar valores dentro de vector
vectorB[2]
## [1] "bad bunny"
vectorDiverso<-c(25.7,"buenos dias",TRUE)

#Verificar tipo de dato

class(vectorDiverso)
## [1] "character"
#Cambiar la clase de un objeto

vectorN<-c(0:5)
as.logical(vectorN)
## [1] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
as.numeric(vectorN)
## [1] 0 1 2 3 4 5
#Paquetes en R

#install.packages("markovchain")
library(markovchain)
## Package:  markovchain
## Version:  0.8.6
## Date:     2021-05-17
## BugReport: https://github.com/spedygiorgio/markovchain/issues
# Funciones ---------------------------------------------------------------

nombre<-function(parametros){
  cuerpo
  return(cuerpo)
}

#Funcion 1
funcioncita<-function(n){
  rta<-(n^(1/3))+10
  return(rta)
}

x<-27
funcioncita(n=x)
## [1] 13
#Funcion 2: retorna grafica

grafica<-function(vectorEjeY){
  ejeX<-seq(from=1,to=length(vectorEjeY),by=1)
  plot(x = ejeX,y=vectorEjeY,type="l",col="blue")
}

vectorNN<-c(1,4,2,6,7,3,2)
grafica(vectorNN)

# Help --------------------------------------------------------------------

#COn ? seguido de la funcion


# Recorridos --------------------------------------------------------------

for(i in 1:20){
  
}

#Crear funcion 
ranita<-function(va,vb){
  vecRta<-rep(0,times=length(va)) #Crear vector lleno de 0 del tamano de va
  
  #Recorrido por vecRta
  for (i in 1:length(vecRta)) {
    vecRta[i]<-max(va[i],vb[i]) #Asigno el max numero a cada posicion de vectRta
  }
  
  return(vecRta)
}

v1<-c(1,2,3,4,5,5)
v2<-c(9,1,3,2,6,2)

ranita(v1,v2) #Evaluando funcion
## [1] 9 2 3 4 6 5
# Estructuras de control-Condicionales ------------------------------------

#if(condicion){
 # ...
#}else{
 # ...
#}

#Probar condicional 1
x<-70

if(x<15){
  print("X es menor a 15")
} else{
  print("x no es menor a 15")
}
## [1] "x no es menor a 15"
#Probar condicional con Y

x<-4

if(x>=2 & x<=10){
  print("X esta entre 2 y 10")
} else if(x>15 & x<20){
  print("X esta entre 15 y 20")
}
## [1] "X esta entre 2 y 10"
# A prueba lo aprendido ---------------------------------------------------

#Ejercicio1

ejemplo1<-function(vector,numero){
  valor<-FALSE
  
  for(i in 1:length(vector)){
    if(vector[i]==numero){
      valor<-TRUE
    }
  }
  return(valor)
}

vector1<-c(1,2,3,2,3,6,7,8)
num<-3

ejemplo1(vector1,num)
## [1] TRUE
#Ejercicio 2

unico<-function(vectorcito){
  vectorRta<-c(vectorcito[1])
  
  for (i in vectorcito) {
    hayIgual<-FALSE
    for (j in vectorRta) {
      if(i==j){
        hayIgual<-TRUE
        break
      }
    }
    
    if(hayIgual==FALSE){
      vectorRta<-c(vectorRta,i)
    }
    
  }
  return(vectorRta) 
  
}
v1<-c(1,2,3,4,3,2,2)
unico(v1)
## [1] 1 2 3 4
# Ejercicio3

ejercicio2<-function(vector, numero){
  contador<-0
  
  for (i in 1:length(vector)) {
    if(numero==vector[i]){
      contador<-contador+1
    }
  }
  
  return(contador)
}
ejercicio2(v1,2)
## [1] 3