Execise 1: Control Flow

Part 1.1

Use a for loop to:

  1. Print all the letters of the Latin alphabet:`

  2. Print the numbers 10 to 100 that are divisible by 7

  3. Print the numbers 1 to 100 that are divisible by 5 but not by 3.

Part 1.2

  1. Find all numbers not greater than 10,000 that are divisible by 5, 7 and 11 and print them.

  2. 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.

  1. 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.

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.

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)
[1] 365
# A random sample of 10 dates from 2018
y2018_sample <- sample(y2018, size = 10)
y2018_sample
 [1] "2018-02-11" "2018-08-15" "2018-08-10" "2018-08-14" "2018-11-07" "2018-08-19"
 [7] "2018-01-04" "2018-03-25" "2018-08-26" "2018-07-03"

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.

LS0tCnRpdGxlOiAiTGVjdHVyZSAyOiBFeGVyY2lzZXMiCmRhdGU6IE9jdG9iZXIgMm5kLCAyMDE4Cm91dHB1dDogCiAgaHRtbF9ub3RlYm9vazoKICAgIHRvYzogdHJ1ZQogICAgdG9jX2Zsb2F0OiB0cnVlCi0tLQoKCiMgRXhlY2lzZSAxOiBDb250cm9sIEZsb3cgCgojIyBQYXJ0IDEuMQoKVXNl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