--- title: "Lecture 2: Exercises" date: October 2nd, 2018 output: html_notebook: toc: true toc_float: true --- # Execise 1: Control Flow ## Part 1.1 Use a for loop to: a. Print all the letters of the Latin alphabet:` b. Print the numbers 10 to 100 that are divisible by 7 c. Print the numbers 1 to 100 that are divisible by 5 but not by 3. ## Part 1.2 a. Find all numbers not greater than 10,000 that are divisible by 5, 7 and 11 and print them. b. Print for each of the numbers x = 2, . . . 20, all numbers that divide x (all factors) excluding 1 and x. Hence, for 18, it should print 2 3 6 9. # Execise 2: Functions ## Part 2.1 a.Create a function what will return the number of times a given integer is contained a given vector of integers. The function should have two arguments one for a vector and the other for a scalar. b. Then, generate a random vector of 100 integers (in a range 1-20) use the function to count the number of times the number 12 is in that vector. ## Part 2.2 Write a function that takes in a data.frame as an input, prints out the column names, and returns its dimensions. # Execise 3: Apply family functions ## Part 1 Below we print six first rows of the built-in dataset, `mtcars`, from the 1974 Motor Trend US magazine, which comprises information on the fuel consumption and 10 aspects of automobile design and performance for 32 selected car models. ```{r} head(mtcars) ``` Use `apply()` function to find the standard deviation and the 0.8-quantile of each of the automobile characteristic. ## Part 2 Below is a vector of dates in year 2018. ```{r} set.seed(1234) y2018 <- seq(as.Date("2018-01-01", format = "%Y-%m-%d"), as.Date("2018-12-31", format = "%Y-%m-%d"), "days") length(y2018) # A random sample of 10 dates from 2018 y2018_sample <- sample(y2018, size = 10) y2018_sample ``` Use an `apply` family function to return the number of weeks left from each day in `y2018_sample` to the New Year, 2019/01/01.