Posts

Showing posts from February, 2026

Module # 7 R Object: S3 vs. S4 assignment

> # Module 7 > > data("mtcars") > > cat("== STEP 1: DATA ==\n") == STEP 1: DATA == > print(head(mtcars, 6)) mpg cyl disp hp drat wt qsec vs am gear carb Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 > > cat("\n== STEP 2: GENERIC FUNCTIONS CHECK ==\n") == STEP 2: GENERIC FUNCTIONS CHECK == > cat("class(mtcars): ", paste(class(mtcars), collapse = ", "), "\n", sep = "") class(mtcars): data.frame > cat("typeof(mtcars): ", typeof(mtcars), "\n", sep = "") typeof(mtcars): list...

Module # 6 Doing math in R part 2

  Answer the following questions and post your answer on your blog: 1. Consider A=matrix(c(2,0,1,3), ncol=2) and B=matrix(c(5,2,4,-1), ncol=2). a) Find A + B b) Find A - B 2. Using the  diag()  function to build a matrix of size 4 with the following values in the diagonal 4,1,2,3. 3. Generate the following matrix: ## [,1] [,2] [,3] [,4] [,5] ## [1,] 3 1 1 1 1 ## [2,] 2 3 0 0 0 ## [3,] 2 0 3 0 0 ## [4,] 2 0 0 3 0 ## [5,] 2 0 0 0 3 > # ----------------------------- > # Question 1: Matrix operations > > A <- matrix(c(2, 0, 1, 3), ncol = 2) > B <- matrix(c(5, 2, 4, -1), ncol = 2) > > A [,1] [,2] [1,] 2 1 [2,] 0 3 > B [,1] [,2] [1,] 5 4 [2,] 2 -1 > > # a) A + B > A_plus_B <- A + B > A_plus_B [,1] [,2] [1,] 7 5 [2,] 2 2 > > # b) A - B > A_minus_B <- A - B > A_minus_B [,1] [,2] [1,] -3 -3 [2,] -2 4 > > # --------------------------------------- ...

Module # 5 Doing Math

> ############################# > # Step 1: Create Matrices > > A <- matrix(1:100, nrow = 10) > B <- matrix(1:1000, nrow = 10) > > # Check dimensions > dim(A) [1] 10 10 > dim(B) [1] 10 100 > > ############################# > # Step 2: Determinant of A > > detA <- det(A) > print(detA) [1] 0 > > ############################# > # Step 3: Attempt Inverse of A > > invA <- tryCatch( + solve(A), + error = function(e) e$message + ) > print(invA) [1] "Lapack routine dgesv: system is exactly singular: U[6,6] = 0" > > ############################# > # Step 4: Determinant of B > > detB <- tryCatch( + det(B), + error = function(e) e$message + ) > print(detB) [1] "'x' must be a square matrix" > > ############################# > # Step 5: Attempt Inverse of B > > invB <- tryCatch( + solve(B), + error = function(e) e$message + ) > print...

Module # 3 data.frame

Assignment # 3  This data set is based on the presidential election during 2016, where it outlined the name of the candidate, the source of the poll (ABC vs, CBS). Discuss your result in your blog. Important note, I made up this data, so this data does not reflect what really happened in the election. > Name <- c("Jeb", “Donald”, "Ted”, “Marco” “Carly”, “Hillary”, “Berine”) > ABC political poll results <- c(4, 62 51, 21, 2, 14, 15) > CBS political poll results <- c(12, 75, 43, 19, 1, 21, 19)       This simulated dataset compares fictional polling results for seven presidential candidates from two sources: ABC and CBS. Overall, both polls identify Donald as the leading candidate, however, there are meaningful differences for several candidates. For example, Ted receives substantially higher support in the ABC poll than in the CBS poll, while Hillary and Bernie show stronger results in the CBS poll than in ABC.      These differen...