## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 1 1 2 3 4 5
## [1] 12 1 2 3 4 5
## [1] 12 1 2 3 4 5
## [1] 1
## [1] 2
## [1] 2
## [1] 4
## [1] 3
## [1] 6
## [1] 1
## [1] 4
## [1] 2
## [1] 8
## [1] 3
## [1] 12
## [1] 1
## [1] 2
## [1] 6
## [1] 24
## [1] 120
## [1] 720
## [1] 5040
## [1] 40320
## [1] 362880
## [1] 3628800
## [1] 3628800
## [1] NA NA NA NA NA
## [1] 2 4 6 8 10
## [1] 2 4 6 8 10
## j
## [1,] 10 100 50
## [,1]
## j 10
## 100
## 50
## A
## [1,] 1 1 2 3 4 5 6 7 8 9 10
## [2,] 1 1 4 9 16 25 36 49 64 81 100
## [3,] 1 5 10 15 20 25 30 35 40 45 50
## [,1] [,2] [,3]
## A 1 1 1
## 1 1 5
## 2 4 10
## 3 9 15
## 4 16 20
## 5 25 25
## 6 36 30
## 7 49 35
## 8 64 40
## 9 81 45
## 10 100 50
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 2 4 6
## [3,] 3 6 9
## [4,] 4 8 12
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 1 2 3
## [3,] 1 2 3
## [4,] 1 2 3
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 2 4 6
## [3,] 3 6 9
## [4,] 4 8 12
## [1] 15
## [1] 20
## [1] 25
## [1] 30
## [1] 30
## [1] 9
## [1] 13
## [1] 17
## [1] 21
## [1] 21
## [1] 5 9
## [1] 5 9 13
## [1] 5 9 13 17
## [1] 5 9 13 17 21
## [1] 21
## [1] 5 9 13 17 21
## [1] -0.9619334
## [1] -0.2925257
## [1] 0.2587882
## [1] 0.2587882
## [1] -0.9619334
## [1] -0.2925257
## [1] -0.2925257
## [,1] [,2] [,3] [,4]
## [1,] 1 4 9 16
## [2,] 2 8 18 32
## [3,] 3 12 27 48
## [4,] 4 16 36 64
## [1] 1 1 2 3 5 8 13 21 34 55 89 144
Fibonacci Sequence
The next number is found by adding up the two numbers before it.
y0 <- 5
y0_trace <- y0
for(i in 1:6){
y0 <- -0.5 * (y0-3) + y0
y0_trace <- c(y0_trace, y0)
}
y0_trace## [1] 5.00000 4.00000 3.50000 3.25000 3.12500 3.06250 3.03125
## [1] 3.03125
We have already seen and used several functions, but you can also create your own! This is incredibly useful when:
function_name: the name you want to give your function,
what you will use to call itfunction(): call this to define a functionparam1, param2: function parameters, what
the user inputs. You can assign default values by setting them equal to
something in the function definitionreturn(): is what your function will return to the
userfahr_to_kelvin <- function(fahr) {
# function that converts temperature in degrees Fahrenheit to kelvin
# input: fahr: numeric value representing temp in degrees farh
# output: kelvin: numeric converted temp in kelvin
kelvin <- ((fahr - 32) * (5 / 9)) + 273.15
return(kelvin)
}
fahr_to_kelvin(32)## [1] 273.15
fahr_to_kelvin <- function(fahr) {
# function that converts temperature in degrees Fahrenheit to kelvin
# input: fahr: numeric value representing temp in degrees farh
# output: kelvin: numeric converted temp in kelvin
kelvin <- ((fahr - 32) * (5 / 9)) + 273.15
return(kelvin)
}
fahr_to_kelvin(212)## [1] 373.15
## [1] 18
## [1] 102
## [1] 3 6 11 18 27
We can create user-defined functions in R. They are specific to what a user wants and once created they can be used like the built-in functions.
Below is an example of how a function is created and used.
## Create a function to print squares of numbers in sequence.
new.function <- function(a) {
for(i in 1:a) {
b <- i^2
print(b)
}
}
new.function(3)## [1] 1
## [1] 4
## [1] 9
## Create a function to print squares of numbers in sequence.
new.function <- function(a) {
for(i in 1:a) {
b <- i^2
print(b)
}
}
new.function(6)## [1] 1
## [1] 4
## [1] 9
## [1] 16
## [1] 25
## [1] 36
# Create a function without an argument.
new.function <- function() {
for(i in 1:5) {
print(i^2)
}
}
# Call the function without supplying an argument.
new.function()## [1] 1
## [1] 4
## [1] 9
## [1] 16
## [1] 25
# Create a function with arguments.
new.function <- function(a,b,c) {
result <- a * b + c
print(result)
}
# Call the function by position of arguments.
new.function(5,3,11)## [1] 26
## [1] 58
quadraticRoots <- function(a, b, c) {
print(paste0("You have chosen the quadratic equation ", a, "x^2 + ", b, "x + ", c, "."))
discriminant <- (b^2) - (4*a*c)
if(discriminant < 0) {
return(paste0("This quadratic equation has no real numbered roots."))
}
else if(discriminant > 0) {
x_int_plus <- (-b + sqrt(discriminant)) / (2*a)
x_int_neg <- (-b - sqrt(discriminant)) / (2*a)
return(paste0("The two x-intercepts for the quadratic equation are ",
format(round(x_int_plus, 5)), " and ",
format(round(x_int_neg, 5)), "."))
}
else #discriminant = 0 case
x_int <- (-b) / (2*a)
return(paste0("The quadratic equation has only one root. This root is ",
x_int))
}
quadraticRoots(1, 0, 5)## [1] "You have chosen the quadratic equation 1x^2 + 0x + 5."
## [1] "This quadratic equation has no real numbered roots."
quadraticRoots <- function(a, b, c) {
print(paste0("You have chosen the quadratic equation ", a, "x^2 + ", b, "x + ", c, "."))
discriminant <- (b^2) - (4*a*c)
if(discriminant < 0) {
return(paste0("This quadratic equation has no real numbered roots."))
}
else if(discriminant > 0) {
x_int_plus <- (-b + sqrt(discriminant)) / (2*a)
x_int_neg <- (-b - sqrt(discriminant)) / (2*a)
return(paste0("The two x-intercepts for the quadratic equation are ",
format(round(x_int_plus, 5)), " and ",
format(round(x_int_neg, 5)), "."))
}
else #discriminant = 0 case
x_int <- (-b) / (2*a)
return(paste0("The quadratic equation has only one root. This root is ",
x_int))
}
quadraticRoots(1, 7, 5)## [1] "You have chosen the quadratic equation 1x^2 + 7x + 5."
## [1] "The two x-intercepts for the quadratic equation are -0.80742 and -6.19258."
quadraticRoots <- function(a, b, c) {
print(paste0("You have chosen the quadratic equation ", a, "x^2 + ", b, "x + ", c, "."))
discriminant <- (b^2) - (4*a*c)
if(discriminant < 0) {
return(paste0("This quadratic equation has no real numbered roots."))
}
else if(discriminant > 0) {
x_int_plus <- (-b + sqrt(discriminant)) / (2*a)
x_int_neg <- (-b - sqrt(discriminant)) / (2*a)
return(paste0("The two x-intercepts for the quadratic equation are ",
format(round(x_int_plus, 5)), " and ",
format(round(x_int_neg, 5)), "."))
}
else #discriminant = 0 case
x_int <- (-b) / (2*a)
return(paste0("The quadratic equation has only one root. This root is ",
x_int))
}
quadraticRoots(2, 4, 2)## [1] "You have chosen the quadratic equation 2x^2 + 4x + 2."
## [1] "The quadratic equation has only one root. This root is -1"
quadraticRoots <- function(a, b, c = 1) {
print(paste0("You have chosen the quadratic equation ", a, "x^2 + ", b, "x + ", c, "."))
discriminant <- (b^2) - (4*a*c)
if(discriminant < 0) {
return(paste0("This quadratic equation has no real numbered roots."))
}
else if(discriminant > 0) {
x_int_plus <- (-b + sqrt(discriminant)) / (2*a)
x_int_neg <- (-b - sqrt(discriminant)) / (2*a)
return(paste0("The two x-intercepts for the quadratic equation are ",
format(round(x_int_plus, 5)), " and ",
format(round(x_int_neg, 5)), "."))
}
else #discriminant = 0 case
x_int <- (-b) / (2*a)
return(paste0("The quadratic equation has only one root. This root is ",
x_int))
}
quadraticRoots(2, 4, 2)## [1] "You have chosen the quadratic equation 2x^2 + 4x + 2."
## [1] "The quadratic equation has only one root. This root is -1"
quadraticRoots <- function(a, b, c = 1) {
print(paste0("You have chosen the quadratic equation ", a, "x^2 + ", b, "x + ", c, "."))
discriminant <- (b^2) - (4*a*c)
if(discriminant < 0) {
return(paste0("This quadratic equation has no real numbered roots."))
}
else if(discriminant > 0) {
x_int_plus <- (-b + sqrt(discriminant)) / (2*a)
x_int_neg <- (-b - sqrt(discriminant)) / (2*a)
return(paste0("The two x-intercepts for the quadratic equation are ",
format(round(x_int_plus, 5)), " and ",
format(round(x_int_neg, 5)), "."))
}
else #discriminant = 0 case
x_int <- (-b) / (2*a)
return(paste0("The quadratic equation has only one root. This root is ",
x_int))
}
quadraticRoots(2, 4)## [1] "You have chosen the quadratic equation 2x^2 + 4x + 1."
## [1] "The two x-intercepts for the quadratic equation are -0.29289 and -1.70711."