#' # 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?
-7^2

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


#' 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`.
v1 + 10

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

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

#' Select the 2nd and 5th element of `v1`.
v1[ c(2, 5) ]

#' Select all except the 2nd and 5th element from `v1`.
v1[ -c(2, 5) ]

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


#' Concatenate `v1` and `v2` and call this vector `v12`.
v12 <- c(v1,v2); v12

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

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

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

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


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

#' Take the square root of all elements of `v1` that are larger or equal to 3.
sqrt( v1[ ww ] )


#' Replace the value 12 in `v1` by 217.
v1[ v1 == 12 ] <- 217

#' Create a vector `id` with the entries of elements in `v1` which are larger than 3.
id <- which( v1 > 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).
w1 <- v1 
w1[id] <- 17
w1


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

#' 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!

g <- c(rep("weak",times=3), rep("strong", times=2)) # or
g <- rep( c("weak", "strong"), times = c(3, 2))
g

#' 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.
min(x)

#' Find the largest element in x.
max(x)

#' Find the range of x.
range(x)

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

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

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


#' Make a table of the frequency of the values in x.
table(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()`.)
hist(x, breaks = seq(-0.5, 10.5, 1))



#' Make a vector with the first five elements in `x`.
x[1:5]

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

#' Make a vector from `x` where all the elements with value less than
#' 5 are removed.
x[x>=5]  ## or
x[!(x<5)]

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

#' Determine the index of those elements in `x` which have value either 0 or 10.
which(x==0 | x==10)
(1:100)[x==0 | x==10] # or
(1:length(x))[x==0 | x==10] # or
(seq_along(x))[x==0 | x==10]

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

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

#' Determine the indices for which x and y have the same value.
(1:100)[x==y]

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

#' 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()`.)
diff(cumsum(x)[y==5])


#' ## 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()`).
mean(mpg)
sd(mpg)
4*sd(mpg)
diff(range(mpg))

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


#' What is $Pr(5 \le X \le 15)$?
pnorm(15, mean=10, sd=2.5) - pnorm(5, mean=10, sd=2.5)


#' 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)$? 
pnorm(2) - pnorm(-2)


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