tinytex::install_tinytex()— title: “STA 314 - LAB 1” subtitle: “Introduction to R” author: “Student Name: ____________________” date: “01 December 2026” output: pdf_document: toc: false number_sections: false df_print: default —

Exercise 1: Install R and RStudio; create project and script

Install R and RStudio. In RStudio, create a project named STA314 using File > New Project. Create a new R script using File > New File > R Script and save it as lab1.R in the project folder.

Exercise 2: Basic calculations

Calculate \(17 \\times 23 - 150\), the square root of 2025, and the logarithm of 1024 to base 2.

calculation_1 <- 17 * 23 - 150
calculation_1
#> [1] 241
calculation_2 <- sqrt(2025)
calculation_2
#> [1] 45
calculation_3 <- log(1024, base = 2)
calculation_3
#> [1] 10

Exercise 3: Seven daily temperatures for Eldoret

The question does not provide specific temperature readings, so the values below are illustrative. Replace them with the readings supplied by your lecturer if available.

eldoret_temps <- c(18, 20, 19, 21, 17, 22, 20)

# Mean temperature
mean_temp <- mean(eldoret_temps)
mean_temp
#> [1] 19.57143
# Sample standard deviation
sd_temp <- sd(eldoret_temps)
sd_temp
#> [1] 1.718249
# Day numbers with temperatures above the mean
days_above_mean <- which(eldoret_temps > mean_temp)
days_above_mean
#> [1] 2 4 6 7
# Temperatures above the mean
eldoret_temps[eldoret_temps > mean_temp]
#> [1] 20 21 22 20

Exercise 4: Data frame of eight students

The following student records are examples created for this exercise. Attendance is recorded as a percentage.

students <- data.frame(
  Student = c(
    "Student 1", "Student 2", "Student 3", "Student 4",
    "Student 5", "Student 6", "Student 7", "Student 8"
  ),
  Attendance = c(80, 65, 90, 75, 85, 70, 95, 60),
  CAT_Score = c(78, 62, 88, 70, 82, 68, 91, 55)
)

# Display the data frame
students
#>     Student Attendance CAT_Score
#> 1 Student 1         80        78
#> 2 Student 2         65        62
#> 3 Student 3         90        88
#> 4 Student 4         75        70
#> 5 Student 5         85        82
#> 6 Student 6         70        68
#> 7 Student 7         95        91
#> 8 Student 8         60        55
# Display students with attendance above 75
students[students$Attendance > 75, ]
#>     Student Attendance CAT_Score
#> 1 Student 1         80        78
#> 3 Student 3         90        88
#> 5 Student 5         85        82
#> 7 Student 7         95        91

Exercise 5: Explore the iris dataset

Use str() and summary() to inspect the dataset. Calculate the mean Petal.Length for each species using tapply(), then draw a boxplot.

# Inspect the structure
str(iris)
#> 'data.frame':    150 obs. of  5 variables:
#>  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
#>  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
#>  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
#>  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
#>  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
# Summary statistics
summary(iris)
#>   Sepal.Length    Sepal.Width     Petal.Length    Petal.Width   
#>  Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100  
#>  1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300  
#>  Median :5.800   Median :3.000   Median :4.350   Median :1.300  
#>  Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199  
#>  3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800  
#>  Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500  
#>        Species  
#>  setosa    :50  
#>  versicolor:50  
#>  virginica :50  
#>                 
#>                 
#> 
# Mean Petal.Length by Species
mean_petal_by_species <- tapply(
  iris$Petal.Length,
  iris$Species,
  mean
)
mean_petal_by_species
#>     setosa versicolor  virginica 
#>      1.462      4.260      5.552
# Boxplot of Petal.Length by Species
boxplot(
  Petal.Length ~ Species,
  data = iris,
  main = "Petal Length by Iris Species",
  xlab = "Species",
  ylab = "Petal Length (cm)",
  col = c("lightblue", "lightgreen", "lightpink")
)

Exercise 6: Plot mpg against hp using mtcars

Create a scatter plot with labelled axes and calculate the correlation between mpg and hp.

plot(
  mtcars$hp,
  mtcars$mpg,
  main = "Miles per Gallon against Horsepower",
  xlab = "Horsepower (hp)",
  ylab = "Miles per Gallon (mpg)",
  pch = 19
)

# Correlation between horsepower and miles per gallon
hp_mpg_correlation <- cor(mtcars$mpg, mtcars$hp)
hp_mpg_correlation
#> [1] -0.7761684

Exercise 7: Import a CSV file; report its size and missing values

For a reproducible example, this section writes the built-in iris dataset to a CSV file and then imports that CSV file. If you have your own CSV file, place it in the STA314 project folder and change csv_file to its filename.

# Create an example CSV file in the project folder
write.csv(iris, "iris_sample.csv", row.names = FALSE)

# Import the CSV file
csv_file <- "iris_sample.csv"
my_data <- read.csv(csv_file, header = TRUE)

# Display the first six rows
head(my_data)
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1          5.1         3.5          1.4         0.2  setosa
#> 2          4.9         3.0          1.4         0.2  setosa
#> 3          4.7         3.2          1.3         0.2  setosa
#> 4          4.6         3.1          1.5         0.2  setosa
#> 5          5.0         3.6          1.4         0.2  setosa
#> 6          5.4         3.9          1.7         0.4  setosa
# Report dataset dimensions (rows and columns)
dim(my_data)
#> [1] 150   5
# Report the number of rows and columns separately
cat("Number of rows:", nrow(my_data), "\n")
#> Number of rows: 150
cat("Number of columns:", ncol(my_data), "\n")
#> Number of columns: 5
# Count missing values in each column
colSums(is.na(my_data))
#> Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
#>            0            0            0            0            0

.