set.seed(2020)
v1 <- c(rnorm(20))
View(v1)
v1[10]
## [1] 0.1173668
v1[5:8]
## [1] -2.7965343  0.7205735  0.9391210 -0.2293777
v1[1]
## [1] 0.3769721
v1[0]
## numeric(0)
which.max(v1)
## [1] 16
v1[16]
## [1] 1.800043
v1[which.max(v1)]
## [1] 1.800043
v1[which.min(v1)]
## [1] -3.038765
v1[10:length(v1)]
##  [1]  0.11736679 -0.85312282  0.90925918  1.19637296 -0.37158390
##  [6] -0.12326023  1.80004312  1.70399588 -3.03876461 -2.28897495
## [11]  0.05830349
head(v1, 5)
## [1]  0.3769721  0.3015484 -1.0980232 -1.1304059 -2.7965343
tail(v1, 5)
## [1]  1.80004312  1.70399588 -3.03876461 -2.28897495  0.05830349
v2 <- c(runif(20))
df1 <- data.frame(v1, v2)
dim(df1)
## [1] 20  2
v3 <- c(v1 + v2)
df2 <- data.frame(df1, v3)
View(head(df2))
df2[10, 2]
## [1] 0.1657461
df2[10, ]
##           v1        v2        v3
## 10 0.1173668 0.1657461 0.2831129
df2[, 2]
##  [1] 0.985161147 0.225329885 0.863937630 0.884683241 0.624841087
##  [6] 0.571553317 0.470844345 0.700667308 0.797935221 0.165746093
## [11] 0.578771094 0.288274719 0.902829078 0.521413869 0.825548264
## [16] 0.002148651 0.441395052 0.224316886 0.543966609 0.485812605
df2[5:10, 1:2]
##            v1        v2
## 5  -2.7965343 0.6248411
## 6   0.7205735 0.5715533
## 7   0.9391210 0.4708443
## 8  -0.2293777 0.7006673
## 9   1.7591313 0.7979352
## 10  0.1173668 0.1657461
options(digits = 2)
df1
##        v1     v2
## 1   0.377 0.9852
## 2   0.302 0.2253
## 3  -1.098 0.8639
## 4  -1.130 0.8847
## 5  -2.797 0.6248
## 6   0.721 0.5716
## 7   0.939 0.4708
## 8  -0.229 0.7007
## 9   1.759 0.7979
## 10  0.117 0.1657
## 11 -0.853 0.5788
## 12  0.909 0.2883
## 13  1.196 0.9028
## 14 -0.372 0.5214
## 15 -0.123 0.8255
## 16  1.800 0.0021
## 17  1.704 0.4414
## 18 -3.039 0.2243
## 19 -2.289 0.5440
## 20  0.058 0.4858
v4 <- c(4.5,8.5,9.7,3.1)
v5 <- c(seq(1:4))
cb <- cbind(v4,v5); cb
##       v4 v5
## [1,] 4.5  1
## [2,] 8.5  2
## [3,] 9.7  3
## [4,] 3.1  4
class(cb)
## [1] "matrix"
rb <- rbind(v4, v5);rb
##    [,1] [,2] [,3] [,4]
## v4  4.5  8.5  9.7  3.1
## v5  1.0  2.0  3.0  4.0
class(rb)
## [1] "matrix"
dim(cb)
## [1] 4 2
dim(rb)
## [1] 2 4
cb[1, 2] = 0
cb[3, 1] = NA
##### Resolver #####
# 2x + 3y = -1
# -4x - y = -3
curve(x^3 - 3*x, -2, 2)#Ejemplo#

curve((-1 -2*x)/3, -5, 5, 
      col = "red", lwd = 2,
      lty = 3)
curve(-4*x + 3, add = T, lty = 1,
      lwd = 3, col = "blue")
points(1, -1, cex = 1.5, pch = 19)
text(0, -2, "Solución")
arrows(0, -1.9, 1, -1)
grid(10, 10, col = "black")
#### Solucion analitica ####
A <- matrix(c(2, 3, -4, -1), 
            nrow = 2, byrow = T)
## Inversa de A ##
iA <- solve(A)
vs <- c(-1, -3)
sol <- c(iA%*%vs)
list(x = sol[1], y = sol[2])
## $x
## [1] 1
## 
## $y
## [1] -1
text(2, -1, paste(
  "(", sol[1], ",", sol[2], 
  ")"))

################## Creando funciones 
## Funcion para pasar grados centigrados a grados farenheit
mi_funcion=function(tempc){
  g_f=(9/5)*tempc+32
  return(g_f)}

mi_funcion(34)
## [1] 93
####### funcion para resolver ecuacion de segundo grado
f2<-function(a,b,c){
  x1=(-b+sqrt(b^2-4*a*c))/(2*a)
  x2=(-b-sqrt(b^2-4*a*c))/(2*a) 
  return(list(x1=x1,x2=x2))
}
f2(3,4,-2)
## $x1
## [1] 0.39
## 
## $x2
## [1] -1.7
f2(3,4,2)
## Warning in sqrt(b^2 - 4 * a * c): Se han producido NaNs

## Warning in sqrt(b^2 - 4 * a * c): Se han producido NaNs
## $x1
## [1] NaN
## 
## $x2
## [1] NaN
####### Validar el discriminante
f2<-function(a,b,c){
  disc=b^2-4*a*c
  if(disc>=0) {
    x1=(-b+sqrt(disc))/(2*a)
    x2=(-b-sqrt(disc))/(2*a) 
    return(list(x1=x1,x2=x2))
  }
  else{
    message("Solucion compleja")
  }
}
f2(3,4,2)
## Solucion compleja
######### Validar cuando a = 0
f2<-function(a,b,c){
  if(a!=0){
    disc=b^2-4*a*c
    if(disc>=0)    {
      x1=(-b+sqrt(disc))/(2*a)
      x2=(-b-sqrt(disc))/(2*a) 
      return(list(x1=x1,x2=x2))
    }
    else{message("Solucion compleja")
    }
  }
  else{x=-c/b
  return(x)
  }
}
f2(0,4,2)
## [1] -0.5
f2(3,4,2)
## Solucion compleja
##############################
#simulacion
# obtener area estimada de un circulo
n=10000
x=runif(n,-0.5,0.5)
y=runif(n,-0.5,0.5)
plot(x,y,xlim=c(-0.5,0.5),ylim=c(-0.5,0.5),pch=19,cex=0.1)

plot(x,y,xlim=c(-0.5,0.5),ylim=c(-0.5,0.5),pch=19,
     col=ifelse(x**2+y**2<0.25,"darkgreen","darkred"),cex=0.1)
grid(10,10)
points(0,0,col="red",pch=16,cex=1.5)

curve(sqrt(0.25-x**2),add=T,col="red")
curve(-sqrt(0.25-x**2),add=T,col="red")

puntos=table(x**2+y**2<0.25)
(porcentaje=puntos[2]/n)
## TRUE 
## 0.79
pi/4
## [1] 0.79