-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathObjetos_Teoria.R
184 lines (123 loc) · 2.68 KB
/
Objetos_Teoria.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
x <- c(1,2,3,4,5,6,7L)
print(x)
dim(x)
y <- x
is.vector(y)
class(y)
typeof(y)
# Para crear una matriz podemos usar la siguiente función.
Matrix <- matrix(c(1,2,3,4,5,6,7,10,20,30,40,50,60,70), nrow = 7, ncol = 2, byrow = FALSE)
Matrix
Matrix[3, ]
class(Matrix[,2])
Calculo <- c(20, 40, 60, 50)/10
DF <-
data.frame(Year = c(20, 40, 60, 50),
Name = c("Pedro", "María", "Tomás", "Nieves"),
Month = Calculo)
print(DF)
class(DF)
class(DF[1])
DF2 <- tibble::tibble(
Year = c(20, 40, 60, 50),
Name = c("Pedro", "María", "Tomás", "Nieves"),
Month = Year / 10
)
DF2
class(DF2[1])
class(DF2)
DF2[1]
DF2$Name
DF$Year[DF$Name == "Pedro"]
Lista <- list(
Vec_Num = x,
Vec_Char = as.character(y),
Mat = Matrix,
DataFrame = DF
)
Lista$DataFrame$Name[3]
data(mtcars)
mtcars
RegLi <- lm(mpg ~ disp + cyl + wt, data = mtcars)
summary(RegLi)
x <- c(1,2,3)
x2 <- c(1,2,3, 4)
y <- sin(x)
mean(x, na.rm = TRUE)
x2 + y
presion_arterial <- 100
if (presion_arterial > 140) {
categoria <- "Alta"
} else {
categoria <- "Normal"
}
print(categoria)
presion_sistolica <- c(120, 130, 110, 140, 135, 150)
edad_grupo <- c("Joven", "Adulto", "Joven", "Adulto", "Adulto", "Adulto")
media_presion <- tapply(presion_sistolica, edad_grupo, mean)
print(media_presion)
presion <- 280
objetivo <- 120
dias <- 0
while (presion > objetivo) {
presion <- presion - 5
dias <- dias + 1
}
dias
print(dias)
alturas <- c(1.70, 1.75, 1.60, 1.75)
pesos <- c(65, 75, 50, 83)
imc <- numeric(length(alturas))
for (i in 1:3) {
imc[i] <- pesos[i] / alturas[i]^2
}
for (i in 1:length(alturas)) {
imc[i] <- pesos[i] / alturas[i]^2
}
print(imc)
<<<<<<< HEAD
=======
# FUNCIONES ---------------------------------------------------------------
sumar <- function(x, y){
x + y
}
# llamar a la funcion (desde entorno de trabajo)
sumar
# ejecutar
sumar(x = 5, y = 5)
sumar(x = c(1,2,3,4), y = 5)
# Definiendo un tipo de operación
calcular <- function(x, y, type) {
if (type == "sumar") {
x + y
} else if (type == "restar") {
x - y
} else if (type == "multiplicar") {
x * y
} else if (type == "dividir") {
x / y
} else {
stop("Tipo de operación desconocida")
}
}
calcular
calcular(x = 23, y = 14, type = "ecualizar")
# Argumentos por defecto
calcular <- function(x, y, type = "sumar") {
if (type == "sumar") {
x + y
} else if (type == "restar") {
x - y
} else if (type == "multiplicar") {
x * y
} else if (type == "dividir") {
x / y
} else {
stop("Tipo de operación desconocida")
}
}
calcular(7,3)
# Importando/argando una función
source("calcular.R")
calcular(7,3)
>>>>>>> bdf1daf3d6a79177dcd12e61bd32629b662729c2