Exercise 1

R comes with many functions that you can use to do sophisticated tasks like random sampling. For example, you can round a number with the round() function, or calculate its factorial with the factorial() function. Using a function is pretty simple. Just write the name of the function and then the data you want the function to operate on in parentheses.

# Create a vector with numbers from 1 to 6
die <- 1:6

You can simulate a roll of the die with R’s sample() function. sample() takes two arguments: a vector named x and a number named size.

Solution to Exercise 1

Q1
round(3.1415)
## [1] 3
factorial(12)
## [1] 479001600
mean(1:17)
## [1] 9

-There is no difference in the expected output (numbers one through six). Named arguments free you from the need to remember or to look up the order of parameters in the parameter lists of called methods.

sample(x = die, size = 1)
## [1] 3
sample(die, size = 1)
## [1] 6
sample(die, 1)
## [1] 5
Q2

Displays the argument names and corresponding default values of a function or primitive.

args(round)
## function (x, digits = 0) 
## NULL
round(3.14159265, digits = 4)
## [1] 3.1416

Exercise 2

Use R as a calculator to compute the following values:

  1. \(27 (38 -17)\)
  2. \(ln(14^7)\)
  3. \(\sqrt\frac{436}{12}\)

Solution to Exercise 2

27*(38-17)
## [1] 567
log(14*7)
## [1] 4.584967
sqrt(436/12)
## [1] 6.027714

Exercise 3

Create the following vectors in R:

\(a = (5, 10, 15, 20, ..., 160)\)

\(b = (87, 86, 85, ..., 56)\)

Multiply these vectors and call the result d. Select subsets of d to identify the following.

Solution to Exercise 3

# Creating vectotr 'a' by 5
a <- seq(5, 160, by = 5)
# Creating vectotr 'b' by -1
b <- seq(87, 56, -1)
# Multipying vector 'a' and vector 'b'
d <- a*b
# Vector 'd', element at position '19'
d[19]
## [1] 6555
# Vector 'd', element at position '20'
d[20]
## [1] 6800
# Vector 'd', element at position '21'
d[21]
## [1] 7035
# Vector 'd', elements less than '2000'
d[d < 2000]
## [1]  435  860 1275 1680

Exercise 4

  1. sample(letters, 5, replace=T)

  2. sample(letters, 5, replace=F)

  3. sample(1:26, 5, replace=T)

  4. c(5,letters)

  5. sample(LETTERS, 5, replace=F)

Solution to Exercise 4

Q1

Same outcome : 669

Averaging Any Long Series of Consecutive Numbers

\[ Average = \frac{X_1+X_n}{2}\]

1:1337 generate a vector 1,2,3…1337 and then calculate the mean:

mean(1:1337)
## [1] 669

c(1,1337) generate a two values vector [1, 1337] the first and last element of vector 1:1337

mean(c(1,1337))
## [1] 669
Q2
# If replace equal TRUE, the output could have duplicate values
sample(letters, 5, replace=T)
## [1] "h" "f" "a" "w" "z"
# If replace equal FALSE, the output wont have duplicate values
sample(letters, 5, replace=F)
## [1] "p" "l" "r" "f" "a"
sample(1:26, 5, replace=F)
## [1] 13 11 26  4 20
c(5,letters)
##  [1] "5" "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p"
## [18] "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"
sample(LETTERS, 5, replace=F)
## [1] "E" "O" "P" "A" "W"
Q2

We should use solution b or e (lowercase or uppercase) for selecting different letters, replace = F wont let us have an output with same letters.

Exercise 5

Look up the usage of the command seq (you can use ?seq or help(seq) to obtain help from within R) Now determine what the following commands achieve:

x <-  seq(from = 0, to = 4*pi, length.out=1000)
y <-  sin(x)
plot(x, y, type = "l", main = "graph of sin(x)")

What happens if you leave out type="l" of the plot command?

Solution to Exercise 5

Removing type="l" changes the graph type and uses p points as default value

Possible types:

  • “p” for points,
  • “l” for lines,
  • “b” for both,
  • “c” for the lines part alone of “b”,
  • “o” for both ‘overplotted’,
  • “h” for ‘histogram’ like (or ‘high-density’) vertical lines,
  • “s” for stair steps,
  • “S” for other steps, see ‘Details’ below,
  • “n” for no plotting.
x <-  seq(from = 0, to = 4*pi, length.out=1000)
y <-  sin(x)
plot(x, y, main = "graph of sin(x)")