Put all required libraries into this chunk.
```{r setup}
```
## Exercise 01
Take a look at the function definition and fill-in answers below (do not run this chunk, as it will overwrite a proper cos() function).
```r
cos <- function(angle_in_radians){
...magic...
return(a_number)
}
```
* What is the name of the function? **Answer:**
* How many parameters does it have? **Answer:**
* How would you use it to compute a cosine of 0, 0.2, and 0.3? Write the code in the chunk below.
```{r exercise 01}
```
## Exercise 02
Write a definition of a function that doubles a value, you do not need to write any code inside of wiggle brackets, so no computation or a return statement at the end of it.
```{r exercise 02-1}
```
Write the code that implements the "double-the-value" computation and test it by creating a variable with the same name as your parameter inside the function.
```{r exercise 02-2}
```
Combine code from 02-1 and 02-2, make sure you `return` the result of computation. Test it by calling the function in the same chunk (you can do this only _after_ the function was defined!).
Store results in a local variable and return its value.
```{r exercise 02-3}
```
Return computation directly (no extra variable).
```{r exercise 02-4}
```
## Exercise 03
Write a function that converts an angle in _degrees_ to _radians_. The formula is $$rad = \frac{deg \cdot \pi}{180}$$.
```{r exercise 03}
```
## Exercise 04
Study the code and decide on what will be printed. Hint, it might help to write comments next to each line that uses a variable to state whether it is a global variable, parameter (can have the same name as a global variable, "overwriting" it), or a local variable (can have the same name as a global variable or a parameter, "overwriting" them). Once you wrote down the answer, run the code to check it.
**Answer #1:**
```{r exercise 04-1}
x <- 1
my_function <- function(){
x <- x + 1
}
print(x)
```
**Answer #2:**
```{r exercise 04-2}
x <- 1
my_function <- function(){
x <- x + 1
}
my_function()
print(x)
```
**Answer #3:**
```{r exercise 04-3}
x <- 1
my_function <- function(){
x <- x + 1
return(x)
}
x <- my_function()
print(x)
```
**Answer #4:**
```{r exercise 04-4}
x <- 1
my_function <- function(x){
y <- x + 1
return(y)
}
x <- my_function(5)
print(x)
```
**Answer #5:**
```{r exercise 04-5}
x <- 1
my_function <- function(x){
x <- 2
return(x)
}
x <- my_function(5)
print(x)
```
**Answer #6:**
```{r exercise 04-6}
x <- 1
my_function <- function(x){
y <- x + 1
return(x)
}
x <- my_function(5)
print(x)
```
**Answer #7:**
```{r exercise 04-7}
x <- 1
my_function <- function(x){
y <- x + 1
return(x + y)
}
x <- my_function(x) + x
print(x)
```
## Exercise 05
Write a function that takes _two_ parameters `x` and `y` computing _radius_ (distance from `(0,0)` to `(x, y)`). The formula is
$$R = \sqrt{x^2 + y^2}$$
```{r exercise 05}
```
## Exercise 06
use [mpg](https://ggplot2.tidyverse.org/reference/mpg.html) table from the _ggplot2_ package. Write a function that takes a table as a _parameter_ (so it does not assume that table exists in the global environment), computes and returns average miles-per-gallon efficiency based on city `cty` and highway `hwy` test cycles for each entry (so you need to compute 234 values based on each pair of `cty` and `hwy` values).
Compute and return a vector of values based on the table passed as parameter.
```{r exercise 06-1}
```
Do the same computation but add the result _to the table_ as `avg_mpg` column and return the entire table.
```{r exercise 06-2}
```
## Exercise 07
Write function that computes [mean](https://stat.ethz.ch/R-manual/R-devel/library/base/html/mean.html) efficiency for a particular cycle, either city or highway across all cars (so, it returns a single value). For this, the function will take _two_ parameters: the table itself and a string with the name of the column. You can then use it to access the column via [double square brackets](#get-single-column). In short, you function takes a table and a string with a column name and returns a single number (mean for the specified column).
```{r exercise 07}
```
## Exercise 08
Call function from exercise 07 with both arguments _named_.
```{r exercise 08-1}
```
Call function from exercise 07 with only one _named_ argument and one argument passed by _position_. What is the only variant that you recommend?
```{r exercise 08-2}
```
## Exercise 09
Modify the function from exercise 07, so that _by default_ it would compute average efficiency for the city cycle. Test this by calling the function with and without the column name parameter.
```{r exercise 09}
```
## Exercise 10
What will be the order of computation in the example below?
**Answer: **
```{r exercise 10-1}
atan2(sqrt(abs(4)), abs(sqrt(log(5))))
```
Decompose the call above into a sequence of calls using temporary variables. Check that you get the same final answer!
```{r exercise 10-2}
```
## Exercise 11
Assume that you need to compute an 1) absolute value, 2) square root, and 3) logarithm of a number (pick the one you like) in that order, so logarithm of square root of absolute number. Write this computation using temporary variables, nested call, and pipes (`|>`). Check that all three versions give you the same answer.
```{r exercise 11-1 temporary variables}
```
```{r exercise 11-2 nested calls}
```
```{r exercise 11-3 pipes}
```
## Exercise 12
Write a function that has three parameters, each holding a value, and returns the _smallest_ number (you can use [min](https://stat.ethz.ch/R-manual/R-devel/library/base/html/Extremes.html)) function for that. Use |>` to pipe value to
1. first parameter (other two parameters here and below, should be specified inside the call)
2. second parameter
3. third parameter
```{r exercise 12-1 define function}
```
```{r exercise 12-2 pipe to first parameter}
```
```{r exercise 12-3 pipe to second parameter}
```
```{r exercise 12-4 pipe to third parameter}
```
## Exercise 13
Reuse function from the previous exercise. Use `%>%` to pipe value to
1. first parameter (other two parameters here and below, should be specified inside the call)
2. second parameter
3. third parameter
4. first and second parameters
5. second and third parameters
6. all three parameters
```{r exercise 13-1 pipe to first parameter}
```
```{r exercise 13-2 pipe to second parameter}
```
```{r exercise 13-3 pipe to third parameter}
```
```{r exercise 13-4 pipe to first and second parameter}
```
```{r exercise 13-5 pipe to second and third parameter}
```
```{r exercise 13-6 pipe to all three parameters}
```