Exercise 1

Vectors

1 - Basics

Let:

x <- c(6, 30, 19, -4, 3, 5, -2, -14)
  1. What is the number of elements in x?
  1. What is the smallest element of x?
  1. What is the largest element of x?
  1. Calculate the sum of \(x^2\).
  1. Extract the third element of x.
  1. Extract the odd-indexed elements of x (1,3,5-th,… places).
  1. Calculate the square root of the product of x.
  1. Sort the vector x in ascending order.
  1. Sort the vector x in descending order.
  1. Extract a sub-vector of the negative elements of x.
  1. What is the proportion of positive elements in x?
  1. Locate the indexes of the extrema (minimun and maximum) of x.

2 - Creating vectors

  1. Create the following vector: 1 2 3 4 5 6 7 6 5 4 3 2 1.
  1. Create the sequence 1, 3, 5, ..., 39 and store it in x.
  1. Create an evenly-spaced sequence from 1 to 99 with 9 elements.
  1. Let hw <- 95 and exam <- 90 be the homework and final exam grades of a student. Write a command that computes the student’s final grade given that the homework constitutes 15%.
  1. Following the previous item, let hw <- c(95,100, 90, 85, 0, 90, 90) and exam <- c(90,100, 80, 81, 75, 60, 80). Assign the vector grades with the final grades corresponding to these achievements. The passing grade is 65. Round the grades to the nearest integer using round. Print both the rounded and un-rounded grades. Do you notice anything surprising?1

3 - Recycling rule

  1. Create the vector: 1 -2 3 -4 5 -6 7 -8 9 -10 11 -12 and assign it to y.
  1. Add 1 only to even-indexed elements of y (that is to -2,-4,-6,…).
  1. Analyze what happens when you run the following command.

Footnotes

  1. The rounding rule employed in R for .5 is IEC 60599, which says ‘go to the nearest even digit’. See ?round for details.↩︎