This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this: ##HECHO EN CLASE ## Primeros comandos en R
R es muy parecido a matlab y permite el trabajo con matrices
A<-38
A<-40
B<-60
C<-B-A
C
## [1] 20
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
Usando el comando data en la consola obtenemos distintos datos ya cargados en la base de datos de R, estos datos estan dado en la tabla de las cuales podemos elegir columnas específicas si escribimos $ despues de el comando de informacion especifico
You can also embed plots, for example:
z1<-rnorm(350,22,5)
z1
## [1] 35.019995 19.548283 25.518039 15.773656 24.229504 18.603951 20.491275
## [8] 22.027387 17.775094 31.831835 28.968834 17.578191 24.901997 28.775902
## [15] 23.976413 23.766074 28.567213 18.074859 22.909458 15.586392 19.624284
## [22] 21.868978 21.936402 17.078832 26.371366 20.429088 15.349707 14.038356
## [29] 24.544702 16.365264 18.071336 35.503135 16.528530 22.917645 25.225555
## [36] 22.732671 27.579961 23.518638 22.155255 30.395423 25.859963 19.950350
## [43] 22.420822 26.878390 29.704831 17.719912 28.951856 19.174830 19.374467
## [50] 28.183578 18.517272 23.351107 25.625246 22.824063 23.557869 24.275645
## [57] 32.603417 20.567446 11.159154 24.028764 15.191334 21.965406 24.247805
## [64] 22.949118 25.857261 27.621055 23.862665 22.970946 16.359528 23.683850
## [71] 12.670582 26.432296 26.831497 24.894750 8.230613 23.133975 21.448640
## [78] 27.860527 18.237174 24.372772 21.404485 23.871875 7.371991 27.101319
## [85] 15.195262 22.890248 14.556404 26.724193 22.214417 17.083999 23.142245
## [92] 24.470594 22.717823 22.238887 23.996270 17.146956 18.070974 17.808131
## [99] 30.792775 18.842978 19.673030 20.116329 18.712242 20.566816 20.864789
## [106] 16.651970 29.352126 23.421491 18.846043 21.287403 25.263506 14.659268
## [113] 20.024046 23.206449 24.344383 26.637017 24.326116 10.765233 13.909377
## [120] 26.031551 24.365907 25.824210 20.578096 23.842382 23.753710 25.912907
## [127] 17.438486 21.974621 18.211483 25.553332 18.480360 24.656115 31.454585
## [134] 16.275913 20.725420 24.165252 21.579006 11.805333 17.466334 24.333966
## [141] 23.060284 25.345970 24.687643 16.123811 28.182065 19.726800 15.853971
## [148] 23.049972 13.924138 23.216658 12.549660 15.573995 17.748954 28.139272
## [155] 29.139646 22.191914 21.117685 14.975848 26.546240 21.215485 21.935682
## [162] 23.490556 16.861610 30.884744 19.004807 25.552547 27.773740 24.362072
## [169] 28.048515 28.526963 22.205068 8.957850 35.571801 32.875658 19.538468
## [176] 22.761396 27.183350 28.316496 22.346239 26.031801 15.629686 27.040959
## [183] 22.943827 24.785570 21.032571 18.319253 21.740923 28.228115 23.981301
## [190] 15.520310 12.875921 23.167313 18.561096 28.092167 18.785140 27.450983
## [197] 25.786961 27.600212 19.763536 26.424360 14.671993 19.459825 29.849834
## [204] 22.069717 19.702670 21.845723 24.049857 15.833876 9.008629 26.544674
## [211] 16.145836 21.748570 19.503749 28.187405 19.919116 14.712191 15.286538
## [218] 26.516832 21.640183 21.301275 15.952370 20.004374 15.866391 20.455667
## [225] 23.392495 34.674570 18.571731 13.876836 27.821988 19.580679 16.447460
## [232] 24.820024 16.606388 25.856392 18.683046 15.395871 13.746241 21.491459
## [239] 24.221545 14.837279 24.022629 21.558710 29.618352 20.831533 18.538611
## [246] 26.758730 29.836814 18.777041 20.376008 28.918948 22.241452 25.146401
## [253] 30.493388 25.206983 20.517184 24.114597 23.727072 28.920520 18.881125
## [260] 23.248886 13.713857 18.142066 18.311062 27.797974 19.269632 18.930113
## [267] 19.474190 22.863325 26.665954 21.937887 21.970772 26.057805 18.634967
## [274] 15.907831 16.967031 26.984285 24.089441 17.917269 30.469246 11.732712
## [281] 20.545430 11.322757 14.498236 28.688364 26.158581 27.721795 32.626044
## [288] 21.643347 18.759201 14.040708 24.376550 19.967188 17.453129 14.936107
## [295] 23.907231 15.968221 20.016339 21.338228 25.332449 18.505264 24.085073
## [302] 22.064955 21.931288 24.616264 24.369412 26.310067 24.901692 11.181817
## [309] 21.306025 21.725212 18.793703 28.712021 24.256558 19.351679 20.224142
## [316] 25.591673 26.736472 11.138213 29.268213 22.346957 17.597226 21.426565
## [323] 30.277144 23.739164 30.093185 15.277816 26.729433 17.732727 27.671861
## [330] 21.449202 26.486212 23.632971 20.910472 20.635783 20.815141 32.881123
## [337] 23.364563 21.142342 20.788970 29.888861 18.220355 23.637812 17.680144
## [344] 28.769272 27.096440 26.551055 22.750500 25.556296 22.528771 28.772057
plot(z1)
w1<-length(z1)
w1
## [1] 350
x1<-(500:849)
x1
## [1] 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
## [19] 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
## [37] 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
## [55] 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
## [73] 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
## [91] 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
## [109] 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
## [127] 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
## [145] 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
## [163] 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
## [181] 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
## [199] 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
## [217] 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
## [235] 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
## [253] 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
## [271] 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
## [289] 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
## [307] 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
## [325] 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
## [343] 842 843 844 845 846 847 848 849
plot(z1,x1)
hist(z1, main="Histograma de z1", col="lightblue")
density(z1)
##
## Call:
## density.default(x = z1)
##
## Data: z1 (350 obs.); Bandwidth 'bw' = 1.416
##
## x y
## Min. : 3.124 Min. :1.055e-05
## 1st Qu.:12.298 1st Qu.:3.371e-03
## Median :21.472 Median :1.492e-02
## Mean :21.472 Mean :2.720e-02
## 3rd Qu.:30.646 3rd Qu.:5.191e-02
## Max. :39.820 Max. :7.761e-02
plot(density(z1))
## Pedir lo de aca
Consigna: Las ultimas 3 cifras de mi DNI son 170, consigna crear una variable que tenga ese número
DNI=170
SECUENCIADNI<-(1:170)
SECUENCIADNI
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
## [19] 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
## [37] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
## [55] 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
## [73] 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
## [91] 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
## [109] 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
## [127] 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
## [145] 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
## [163] 163 164 165 166 167 168 169 170
TOTAL<-0
VALORFINAL<-length(SECUENCIADNI)
for (i in 1:VALORFINAL){
TOTAL<-TOTAL+i }
TOTAL
## [1] 14535
##Ejercicio 3 ##Repetir el ejercicio anterior pero en Python
suma=0
for i in range (1,171):
# range llega hasta uno antes del final, por eso 171
suma+=i # <--- Esta línea DEBE tener un espacio al principio
print(suma)
## 14535
##Ejercicio 4 ##Hacer lo mismo en Julia ## no funciona ## hacer las consignas del pdf en github del profe, en industrial
##HASTA ACÁ LLEGA LO HECHO EN CLASE
1.4 Generar un vector secuencia
# Método con for
A <- 0
start_for <- Sys.time()
for (i in 1:50000) { A[i] <- (i*2) }
end_for <- Sys.time()
tiempo_for <- end_for - start_for
# Método con seq() de R
start_seq <- Sys.time()
B <- seq(2, 100000, by = 2)
end_seq <- Sys.time()
tiempo_seq <- end_seq - start_seq
# Mostrar resultados
head(A)
## [1] 2 4 6 8 10 12
tail(A)
## [1] 99990 99992 99994 99996 99998 100000
head(B)
## [1] 2 4 6 8 10 12
tail(B)
## [1] 99990 99992 99994 99996 99998 100000
# Comparar tiempos
print(paste('Tiempo for:', tiempo_for))
## [1] "Tiempo for: 0.0148923397064209"
print(paste('Tiempo seq:', tiempo_seq))
## [1] "Tiempo seq: 0.00169658660888672"
1.5 Implementación de una serie Fibonachi o Fibonacci
fibonacci <- function(n) {
fib <- c(0, 1)
while (tail(fib, 1) < n) {
len <- length(fib)
fib <- c(fib, fib[len] + fib[len - 1])
}
return(fib)
}
# Generar hasta superar 1.000.000
resultado <- fibonacci(1000000)
print(resultado)
## [1] 0 1 1 2 3 5 8 13 21
## [10] 34 55 89 144 233 377 610 987 1597
## [19] 2584 4181 6765 10946 17711 28657 46368 75025 121393
## [28] 196418 317811 514229 832040 1346269
print(paste('Iteraciones necesarias:', length(resultado)))
## [1] "Iteraciones necesarias: 32"
print(paste('Ultimo numero mayor a 1.000.000:', tail(resultado, 1)))
## [1] "Ultimo numero mayor a 1.000.000: 1346269"
1.7 Ordenación de un vector por método burbuja
# Cargamos la librería para medir performance
library(microbenchmark)
# 1. Creamos la muestra de tamaño 20.000
x <- sample(1:100000, 20000)
# 2. Definimos la función de la burbuja
burbuja <- function(x) {
n <- length(x)
for (j in 1:(n - 1)) {
for (i in 1:(n - j)) {
if (x[i] > x[i + 1]) {
temp <- x[i]
x[i] <- x[i + 1]
x[i + 1] <- temp
}
}
}
return(x)
}
# 3. Comparamos la performance
comparacion <- microbenchmark(
Metodo_Burbuja = burbuja(x),
Metodo_Sort_R = sort(x),
times = 1
)
# 4. Mostramos el resultado
print(comparacion)
## Unit: milliseconds
## expr min lq mean median uq
## Metodo_Burbuja 21057.60840 21057.60840 21057.60840 21057.60840 21057.60840
## Metodo_Sort_R 1.10599 1.10599 1.10599 1.10599 1.10599
## max neval
## 21057.60840 1
## 1.10599 1
1.8 La penitencia de Newton
# Metodo 1: suma con for
metodo_for <- function() {
numeros <- 1:1e10
suma <- 0
for (i in numeros) {
suma <- suma + i
}
return(suma)
}
# Metodo 2: formula matematica de Gauss
metodo_formula <- function() {
n <- 1e10
suma <- n * (n + 1) / 2
return(suma)
}
# Medir tiempo metodo for (advertencia: puede tardar mucho)
# Para prueba rapida usar 1:1e6 en lugar de 1:1e10
tiempo_for <- system.time({
resultado_for <- metodo_for()
})
# Medir tiempo metodo formula
tiempo_formula <- system.time({
resultado_formula <- metodo_formula()
})
# Mostrar resultados
print(resultado_for)
## [1] 5e+19
print(resultado_formula)
## [1] 5e+19
# Mostrar tiempos
print(tiempo_for)
## user system elapsed
## 168.097 0.101 169.249
print(tiempo_formula)
## user system elapsed
## 0 0 0
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.