#' square funtion
#' @param x
#' @retrun the x squared
f <- function(x) {
return(x^2)
}
#' Linear Funtion
#' @param x value
#' @retrun 2x=5
g <- function(x){
return(2*x+5)
}
#test functions
f(2)
## [1] 4
f(-5)
## [1] 25
g(-5/2)
## [1] 0
g(0)
## [1] 5
#'comp. fun.
#'
#'@param x value
#'@retrun the f(g(x))
fog <- function(x){
return(f(g(x)))
}
#'comp. fun.
#'
#'@param x value
#'@retrun the g(f(x))
gof<- function(x){
return(g(f(x)))
}
#Test
fog(2)
## [1] 81
fog(-5)
## [1] 25
gof(0)
## [1] 5
gof(-5/2)
## [1] 17.5
#' Pythagoras fun.
#' @description gets the length of the hypotense
#' @param a value
#' @param b value
#' @retrun the a^2 + b^2
pythagoras <- function(a, b=a){
return(sqrt(a^2 + b^2))
}
# Test
pythagoras(3, 4)
## [1] 5
pythagoras(5)
## [1] 7.071068
#'@title area of a circle
#'@discription create a circle object with default 1
#'@param radius
#'@param pi
#'@retrun Pi *r^2
circle_area <- function(radius=1){
if (radius<0){
stop("radius cannot be negative")
}
return(pi*radius^2)
}
#Test
area()
## Error in area(): could not find function "area"
area(-2)
## Error in area(-2): could not find function "area"
area(3)
## Error in area(3): could not find function "area"
#'@title SA of cylinder
#'@discription Finds the SA of a cylinder
#'@param Height =h
#'@param radius = r
#'@retrun 2*pi*r*h+2*pi*r^2
cylinder_area <- function(radius=1, height=1){
if(radius<0 || height<0){
stop("radius or height cannot be negative")
}
sa_circle_area <- circle_area(radius)
return( 2*sa_circle_area+(2*pi*radius*height))
}
cylinder_area(2,3)
## [1] 62.83185
cylinder_area(-2,3)
## Error in cylinder_area(-2, 3): radius or height cannot be negative
cylinder_area(2,-3)
## Error in cylinder_area(2, -3): radius or height cannot be negative
#'@title area of cylinder
#'@discription Finds the area of a cylinder
#'@param Height =h
#'@param radius = r
#'@retrun 2*pi*r(r+h)
cylinder_area<- function(radius=1, height=1){
cylinderC_area <- circle_area(radius)
return(cylinderC_area+(2*pi*radius*height))
}
cylinder_area(3,10)
## [1] 216.7699
#'@title Miles to kilometers
#'@discription converts miles into kilometers
#'@param miles = x
#'@retrun miles*1.609
miles2kms <- function(x) {
return(x*1.609)
}
conversion_table <-
data.frame(miles = c(1:10, seq(20, 100, by = 10)),
kms =miles2kms( c(1:10, seq(20, 100, by = 10))
))
print(conversion_table)
## miles kms
## 1 1 1.609
## 2 2 3.218
## 3 3 4.827
## 4 4 6.436
## 5 5 8.045
## 6 6 9.654
## 7 7 11.263
## 8 8 12.872
## 9 9 14.481
## 10 10 16.090
## 11 20 32.180
## 12 30 48.270
## 13 40 64.360
## 14 50 80.450
## 15 60 96.540
## 16 70 112.630
## 17 80 128.720
## 18 90 144.810
## 19 100 160.900
#'@title Gallons to Liters,
#'@discription converts Gallons to Liters, and viceversa
#'@param miles = x
#'@retrun gallon*3.78541
gallon2liters <- function(gallons){
return(gallons*3.78541)
}
liters2gallons <- function(liters) {
return(liters / 3.78541)
}
liters_values <- c(1:10, seq(20, 100, by = 10))
gallons_values <- liters2gallons(liters_values)
conversion_table <- data.frame(liters = liters_values, gallons = gallons_values)
print(conversion_table)
## liters gallons
## 1 1 0.2641722
## 2 2 0.5283444
## 3 3 0.7925165
## 4 4 1.0566887
## 5 5 1.3208609
## 6 6 1.5850331
## 7 7 1.8492052
## 8 8 2.1133774
## 9 9 2.3775496
## 10 10 2.6417218
## 11 20 5.2834435
## 12 30 7.9251653
## 13 40 10.5668871
## 14 50 13.2086088
## 15 60 15.8503306
## 16 70 18.4920524
## 17 80 21.1337741
## 18 90 23.7754959
## 19 100 26.4172177
poly1 <- function(x) {
x^3
}
x <- seq(-4, 4, length.out = 20)
y <- poly1(x)
plot(x, y, type = 'l', lwd = 3, col = "#FB7215", las = 1)
abline(h = 0, v = 0, col = '#888888aa', lwd = 1.5)
title(main = expression(f(x) == x^3))

poly2 <- function(x) {
(x^2 - 1) * (x + 3)^3
}
x <- seq(-4, 4, length.out = 20)
y <- poly2(x)
plot(x, y, type = 'l', lwd = 3, col = "#FB7215", las = 1)
abline(h = 0, v = 0, col = '#888888aa', lwd = 1.5)
title(main = expression(f(x) == (x^2 - 1)(x + 3)^3))

poly3 <- function(x) {
(x^2 - 1) * (x^2 - 9)
}
x <- seq(-4, 4, length.out = 20)
y <- poly3(x)
plot(x, y, type = 'l', lwd = 3, col = "#FB7215", las = 1)
abline(h = 0, v = 0, col = '#888888aa', lwd = 1.5)
title(main = expression(f(x) == (x^2 - 1)(x^2 - 9)))

z <- 100 * pi
if (z < 0) {
z <- 0
} else if (z > 100) {
z <- 100
} else {
z <- z
}
print(z)
## [1] 100
#'@title True or false
#'@discription checks if a number is ture or false
#'@retrun ture if even false if odd na if it isnt a number
is_even <- function(x) {
if (!is.numeric(x)) {
return(NA)
} else if (x %% 2 == 0) {
return(TRUE)
} else {
return(FALSE)
}
}
#Test
is_even(10)
## [1] TRUE
is_even(33)
## [1] FALSE
is_even("a")
## [1] NA
#'@title Grader
#'@discription checks the letter grade of a number
#'@retrun A,B,C,D,F determining on the grading scale
grade <- function(score) {
if (score < 0 || score > 100) {
stop("score must be a number between 0 and 100")
}
grade <- switch(
TRUE,
score >= 90 ~ "A",
score >= 80 ~ "B",
score >= 70 ~ "C",
score >= 60 ~ "D",
score < 60 ~ "F"
)
return(grade)
}
#Test
grade(90)
## score >= 90 ~ "A"
## <environment: 0x625372e7ee20>
grade(89.999)
## score >= 90 ~ "A"
## <environment: 0x6253727093e8>
grade(70.000001)
## score >= 90 ~ "A"
## <environment: 0x625372964998>
grade(50)
## score >= 90 ~ "A"
## <environment: 0x6253729b3fa8>
grade(-10)
## Error in grade(-10): score must be a number between 0 and 100
#'@title miles to inches,
#'@discription converts miles to inches
#'@param inches = x
#'@retrun inches *63360
miles2inches <- function(x = 1) {
return(x * 63360)
}
#'@title miles to feet,
#'@discription converts miles to feet
#'@param feet = x
#'@retrun feet *5280
miles2feet <- function(x = 1) {
return(x * 5280)
}
#'@title miles to yards,
#'@discription converts miles to yards
#'@param feet = x
#'@retrun yards *1760
miles2yards <- function(x = 1) {
return(x * 1760)
}
#'@title miles to meters,
#'@discription converts miles to meters
#'@param meters = x
#'@retrun meters *0.00062137
miles2meters <- function(x = 1) {
return(x / 0.00062137)
}
#'@title miles to kms,
#'@discription converts miles to kms
#'@param kms = x
#'@retrun kms *0.62137
miles2kms <- function(x = 1) {
return(x / 0.62137)
}
#'@title convert
#'@discription converts everthing of the same var.
#'@retrun all values from x
convert <- function(x, to = "km") {
result <- switch(
to,
in. = miles2inches(x),
ft = miles2feet(x),
yd = miles2yards(x),
m = miles2meters(x),
km = miles2kms(x),
stop("Invalid unit provided")
)
return(result)
}
#Test
convert(3)
## [1] 4.828041