Question 3

Define a function titled ratio that takes arguments \(x\) and \(y\) and returns their ratio, \(x/y\). Call ratio\(()\) with arguments 3 and 4. (Paste your code in the answer box).

ratio <- function(x, y) {
  x/y
}

ratio(3,4)
## [1] 0.75

Question 4.

Write a function that converts miles to kilometers. There are 1.609 kilometers in 1 mile. Name the function as miles_to_km. You can choose any name for your arguments and need to return the kilometers as the result. (Paste your code in the answer box).

miles_to_km <- function(M, KM = 1.609){
  KM*M
}

miles_to_km(1:100)
##   [1]   1.609   3.218   4.827   6.436   8.045   9.654  11.263  12.872  14.481
##  [10]  16.090  17.699  19.308  20.917  22.526  24.135  25.744  27.353  28.962
##  [19]  30.571  32.180  33.789  35.398  37.007  38.616  40.225  41.834  43.443
##  [28]  45.052  46.661  48.270  49.879  51.488  53.097  54.706  56.315  57.924
##  [37]  59.533  61.142  62.751  64.360  65.969  67.578  69.187  70.796  72.405
##  [46]  74.014  75.623  77.232  78.841  80.450  82.059  83.668  85.277  86.886
##  [55]  88.495  90.104  91.713  93.322  94.931  96.540  98.149  99.758 101.367
##  [64] 102.976 104.585 106.194 107.803 109.412 111.021 112.630 114.239 115.848
##  [73] 117.457 119.066 120.675 122.284 123.893 125.502 127.111 128.720 130.329
##  [82] 131.938 133.547 135.156 136.765 138.374 139.983 141.592 143.201 144.810
##  [91] 146.419 148.028 149.637 151.246 152.855 154.464 156.073 157.682 159.291
## [100] 160.900
#or

miles_to_km <- function(M, KM = 1.609){
  kilometers <- KM*M
  return(kilometers)
}

miles_to_km(1:100)
##   [1]   1.609   3.218   4.827   6.436   8.045   9.654  11.263  12.872  14.481
##  [10]  16.090  17.699  19.308  20.917  22.526  24.135  25.744  27.353  28.962
##  [19]  30.571  32.180  33.789  35.398  37.007  38.616  40.225  41.834  43.443
##  [28]  45.052  46.661  48.270  49.879  51.488  53.097  54.706  56.315  57.924
##  [37]  59.533  61.142  62.751  64.360  65.969  67.578  69.187  70.796  72.405
##  [46]  74.014  75.623  77.232  78.841  80.450  82.059  83.668  85.277  86.886
##  [55]  88.495  90.104  91.713  93.322  94.931  96.540  98.149  99.758 101.367
##  [64] 102.976 104.585 106.194 107.803 109.412 111.021 112.630 114.239 115.848
##  [73] 117.457 119.066 120.675 122.284 123.893 125.502 127.111 128.720 130.329
##  [82] 131.938 133.547 135.156 136.765 138.374 139.983 141.592 143.201 144.810
##  [91] 146.419 148.028 149.637 151.246 152.855 154.464 156.073 157.682 159.291
## [100] 160.900

you can change the M and KM

Question 5

Ideally we would like to have one function that is able to convert miles to kilometers as well as convert kilometers to miles \((kilometer = 1.609*mile)\). Create a new function named convert_distance. (Paste your code in the answer box).

#Mi  <- .621
#Km <- 1.609
  
convert_distance <- function(distance_to_convert, to_kilometers = TRUE) {
  Mi  <- .621
  Km <- 1.609
if(to_kilometers == TRUE)
  print(Km*distance_to_convert)
if(to_kilometers == FALSE)
  print(Mi*distance_to_convert)
}

convert_distance(100)
## [1] 160.9
convert_distance(100, FALSE)
## [1] 62.1

you can change the distance_to_convert also you can change the Mi and Km names and place them above the convert_distance code and it’ll work. The hash tags will show you were to place them.

Question 6

Create a function named f_to_k, that takes temp_F as the argument, which is the Fahrenheit temperature. The function will convert the Fahrenheit temperature to the Kelvin temperature by using the following formula:

     temp_K = (temp_F-32)*5/9 + 273.15   

Return the temp_K as the result. (Paste your code in the answer box).

f_to_k <- function(temp_F){
  (temp_F-32)*5/9 + 273.15
}

f_to_k(10)
## [1] 260.9278
#or

f_to_k2 <- function(temp_F){
  temp_K <- (temp_F-32)*5/9 + 273.15
  return(temp_K)
}

f_to_k(10)
## [1] 260.9278

don’t put in the f_to_k(10) as an answer it is just to test if the conversion works

Question 7

Create a new function named k_to_c, that takes temp_K as the argument, which is the Kelvin temperature. The function will convert the Kelvin temperature to the Celsius temperature by using the following formula:

temp_C = temp_K - 273.15

Return the temp_C as the result. (Paste your code in the answer box).

k_to_c <- function(temp_K){
  temp_C <- temp_K - 273.15
  return(temp_C)
}

k_to_c(10)
## [1] -263.15
#or

k_to_c2 <- function(temp_K){
  temp_K - 273.15
  
}

k_to_c(10)
## [1] -263.15

don’t put in the k_to_c(10) as an answer it is just to test if the conversion works

Question 8

Create a function named f_to_c to convert Fahrenheit to Celsius.Take temp_F as the argument. Use the two existing functions f_to_k() and k_to_c() that are created in the previous problems to get there.

First
pass a Fahrenheit temp (temp_F) to f_to_k() to get Kelvin temperature (Temp_K)

Then
pass that result to k_to_c() to get the Celsius temperature (temp_C).
Return temp_C as the result. Paste your code in the answer box.

Test the result of the following code:

f_to_c(32)
f_to_c(100)
f_to_c <- function(temp_F){
  kelvin <- (temp_F-32)*5/9 + 273.15
  celsius <- kelvin - 273.15
  return(celsius)
}

f_to_c(32)
## [1] 0
f_to_c(100)
## [1] 37.77778

you can change the names of kelvin and celsius