*JOHANNA AGUILAR
*JOSE BAÑO
*SAYRI MENDOZA
*JAIME PAREDES
suma <- function(x,y){
x+y
}
suma(2,3)
## [1] 5
potencia <- function(x, y) {
result <- x^y
paste(x,"elevado a la potencia de", y, "es", result)
}
potencia(2,3)
## [1] "2 elevado a la potencia de 3 es 8"
potencia2 <- function(x, y) {
result <- x^y
cat(x,"elevado a la",y,"es igual a
",result)
}
potencia2(2,3)
## 2 elevado a la 3 es igual a
## 8
absoluto<-function(x) {
if(x<0){
paste(x*(-1))
}else {
paste(x)
}
}
absoluto(-3)
## [1] "3"
ftrozos <- function(x) {
if (x < 5){0}
else
(x=10)
}
ftrozos(3)
## [1] 0
ftrozos(5)
## [1] 10
TMB<-function(Sexo,Altura,Peso,Edad) {
if(Sexo=="mujer")
{ 655 + (9.6 * Peso) + (1.8 * Altura) - (4.7 * Edad) }
else{ 66 + (13.7 * Peso) + (5 * Altura) - (6.8 * Edad) }
}
TMB("hombre",170,57,32)
## [1] 1479.3
TMB("mujer",150,50,25)
## [1] 1287.5
hipotenusa<-function(cateto1, cateto2) {
sqrt(cateto1^2+cateto2^2)
}
hipotenusa(2,4)
## [1] 4.472136
hipotenusa<-function(cateto1, cateto2){
h<-sqrt(cateto1^2+cateto2^2)
list(cateto1=cateto1,cateto2=cateto2,hipotenusa=h)
}
hipotenusa(2,4)
## $cateto1
## [1] 2
##
## $cateto2
## [1] 4
##
## $hipotenusa
## [1] 4.472136
hipotenusa<- function(cateto1, cateto2)
{
h<-sqrt(cateto1^2+cateto2^2)
list(cateto1=cateto1,cateto2=cateto2,hipotenusa=h)
}
hipotenusa(2:4,4:6)
## $cateto1
## [1] 2 3 4
##
## $cateto2
## [1] 4 5 6
##
## $hipotenusa
## [1] 4.472136 5.830952 7.211103
hipotenusa2<-function(cateto1, cateto2) {
h2<-sqrt(cateto1^2+cateto2^2)
data.frame(variable=c("cateto","cateto","hipotenusa"),valor=c(cateto1,cateto2,h2))
}
hipotenusa2(2,4)
## variable valor
## 1 cateto 2.000000
## 2 cateto 4.000000
## 3 hipotenusa 4.472136
f<-function(x,y) {
return("y debe ser numerico")
x+y
}
f(2,"hola")
## [1] "y debe ser numerico"