#' # Exercise demonstration

#' Calculate $2+2$. Did you get what you expected?
2+2


#' # Exercises

#' ## Simple arithmetic

#' Calculate $-7^2$. Did you get what you expected?

#' Calculate $-7^2 + 7 \times 7 - 7/7$. Did you get what you expected?

#' Create the following two vectors:
v1 <- c(1, -5, 3, -7, 12, -9)
v2 <- c(30, 50, 10)

#' Add 10 to each element of `v1`.

#' Multiply each element of `v1` by 10.

#' Add `v1` to `v2`. Make sure you understand the result.

#' Select the 2nd and 5th element of `v1`.

#' Select all except the 2nd and 5th element from `v1`.

#' Generate a vector, that contains the 2nd, 2nd, 3rd, 4th, 2nd, 5th, 5th, 1st and 1st element of `v1`.


#' Concatenate `v1` and `v2` and call this vector `v12`.

#' How long is `v12`? Hint: Use the `length()` function.

#' Find out whether any element of `v12` lies between 2 and 4. Hint: `any()` is your friend.

#' Find out whether all elements of `v12` are non-missing. Hint: `is.na()` is your friend.

#' How many negative elements does `v12` contain? (Hint: Use the function `sum()` on a logical vector telling which elements are negative).


#' Create the logical vector `ww` which elements are `TRUE` if the elements of `v1` are larger or equal to 3 and `FALSE` otherwise.

#' Take the square root of all elements of `v1` that are larger or equal to 3.

#' Replace the value 12 in `v1` by 217.

#' Create a vector `id` with the entries of elements in `v1` which are larger than 3.

#' Replace values larger then 3 in `v1` by 17.
#' Call the resulting vector `w1`. (Hint: use the index vector `id`
#' from the previous task).


#' Give the elements of `v2` the names `weight`, `height`, `size`. Hint: `names()`

#' Generate the vector `g` which contains first 3 times the
#' haracter value 'weak' followed by 2 times the character value
#' 'strong'. Hint: Use the `rep()` function!

#' Run the following commands:
set.seed(1234)
x <- round(runif(100, 0, 10))
y <- round(runif(100, 0, 10))

#' Find the smallest element in x.

#' Find the largest element in x.

#' Find the range of x.

#' Find the sum of all the elements in x.

#' Find the mean of all the elements in x. (Does it seem reasonable?)

#' Find the standard deviation of all the elements in x. (Does it seem reasonable?)

#' Make a table of the frequency of the values in x.

#' Make a histogram of x (use the command `hist`). (If you are not
#' satisfied with the partition in intervals, use the option
#' breaks. The numbers $-0.5, 0.5, 1.5, ...,10.5$ can be obtained by
#' `seq()`.)

#' Make a vector with the first five elements in `x`.

#' Make a vector from `x` where the elements no. $11-20$
#' and element no. $51$
#' are removed. (Hint: Use indices with negative sign.)

#' Make a vector from `x` where all the elements with value less than
#' 5 are removed.

#' Make a vector from `x` where the elements with an odd value is
#' removed. (Hint: Use the modulo function `%%`.)

#' Determine the index of those elements in `x` which have value either 0 or 10.

#' Determine the index of those elements in `x` which have the same value as the
#' preceding one. (Hint: use the command `diff()`.)

#' Consider x and y as paired observations. Make a vector of those elements of x
#' for which y takes the value 5.

#' Determine the indices for which x and y have the same value.

#' Make a vector which from every pair of x and y chooses the largest of the two
#' values. (Hint: use the command `ifelse()`.)

#' Make a vector which for each time y takes the value 5 shows the cumulated sum
#' of x values from the preceding position where y took the value 5. 1 (Hint: use
#' the commands `cumsum()` and `diff()`.)


#' ## Summarizing data
#' We have a set of observations $x_1, x_2, \dots, x_n$ and form the $z$--score
#' $$
#'     z_i = \frac{x_i-\bar x}{s_x}
#' $$
#' 
#' Calculate (theoretically) the sample mean and sample variance of $z_1, \dots, z_n$. 
#' Verify your finding empirically; either from a real dataset or from a simulated dataset.

#' The variable
mpg <- mtcars$mpg
mpg
#' contains miles per gallon for 32 cars. Find the sample mean and sample
#' standard deviation. Compare with the median and the "4sd rule" (hint: `range()`).

#' If $X \sim N(10, 2.5^2)$ what is then the probability $Pr(X \le 15)$? 


#' What is $Pr(5 \le X \le 15)$?

#' If $X \sim N(10, 2.5^2)$ what is then the distribution of $Z=\frac{X-10}{2.5}$? 

#' What is $Pr(-2 \le Z \le 2)$? 

#' How does that relate to the "4sd-rule"?
