15 Exercises

15.1 Vectors

15.1.1

Compute the mean of:

  1. \(v=(0, 1, NA, 2, 7).\)
  2. \(u=(-Inf, 0, 1, NA, 2, 7, Inf).\)

15.1.2

Create:

  1. a vector with values \(e^x\cos(x)\) at \(x=3, 3.1, 3.2, \ldots, 6.\)

  2. a vector with values \(\left(2,\frac{2^2}{2},\frac{2^3}{3}, \ldots, \frac{2^{25}}{25}\right).\)

15.1.3

Compute:

  1. \[\sum_{j=10}^{100}{j^3+4j^2}.\]

  2. \[\sum_{j=1}^{25}{\frac{2^j}{j}+\frac{3^j}{j^2}}.\]

15.1.4

Create the vectors:

  1. \((1,2,3,\ldots,19,20)\).
  2. \((20,19,\ldots,2,1)\).
  3. \((1,2,3,\ldots,19,20,19,18,\ldots,2,1)\).
  4. \((1,2,3, 1,2,3,\ldots,1,2,3)\) where there are 10 occurrences of 1.
  5. \((1,1,\ldots,1, 2,2,\ldots,2, 3,3,\ldots,3)\) where there are 10 occurrences of 1, 20 occurrences of 2 and 30 occurrences of 3.
  6. \((0.1^30.2^1,0.1^60.2^4,\ldots,0.1^{36}0.2^{34})\).

15.1.5

Use the function paste to create the following character vector of length 20: \[ \left(\text{"label 1"},\text{"label 2"},\ldots,\text{"label 20"}\right) \]

15.1.6

Set the vector v2 equal to the following: “A” “A” “B” “B” “C” “C” “D” “D” “E” “E” (note the letters are all uppercase).

15.1.7

Set the vector v3 equal to the following: “a” “b” “c” “d” “e” “a” “b” “c” “d” “e” (note the letters are all lowercase)

15.1.8

Set the vector v4 equal to the words “dog” 10 times, “cat” 9 times, “fish” 6 times, and “fox” 1 time.

15.1.9

Run the following lines:

> set.seed(50)
> x <- sample(0:999, 250, replace=TRUE)
> y <- sample(0:999, 250, replace=TRUE)
  1. Explain the meaning of x and y.

  2. Denote by \(x_i\) and \(y_i\) the entries of the vectors \(x\) and \(y\), respectively. Create vectors \((y_2-x_1,\ldots,y_n-x_{n-1})\) and \(\left(\frac{\sin(y_1)}{\cos(x_2)},\ldots,\frac{\sin(y_{n-1})}{\cos(x_n)}\right).\)

  3. Compute \[ \sum_{i=1}^{n-1}\frac{e^{-x_{i+1}}}{x_i+10}\]

  4. Denote by \(\overline{x}\) the mean of the vector \(x\) and compute \[\sum_{i=1}^n|x_i-\bar{x}|^{1/2}.\]

  5. Extract the values of \(x\) which correspond to the values of \(y\) which are \(> 500\).

  6. How many values of \(x\) are above the mean of \(y\)?

  7. How many values of \(x\) are smaller than the minimum value of \(y\)?

  8. How many values of \(x\) are divisible by \(2\)?

  9. Extract the 1st minimum value of \(x\), the 4th minimum value of \(x\), the 7th minimum value of \(x\), etc.

15.2 Matrices

15.2.1

Consider the matrix

\[A = \begin{bmatrix} 1 & 1 & 3 \\ 5 & 2 & 6 \\ -2 & -1 & -3 \end{bmatrix}\]

  1. Verify that \(A^3=0\).

  2. Replace the third column by the sum of column 1 and column 2.

15.2.2

Consider the matrix

\[ M = \begin{bmatrix} 33 & 51 & 60 \\[0.3em] 20 & 35 & 17 \\[0.3em] 15 & 27 & 24 \\[0.3em] 15 & 21 & 28 \\[0.3em] 14 & 25 & 29 \\[0.3em] 10 & 53 & 24 \\[0.3em] \end{bmatrix},\]

and replace even numbers by \(-1\).

15.2.3

Solve the linear system

\[\begin{cases} 2y+x-z=1\\ 2z-x-y=0\\ x+1+y+2z=1 \end{cases}\]

15.2.4

Run the following lines:

> set.seed(75)
> M <- matrix( sample(10, size=60, replace=TRUE), nrow=6)
  1. Explain the result.

  2. Find the number of entries in each row which are greater than 4.

  3. Which rows contain exactly two occurrences of the number 7?

  4. Denote by \(M_{i,j}\) the entries of the \(n\times m\) matrix \(M\) and compute the following means and sums of squares: \[\begin{align*} \mu&=\frac{1}{n\times m}\sum_{i,j}M_{i,j}\\ \mu_i&=\frac{1}{m}\sum_{j}M_{i,j},\quad i=1,\ldots,n\\ SS_1&=\sum_i (\mu-\mu_i)^2\\ SS_2&=\sum_{i,j}(M_{i,j}-\mu_i)^2\\ SS_3&=\sum_{i,j}(M_{i,j}-\mu)^2 \end{align*}\]

15.3 Data-frames

15.3.1

Install and load the package DAAG. Execute:

  1. Type library(help="DAAG") to get the information on this package.

  2. Consider the dataset carpriceand apply the convenient R command to check the structure of the dataset.

  3. Set a name for carprice, say x.

  4. Explain the meaning of table(x$Type).

  5. Replace all column names.

  6. Compute the mean, variance, standard deviation, range, maximum and minimum of the maximum price variable.

  7. Drop all the rows with maximum price higher than 24.8 m.u.

  8. Drop all the rows with minimum price lower than 14.0 m.u.

15.3.2

Create a list with information about 3 workers as follows:

  1. The list should have 4 vectors as components: name, age, gender and a boolean variable (it assumes values 0 or 1 that defines whether the person works or not).

  2. Create a data frame with the information from the previous list.

  3. Add a vector to the previous data frame with information about the person’s profession.

  4. Compute the mean, variance, standard deviation, range, maximum and minimum of the age.

15.4 Lists

15.4.1

Run the following lines:

> set.seed(75)
> x <- sample(10, size=10, replace=TRUE)
> y <- sample(10, size=10, replace=TRUE)
  1. Create a list whose elements are the vectors \(x\) and \(y\) with names “sample1” and “sample2”, respectively.
  2. Create a list with the mean and standard deviation of each sample.

15.4.2

Create a list:

  1. with years from 2005 to 2016, 12 months and 31 days.
  2. replace year by c(2000:2010).
  3. delete the value 4 of the month.

15.5 Functions

15.5.1

Write a function called my_add that adds two numbers (x and y) together and returns the results. Run my_add(5,7).

15.5.2

Copy the function my_add above and add an error message that returns “x and y must be numbers” if x or y are not both numbers.

15.5.3

  1. Write a function that accepts a numerical vector \((x_1,x_2,\ldots,x_n)\) as argument and returns the vector \[\left(x_1,\frac{x_2^2}{2},\ldots,\frac{x_n^n}{n}\right).\] Run for 1:4.

  2. Write a function that accepts a single number \(x\) and a positive integer \(n\) and returns the sum \[1+x+\frac{x^2}{2}+\frac{x^3}{3}+\cdots+\frac{x^n}{n}.\] Run for \(x=\sqrt{2}\) and \(n=5\).

  3. Write a function which takes a matrix as single argument and returns a matrix which is the same as the function argument but every odd entry is doubled. Run for matrix(sample(1:10),2,5)

  4. Write a function to convert the temperature measured in Celsius (°C) to Fahrenheit (°F). Check that f.d(0) = 32 and f.d(-40) = -40.

  5. Write a function that takes a single integer argument \(n\) and returns the sum of its divisors, e.g., if \(n=6\), then the function returns \(1+2+3+6\).

  6. Write a function that takes arguments \(a,b,c\in\mathbb{R}\) and returns the roots of the polynomial \(ax^2+bx+c\). The output should be a list containing the two roots. In case the roots are complex conjugated, the output of each root should be a list of its real and imaginary parts. Find the solutions of \(x^2-4x+4=0\) and \(3x^2-x+4=0\).

15.5.4

Consider the function

\[ f(x)=\begin{cases} 2e^{-2x}, & x \geq 0 \\ 0, & x<0 \end{cases}. \]

  1. Show that \(f\) is a density probability function (dpf).

  2. Let \(X\) be a r.v. with dpf \(f\) and compute \(P(X>1)\). Show the value with 2 decimal places.

  3. Compute \(P(0.3<X<0.7)\). Show the value with 3 decimal places.

  4. Compute \(E[X]\). Show the value with 1 decimal place.

15.5.5

Plot a graphic of \(f(x)=1-\frac{1}{x}sin(x)\) for \(0<x<5\) and for \(0<x<50\).

15.5.6

Plot a graphic of \(f(x,y)=10\frac{\sin(\sqrt{x^2+y^2})}{\sqrt{x^2+y^2}}\) for \(1<x<20, ~ 1<y<20\) and \(z=\)outer(x,y,g). By the way, run outer(0:9, 0:9, "*") and outer(0:9, 0:9, "+").

Change the parameters and run again.

15.6 Importing files

15.6.1

Consider the file describing the life expectancy of males and females born in the UK. Execute:

  1. Save the file to your computer and import the data to R. Also display the dimensions, column names and a summary of the data frame.

  2. Determine the number of years between 1841 and 1900 where male life expectancy was less than 40.

  3. Plot how the female life expectancy changes over the years (given in the age column). Add labels to axis of the plot and join the points by straight lines.

  4. Plot the relationship between male and female life expectancy as a scatter plot. e.g. male on the x axis and female on the y axis. Add labels to the axis and a title.

  5. Add a linear trend to the previous scatter plot.

  6. Create a histogram and a boxplot of the male and female life expectancy. Use 25 breaks.

 

Copyright: All rights reserved. No parts of the content of this website may be reproduced or distributed without the prior written permission of the author. Without prior written permission, it is not permitted to copy, download or reproduce the text, code, and images in any way whatsoever.

2024 \(|\) Nuno M. Brites \(|\)