Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
robertopucp
GitHub Repository: robertopucp/1eco35_2022_2
Path: blob/main/Trabajo_grupal/WG3/Solucion/script_r.R
2835 views
1
################ WG3 R script ############################
2
## Curso: Laboratorio de R y Python ###########################
3
## @author: Roberto Mendoza
4
5
6
# Loop replacement a un vector
7
8
vector = sample(1:500, size = 100)
9
10
11
escalar <- function (x, min_x, max_x){ (x - min_x)/(max_x - min_x) }
12
13
14
lapply(vector, escalar, min_x = min(vector), max_x = max(vector))
15
16
17
sapply(vector, escalar, min_x = min(vector), max_x = max(vector) )
18
19
20
21
# Loop replacement a una matrix
22
23
elements <- sample(5000) # random number
24
25
M <- matrix(elements , nrow = 100, ncol = 50) # reshape matrix
26
27
28
escalar <- function(x){ (x-min(x))/(max(x) - min(x)) }
29
30
31
apply(M, 2, escalar) # 2 para que aplicase la función por columnas
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64