Exercise 1

(a) Use c() to generate two datasets.

modifiedMortar <- c(16.85, 16.40, 17.21, 16.35, 16.52, 17.04, 16.96, 17.15, 16.59, 16.57)
unmodifiedMortar <- c(16.62, 16.75, 17.37, 17.12, 16.98, 16.87, 17.34, 17.02, 17.08, 17.27)

(b) Use mean() and median() to calculate the mean and median of each dataset.

mMean <- mean(modifiedMortar)
uMean <- mean(unmodifiedMortar)
mMedian <- median(modifiedMortar)
uMedian <- median(unmodifiedMortar)

Modified Mortar Mean: 16.764

Unmodified Mortar Mean: 17.042

Modified Mortar Median: 16.72

Unmodified Mortar Median: 17.05

(c) Use sd(), var() and IQR() to calculate the sample standard deviation, sample variance

and IQR of each dataset.

mSd <- sd(modifiedMortar)
uSd <- sd(unmodifiedMortar)
mVar <- var(modifiedMortar)
uVar <- var(unmodifiedMortar)
mIQR <- IQR(modifiedMortar)
uIQR <- IQR(unmodifiedMortar)

Modified Mortar Standard Deviation: 0.3164455

Unmodified Mortar Standard Deviation: 0.2479158

Modified Mortar Variance: 0.1001378

Unmodified Mortar Variance: 0.0614622

Modified Mortar IQR: 0.4875

Unmodified Mortar IQR: 0.335

(d) Construct the histograms for the two datasets and make comments about the shapes.

hist(modifiedMortar)

The histogram is skewed to the left which visually displays that the mean is greater than the median.

hist(unmodifiedMortar)

The histogram is skewed to the right which visually displays that the mean is less than the median.

(e) Construct comparative box-plots for the two groups and make comments about the similarity and difference.

boxplot(modifiedMortar, unmodifiedMortar)

Tension bond strength is seen to generally be higher than those in the unmodified mortar.

Exercise 2

(a) Use c() to generate the data.

coursesPerStudent <- c(4,2,3,3,1,5,4,2,2,4,5,6,4,3,3,4,4,5,6,1,2,2,3,4,3,3,5,2,1,3)

(b) Construct the frequency table using table().

table(coursesPerStudent)
## coursesPerStudent
## 1 2 3 4 5 6 
## 3 6 8 7 4 2

(c) Construct pie chart using pie(). Add colors and title to the chart.

pie(coursesPerStudent, col = c("lightblue", "lightgreen", "lightcoral", "lightyellow"), main="Pie Chart: Course Distribution per Student")

(d) Construct bar-plot using barplot(). Add color and title to the plot.

barplot(coursesPerStudent, col = c("lightblue", "lightgreen", "lightcoral", "lightyellow"), main="Bar-Plot: Course Distribution per Student")

(e) Use R functions to count how many students are taking more than four courses?

studentsMoreThanFour <- 0;
for (i in coursesPerStudent){
  if (i > 4){
    studentsMoreThanFour <- studentsMoreThanFour + 1
  }
}

Total number of students taking more than 4 courses: 6

Excercise 3

(a) Use seq() to generate a sequence 2, 4, …, 50.

sequence <- seq(2,50, by = 2)

2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50

(b) Use log() to generate a new sequence where each element is log-transformed from the sequence in (a).

sequence <- log(sequence)

0.6931472, 1.3862944, 1.7917595, 2.0794415, 2.3025851, 2.4849066, 2.6390573, 2.7725887, 2.8903718, 2.9957323, 3.0910425, 3.1780538, 3.2580965, 3.3322045, 3.4011974, 3.4657359, 3.5263605, 3.5835189, 3.6375862, 3.6888795, 3.7376696, 3.7841896, 3.8286414, 3.871201, 3.912023

sequence <- sequence[-(3:10)]

0.6931472, 1.3862944, 3.0910425, 3.1780538, 3.2580965, 3.3322045, 3.4011974, 3.4657359, 3.5263605, 3.5835189, 3.6375862, 3.6888795, 3.7376696, 3.7841896, 3.8286414, 3.871201, 3.912023 ## (d) Use length() to obtain the length of the resulting sequence in (c).

length <- length(sequence)

17

(e) Sort the resulting sequence in (c) from high to low using sort().

sort(sequence)
##  [1] 0.6931472 1.3862944 3.0910425 3.1780538 3.2580965 3.3322045 3.4011974
##  [8] 3.4657359 3.5263605 3.5835189 3.6375862 3.6888795 3.7376696 3.7841896
## [15] 3.8286414 3.8712010 3.9120230

0.6931472, 1.3862944, 3.0910425, 3.1780538, 3.2580965, 3.3322045, 3.4011974, 3.4657359, 3.5263605, 3.5835189, 3.6375862, 3.6888795, 3.7376696, 3.7841896, 3.8286414, 3.871201, 3.912023