Posts

Showing posts from September, 2025

Assignment 6

Image
  Assignment 6: Matrix Operations and Construction In this assignment,  I practiced creating and manipulating matrices in R. The goal was to strengthen my understanding of how R handles matrix operations such as addition, subtraction, diagonal construction, and combining blocks of matrices to form structured arrays. R Code 🔹 Task 1: Matrix Addition & Subtraction # Task 1: define A and B A <- matrix(c(2, 0, 1, 3), ncol = 2) B <- matrix(c(5, 2, 4, -1), ncol = 2) # Addition A_plus_B <- A + B print("A + B:") print(A_plus_B) # Subtraction A_minus_B <- A - B print("A - B:") print(A_minus_B) 🔹 Task 2: Create a Diagonal Matrix # Task 2: Create a Diagonal Matrix D <- diag(c(4, 1, 2, 3)) print("Diagonal Matrix D:") print(D) 🔹 Task 3: Construct a Custom 5×5 Matrix # Task 3: Construct a Custom 5x5 Matrix # Step 1: create the first row row1 <- c(3, 1, 1, 1, 1) # Step 2: create the remaining rows: # - first column all 2’s (rows 2–5) # - diagonal...

Assignment 5

Image
Assignment 5: Matrix Algebra in R For this assignment, I explored how to create and inspect matrices in R, how to compute matrix inverses and determinants, and how to handle errors when working with non-square matrices. I also reflected on issues of numeric stability and performance that arise in matrix algebra computations. R Code # 1. Create the matrices A <- matrix(1:100, nrow = 10) B <- matrix(1:1000, nrow = 10) # 2. Inspect dimensions dimA <- dim(A) # should be 10 × 10 dimB <- dim(B) # should be 10 × 100 print("Dimensions of A:") print(dimA) print("Dimensions of B:") print(dimB) # 3. Compute inverse and determinant for A invA <- solve(A) detA <- det(A) print("Inverse of A:") print(invA) print("Determinant of A:") print(detA) # 4. Handle non-square matrix B with tryCatch invB <- tryCatch(solve(B), error = function(e) e) detB <- tryCatch(det(B), error = function(e) e) print("Inverse of B attempt:") print...

Assignment 4

Image
   Assignment 4: Visualizing and Interpreting Hospital Patient Data In this assignment, I explored a small hospital dataset to visualize and interpret patient vitals, particularly blood pressure , alongside physician assessments. The goals were to practice data cleaning , handle missing values , create boxplots and histograms , and interpret trends in patient data.  R Code 1. Data Preparation and Cleaning # Define vectors Frequency     <- c(0.6, 0.3, 0.4, 0.4, 0.2, 0.6, 0.3, 0.4, 0.9, 0.2) BloodPressure <- c(103, 87, 32, 42, 59, 109, 78, 205, 135, 176) FirstAssess   <- c(1, 1, 1, 1, 0, 0, 0, 0, NA, 1)    # bad=1, good=0 SecondAssess  <- c(0, 0, 1, 1, 0, 0, 1, 1, 1, 1)    # low=0, high=1 FinalDecision <- c(0, 1, 0, 1, 0, 1, 0, 1, 1, 1)    # low=0, high=1 # Create dataframe df_hosp <- data.frame(   Frequency, BloodPressure, FirstAssess,   SecondAssess, FinalDecision, stringsAsFactors = F...

Assignment 3

Image
 Assignment 3: Analyzing 2016 data “Poll” Data in R In this assignment, I analyze a small dataset of polling results using R. The dataset compares two fictional polls, one from ABC and one from CBS, for seven political candidates. The purpose is to practice data wrangling, visualization, and interpretation with ggplot2 while also reflecting on how to properly use polling data. R Code # Step 1: Define data Name <- c("Jeb", "Donald", "Ted", "Marco", "Carly", "Hillary", "Bernie") ABC_poll   <- c(  4,      62,      51,    21,      2,        14,       15) CBS_poll   <- c( 12,      75,      43,    19,      1,        21,       19) # Step 2: Create data frame df_polls <- data.frame(Name, ABC_poll, CBS_poll) # Step 3: Inspect data structure and first few rows str(df_...

Assignment 2

  Assignment 2 - Importing Data and Function Evaluation in R For this part of the assignment, I tested the provided myMean function in RStudio, carefully recorded the errors that appeared, explained why they happened, and then corrected the code so it would successfully return the mean of the given dataset. Original Code:  assignment2 <- c(16, 18, 14, 22, 27, 17, 19, 17, 17, 22, 20, 22) myMean <- function(assignment2) {       return(sum(assignment) / length(someData))  }  Errors from Testing Error 1: > return(sum(assignment) / length(someData))  Error: object 'assignment' not found  Inside the function body, the code used assignment instead of assignment2. Since no variable named assignment exists, R could not evaluate the expression and produced this error. Error 2: > return(sum(assignment2) / length(someData))  Error: object 'someData' not found  Here, assignment2 was corrected in the sum() part, but t...