File paths


Ex. 2.1

Open R and print to screen your current working directory.

Create a directory on your Desktop called Rcourse_day2. You can do it manually or using the dir.create function.

Tip: use the help function to understand how dir.create is supposed to work.

Create two sub-directories in the Rcourse_day2 directory called Data and Scripts.

Change your working directory to the Scripts subfolder and check whether the change has been successful.


R data and scripts

Ex. 2.2

Initialize two numeric vectors, b equal to 5 and h equal to 4, with the base and height of a triangle.

Save the b and h objects in the Data sub-directory, in a file named triangle.RData.


Ex. 2.3

Open a new R script file from the toolbar: File > New File > R Script.

Copy the following text in the script:

getArea <- function (base, height) {
  
  area <- #...
    
  return(area)
  
}

Substitute the commented dots with the code necessary to compute the triangle area to be assigned to the area variable and save the final script in Scripts/Triangle_area.R.


Ex. 2.4

Open a new R script file and save it with the name Scripts/Day2_Ex4.R.

In this script write the code to:

  • Clean your workspace
  • Load the triangle.RData data (from Ex. 2.2)
  • Source the Triangle_area.R script (from Ex. 2.3)
  • Apply the getArea function to compute the triangle area from b and h and save it into a new variable called A.


Applying functions to vectors


Ex. 2.5

Initialize in R the following vectors:

x <- c(1, 2, 200, 6, 80, 23)
y <- c(100, 50, 5, 30, 1, 20)

Then compute:

  • The mean of x and y, separately.

  • The variance of x and y, separately.

  • Spearman’s correlation of x and y.

Ex. 2.6

Initialize in R the following named vector:

mLen <- c(31, 28, 31, 30, 
  31, 30, 31, 31, 
  30, 31, 30, 31)

names(mLen) <- c("Jan", "Feb", "Mar", "Apr", 
  "May", "Jun", "Jul", "Aug",
  "Sep", "Oct", "Nov", "Dec")

Use the table function to count how many months are 31-day long.


Ex. 2.7

Use the sample function to create a vector x containing the first 10 numbers obtained in a bingo extraction (possible values from 1 to 90).

Sort the numbers in increasing order and save them in vector named y.


Ex. 2.8

Initialize in R the following vectors:

x <- seq(1, 5)
y <- seq(3, 7)

Use the intersect and setdiff functions to identify the elements belonging to the colored areas in the figure below, and save them into two variables called a and b.


Ex. 2.9

Initialize in R the following vectors:

x <- c(2, 4, 5, 7, 8)
y <- c(2, 4, 6, 8, 10)
z <- c(2, 3, 4, 5, 6)

Identify the elements belonging to the colored areas in the figure below, and save them into two variables a and b.

Tip: The intersect and setdiff functions only accept two sets, but can be combined recursively (e.g., intersection of the intersection).


Ex. 2.10

Create a function called firstNsum that for a positive integer n specified as argument, computes the sum of the first n positive numbers

Tip: use seq and sum.

Create a function called firstNsum2 that for a positive integer n specified as argument, computes the following formula:

\[ \frac{n(n+1)}{2} \]

Verify that the results from these two functions are the same using n=10, 20, and 100 as examples.


Ex. 2.11

Create a function called FtoCtemp that takes as input the temperature in Fahrenheit and converts it into Celsius using the following formula:

\[ C=\frac{5}{9}(F-32) \]

Use this function to determine whether your are going to sleep comfortably in your hotel room in New York City, where the temperature is set to 47ºF.