File paths


Ex. 2.1

Open R and print to screen your current working directory.

getwd()
## [1] "/Users/francescafinotello/Dropbox/R_course_MUI_2021/Solutions"

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.

help(dir.create)
dir.create("/Users/francescafinotello/Desktop/Rcourse_day2", showWarnings = FALSE)

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

dir.create("/Users/francescafinotello/Desktop/Rcourse_day2/Data", showWarnings = FALSE)
dir.create("/Users/francescafinotello/Desktop/Rcourse_day2/Scripts", showWarnings = FALSE)

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

setwd("/Users/francescafinotello/Desktop/Rcourse_day2/Scripts")
getwd()
## [1] "/Users/francescafinotello/Desktop/Rcourse_day2/Scripts"


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.

b <- 5
h <- 4

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

save(b, h, file = "../Data/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.

getArea <- function (base, height) {
  
  area <- b * h / 2
    
  return(area)
  
}


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.
rm( list = ls() )
load("../Data/triangle.RData")
source("Triangle_area.R")
A <- getArea(b, h) 
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.
mean(x)
## [1] 52
mean(y)
## [1] 34.33333
  • The variance of x and y, separately.
var(x)
## [1] 6149.2
var(y)
## [1] 1350.667
  • Spearman’s correlation of x and y.
cor(x, y, method = "spearman")
## [1] -0.9428571

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.

table(mLen)
## mLen
## 28 30 31 
##  1  4  7


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.

y <- sort( sample( seq(1, 10), 10) )


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.

a <- intersect(x, y)
a
## [1] 3 4 5
b <- setdiff(y, x)
b
## [1] 6 7


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

a <- intersect( intersect(x, y), z )
a
## [1] 2 4
b <- setdiff( intersect(x, y), z )
b
## [1] 8


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

firstNsum <- function (n) {
  
  Nsum <- sum(seq(1, n))
  return(Nsum)
  
}

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} \]

firstNsum2 <- function (n) {
  
  Nsum2 <- n * ( n + 1 ) / 2
  return(Nsum2)
  
}

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

firstNsum(10) == firstNsum2(10)
## [1] TRUE
firstNsum(20) == firstNsum2(20)
## [1] TRUE
firstNsum(100) == firstNsum2(100)
## [1] TRUE


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) \]

FtoCtemp <- function (F) {
  
  C = 5 * ( F -  32 ) / 9
  return(C)
  
}

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.

FtoCtemp(47)
## [1] 8.333333