Assignment Two Answer to the question number one
# Create vectors a, b, and c
a <- c(2, 5, 6, 7)
b <- c(1, 0, 9, 8)
c <- c(6, 5, 8, 3)
# Combine vectors to form a matrix
matrix_data <- rbind(a, b, c)
# Set column names to (Mon, Tue, Wed, Thu)
colnames(matrix_data) <- c("Mon", "Tue", "Wed", "Thu")
# Set row names to (Present, Absent, On leave)
rownames(matrix_data) <- c("Present", "Absent", "On leave")
# Print the matrix
print(matrix_data)
## Mon Tue Wed Thu
## Present 2 5 6 7
## Absent 1 0 9 8
## On leave 6 5 8 3
# Calculate row sums
row_sums <- rowSums(matrix_data)
print("Row Sums:")
## [1] "Row Sums:"
print(row_sums)
## Present Absent On leave
## 20 18 22
# Calculate column sums
col_sums <- colSums(matrix_data)
print("Column Sums:")
## [1] "Column Sums:"
print(col_sums)
## Mon Tue Wed Thu
## 9 10 23 18
Answer to the question number two
# Load the required library
library(ggplot2)
# a) Scatterplot of mpg vs disp
ggplot(mtcars, aes(x = disp, y = mpg)) +
geom_point(color = "blue", size = 3) +
labs(title = "Scatterplot of mpg vs disp",
x = "Displacement (cu.in.)", y = "Miles per Gallon (mpg)")
# b) Boxplot of mpg with gear
ggplot(mtcars, aes(x = factor(gear), y = mpg)) +
geom_boxplot(fill = "lightblue") +
labs(title = "Boxplot of mpg with gear",
x = "Number of Gears", y = "Miles per Gallon (mpg)")
# c) Histogram of disp
ggplot(mtcars, aes(x = disp)) +
geom_histogram(binwidth = 50, fill = "purple", color = "white") +
labs(title = "Histogram of Displacement",
x = "Displacement (cu.in.)", y = "Frequency")