"Hola mundo desde R"
# R es como una calculadora
# Suma
5 + 5
# Mult
3 * 3
# Division
20 / 5
# Potencia
3 ** 2
# Residuo (%%)
21 %% 5
# Cadenas de texto
"Hola mundo"
# Varibles
# =: asignacion
x = 5
x
# <-: En R: significa tambien asignacion
y <- 5
y
"Hola" * 2
Error in "Hola" * 2: non-numeric argument to binary operator Traceback:
# Operadores de comparacion
# distinto de
0 != 1
# igual a
0 == 1
# menor o igual
10 <= 20
# Booleanos
# En Python: True, False
T
F
TRUE
FALSE
# Operadores logicos
# AND---->&
T & T
# OR: |
F | F
# not: !
!F
En Python input()
: solicita información al usuario. float("5")-->5
En Rreadline()
: solicita información al usuario. as.numeric("5")--->5
edad <- as.numeric(readline(prompt="Ingrese su edad: "))
edad * 100
"bienvenido a la calculadora de áreas de círculos"
r <- as.numeric(readline(prompt="Ingrese el valor del radio"))
# Calculo del area
area <- pi * r ^ 2
area
Python
:¶cal = 8.5
if cal>=0 and cal<=10:
# condicion1
if cal<6:
# Accion1:
print("Reprobado")
# condicion2
elif cal>=6 and cal<8:
# accion2
print("Bien")
# condicion3
elif cal>=8 and cal<9.5:
# accion2
print("Muy bien")
else:
print("Excelente")
else:
print("La calificación debe ser mayor o igual a cero y menor o igual a 10")
cal <- as.numeric(readline(prompt="Ingrese calificación: "))
if (cal >= 0 & cal <= 10){
if (cal < 6){
"Reprobado"
# En vez de escribir elif vamos a escribir else if
} else if (cal >= 6 & cal < 8){
"Bien"
} else if (cal >= 8 & cal < 9.5){
"Muy bien"
} else {
"Excelente"
}
} else {
"Calificación errónea"
}
Estructura dle if en Python:
if <condicion>:
# accion 1
else:
# accion 2
Estructura básica en R:
if (<condicion>){
# accion 1
} else{
# accion 2
}
if (condicion){
# accion1
} else {
# accion 2
}
for (recorrido) {
# accion a repetir
}
# variable auxiliar del bucle: i
# c(): define vectores o rangos de numeros
# c(1,2,3)
# Primera vuelta: i=1 ---> print("Hola")
# Segunda vuelta: i=2 ---> print("Hola")
# Tercer vuelta: i=3 ---> print("Hola")
for (i in c(1,2,3, 40)){
# Accion a repetir
print("hola")
}
[1] "hola" [1] "hola" [1] "hola" [1] "hola"
for (i in c(10,100)){
# Accion a repetir
print("hola")
}
[1] "hola" [1] "hola"
for (i in c(1,2,3)){
# Accion a repetir
print(i ** 2)
}
[1] 1 [1] 4 [1] 9
c(1:100)
# forma 1:
vect1 <- c(1,10,5, 90)
vect2 <- c(-10: 10)
vect1
vect2
# forma 2:
-10:10
# Forma 3
seq(1, 20)
1:20
c(1:20)
seq(1,20, 0.5)
for (i in c(1:100)){
# Accion a repetir
print(i ** 2)
}
[1] 1 [1] 4 [1] 9 [1] 16 [1] 25 [1] 36 [1] 49 [1] 64 [1] 81 [1] 100 [1] 121 [1] 144 [1] 169 [1] 196 [1] 225 [1] 256 [1] 289 [1] 324 [1] 361 [1] 400 [1] 441 [1] 484 [1] 529 [1] 576 [1] 625 [1] 676 [1] 729 [1] 784 [1] 841 [1] 900 [1] 961 [1] 1024 [1] 1089 [1] 1156 [1] 1225 [1] 1296 [1] 1369 [1] 1444 [1] 1521 [1] 1600 [1] 1681 [1] 1764 [1] 1849 [1] 1936 [1] 2025 [1] 2116 [1] 2209 [1] 2304 [1] 2401 [1] 2500 [1] 2601 [1] 2704 [1] 2809 [1] 2916 [1] 3025 [1] 3136 [1] 3249 [1] 3364 [1] 3481 [1] 3600 [1] 3721 [1] 3844 [1] 3969 [1] 4096 [1] 4225 [1] 4356 [1] 4489 [1] 4624 [1] 4761 [1] 4900 [1] 5041 [1] 5184 [1] 5329 [1] 5476 [1] 5625 [1] 5776 [1] 5929 [1] 6084 [1] 6241 [1] 6400 [1] 6561 [1] 6724 [1] 6889 [1] 7056 [1] 7225 [1] 7396 [1] 7569 [1] 7744 [1] 7921 [1] 8100 [1] 8281 [1] 8464 [1] 8649 [1] 8836 [1] 9025 [1] 9216 [1] 9409 [1] 9604 [1] 9801 [1] 10000
for (i in seq(1,2, 0.1)){
# Accion a repetir
print(i ** 2)
}
[1] 1 [1] 1.21 [1] 1.44 [1] 1.69 [1] 1.96 [1] 2.25 [1] 2.56 [1] 2.89 [1] 3.24 [1] 3.61 [1] 4
v1 <- c(1,3,5)
v2 <- c(2,4,6)
v1 * v2
v1 + v2
v1 ** v2
v1 %% v2
length(v10)
v10 <- seq(1, 100, 0.5)
v10
# promedio de las entradas del vector
mean(v10)
v11 <- c(3,1,0)
# orden ascendente
sort(v11)
# Orden descendente
sort(v11, decreasing = TRUE)
length(v11)
# Contador
n <- 0
n
n <- 1
n
n <- 2
n
k <- 0
# 0 + 1
# 1
# k <- 1
k <- k + 1
# 1 + 1
# k <- 2
k <- k + 1
k
while (condicion){
# accion a repetir
}
count <- 0
while (count < 10){
print("hola")
count <- count + 1
}
"Por fin el bucle terminó"
[1] "hola" [1] "hola" [1] "hola" [1] "hola" [1] "hola" [1] "hola" [1] "hola" [1] "hola" [1] "hola" [1] "hola"
1:10
c(1:10)
seq(1, 10, 0.5)