Posts

Showing posts from February, 2026

Matrix Operations and Diagonal Construction in R

# Question 1 # Define matrices A and B A <- matrix ( c ( 2 , 0 , 1 , 3 ), ncol = 2 ) B <- matrix ( c ( 5 , 2 , 4 , - 1 ), ncol = 2 ) # a) A + B A_plus_B <- A + B # b) A - B A_minus_B <- A - B A B A_plus_B A_minus_B # Question 2 # Create diagonal matrix with values 4,1,2,3 D <- diag ( c ( 4 , 1 , 2 , 3 )) D # Question 3 # Generate the required 5x5 matrix M <- diag ( 3 , 5 ) M [ 2 : 5 , 1 ] <- 2 M [ 1 , 2 : 5 ] <- 1 M  In this assignment, I practiced basic matrix operations in R, including matrix addition, subtraction, and constructing matrices using the diag() function. First, I defined matrices A and B using the matrix() function. I then computed A + B and A − B to demonstrate how R performs element-wise arithmetic operations on matrices of the same dimensions. This reinforces how R handles structured data efficiently using vectorized operations. Next, I used the diag() function to construct a 4×4 diagonal matrix with the values 4, ...

Module # 5 Doing Math

  Matrix Operations in R: Determinant and Inverse The goal of this assignment is to learn how to work with matrices in R , specifically how to compute the determinant and inverse of a matrix using built-in R functions. Step 1: Creating the Matrices First, I created the two matrices provided in the assignment using the matrix() function. A <- matrix ( 1 : 100 , nrow = 10 ) B <- matrix ( 1 : 1000 , nrow = 10 ) To better understand these matrices, I checked their dimensions: dim ( A ) dim ( B ) Matrix A has dimensions 10 × 10 , so it is a square matrix . Matrix B has dimensions 10 × 100 , so it is not square . Step 2: Determinant of Matrix A Since matrix A is square, its determinant can be calculated using the det() function. det ( A ) Result: The determinant of matrix A is: 0 A determinant of 0 means that matrix A is singular , which has important consequences for finding its inverse. Step 3: Inverse of Matrix A The inverse of a matri...

Module # 4 Programming structure assignment

 # ------------------------------------------------------------ # hospital_analysis.R # Hospital patient intake dataset: plots + brief summaries # ------------------------------------------------------------ # ---- 1) Create the dataset ---- Freq <- c(0.6, 0.3, 0.4, 0.4, 0.2, 0.6, 0.3, 0.4, 0.9, 0.2) BP   <- c(103, 87, 32, 42, 59, 109, 78, 205, 135, 176) # first: bad = 1, good = 0, NA = missing First <- c(1, 1, 1, 1, 0, 0, 0, 0, NA, 1) # second: low = 0, high = 1 Second <- c(0, 0, 1, 1, 0, 0, 1, 1, 1, 1) # finaldecision: low = 0, high = 1 FinalDecision <- c(0, 1, 0, 1, 0, 1, 0, 1, 1, 1) hospital_data <- data.frame(Freq, BP, First, Second, FinalDecision) # Print the dataset print("Hospital dataset:") print(hospital_data) # ---- 2) Boxplot: Blood Pressure by Final Decision ---- # Side-by-side comparison of BP for Low vs High final decision boxplot(   BP ~ FinalDecision,   data = hospital_data,   names = c("Low Priority (0)", "High Priority (...

ABC vs. CBS: Comparing Fictional Presidential Poll Data in R

 # Assignment 3 - Fictional 2016 Presidential Poll Data # Note: This data is made up and does NOT reflect real election results # Candidate names Name <- c("Jeb", "Donald", "Ted", "Marco", "Carly", "Hillary", "Bernie") # Poll results from ABC and CBS ABC_poll <- c(4, 62, 51, 21, 2, 14, 15) CBS_poll <- c(12, 75, 43, 19, 1, 21, 19) # Combine data into a data frame poll_data <- data.frame(Name, ABC_poll, CBS_poll) # Add a column to compare differences between polls poll_data$Difference <- CBS_poll - ABC_poll # View the data print(poll_data) # Optional: Simple bar plot comparison barplot(   t(as.matrix(poll_data[, c("ABC_poll", "CBS_poll")])),   beside = TRUE,   names.arg = poll_data$Name,   col = c("gray70", "gray40"),   legend.text = c("ABC Poll", "CBS Poll"),   main = "Fictional 2016 Presidential Poll Results",   ylab = "Poll Per...