Title: Report for R-Function Cook
Author: Jean-Claude Walser
Date: 12.10.22
This is an example report for the R function assignment. I am using something from the lecture and explore possible improvements. This example shows the format and the structure I except for the assignment, but it should not restrict your creativity in any way. I know you can do better than this!
Inspired by the temperature converter function and my love for cooking, I decided to extend the function. I would like to combine multiple converter possibilities for the kitchen into one function and improve the usage.
During the R lecture, we learned to define functions. We created two functions to convert celsius to Fahrenheit and the other way around.
# Convert Celsius to Fahrenheit
C2F <- function(celsius) {
return(9 / 5 * celsius + 32)
}
# Convert Fahrenheit to Celsius
F2C <- function(fahrenheit) {
return((fahrenheit - 32) * 5 / 9)
}
I also learned that I could source
a text file with a collection of functions, but I like to have one function with multiple options.
We also learned to combine tasks and create a function with multiple choices (converter options) using switch()
. It is a base function (switch {base}
) and it tests an expression against elements of a list. If the value evaluated from the expression matches the item from the list, the corresponding value is returned.
# Switch Syntax
# switch(<selection>,<Choice1>,<Choice2>,...,<ChoiceX>)
Using the function switch
inside our function to convert temperature. The new function hot
has a predetermined choice (option = "f2c"
) and will convert Fahrenheit to Celsius by default.
hot <- function(x, option = "f2c") {
switch(option,
f2c = ((x - 32) * 5 / 9),
c2f = (9 / 5 * x + 32))
}
So far, so good. In a next step, I like to extend the hot
function and add more converter options to it. For example, I like to convert pounds to (kilo)grams. I know that
1 pound (lb) is equal to 0.45359237 kilograms (kg).
The list of items (options) in the function switch
can easily be extended. I also will change the name of the function from hot
to cook
to make it a better fit.
cook <- function(x, option = "f2c") {
switch(option,
f2c = ((x - 32) * 5 / 9),
c2f = (9 / 5 * x + 32),
k2p = x / 0.45359237,
p2k = x * 0.45359237)
}
It is a neat function and we could be extended further if needed. Although the function works, it might be difficult to guess the options for somebody not familiar with the code of the function.
Table: Test function cook()
with different values and options.
x | option | y | Note |
---|---|---|---|
77 | 25 | works with default | |
25 | c2f |
77 | correct |
1 | k2p |
2.2 | too many decimal places |
2.2 | p2k |
1 | too many decimal places |
Error | does not work |
It does not make sense to have too many decimal places. I will restrict it to maximal 3 (round(<value>, 3)
). I would also like to add an error message if no value for x (x = NULL
) is provided. For this purpose I include an if
statements to test if x is NULL
. I tried x == NULL
and x < 1
but it did not work. I found the base function is.null(x)
and it works well.
cook <- function(x = NULL, option = "f2c") {
if (is.null(x)) {
cat("Missing value")
} else {
switch(
option,
# Temperatur
f2c = ((x - 32) * 5 / 9),
c2f = (9 / 5 * x + 32),
# Weight (rounded to 3 decimal places)
k2p = round(x / 0.45359237, 3),
p2k = round(x * 0.45359237, 3)
)
}
}
It works very well even with missing values.
cook(77)
# [1] 25
cook()
# Missing value
Better but help features are still missing. The options are also still hidden, and it is difficult to see what it converts into what. The function should be more talk-active. I could not implement my ideas using the function switch
. I think this is easier using if-else
statements.
cook <- function(x, name = NULL) {
if (is.null(name)) {
cat("Usage: cook(<Value>, \"Option\")
Options:
- c2f: Celsius to Fahrenheit
- f2c: Fahrenheit to Celsius
- k2p: Kilogram to Pound
- p2k: Pound to Kilogram")
} else if (name == "c2f") {
y <- (9 / 5 * x + 32)
return(cat(x, "C is", y, "F"))
} else if (name == "f2c") {
y <- ((x - 32) * 5 / 9)
return(cat(x, "F is", y, "C"))
} else if (name == "k2p") {
y <- round((x / 0.45359237), 3)
return(cat(x, "kg is", y, "lb"))
} else if (name == "p2k") {
y <- round((x * 0.45359237), 3)
return(cat(x, "lb is", y, "kg"))
} else {
return(cat("This option is not yet available!\n"))
}
}
Hurray, it works. Now I have usage and the options if the users is not providing any input.
cook()
# Usage: cook(<Value>, "Option")
# Options:
# - c2f: Celsius to Fahrenheit
# - f2c: Fahrenheit to Celsius
# - k2p: Kilogram to Pound
# - p2k: Pound to Kilogram
I also have a nicer output where I have a reminder of what I wanted to convert.
f(77, "f2c")
# 77 F is 25 C
The function is still not very elaborate and still limited. However, to add more conversions should not be too difficult.
Bon appétit !
A few more …