Posts

Showing posts from February, 2026

Module # 6 Doing math in R part 2

Github Link: https://github.com/rayhankhan-svg/r-programming-assignments R Code:  # 1) Matrices A and B A <- matrix(c(2,0,1,3), ncol=2) B <- matrix(c(5,2,4,-1), ncol=2) A B # a) A + B A_plus_B <- A + B A_plus_B # b) A - B A_minus_B <- A - B A_minus_B # 2) diag() matrix size 4 with diagonal values 4,1,2,3 D <- diag(c(4,1,2,3)) D # 3) Generate the 5x5 matrix using diag() M <- diag(3, 5)      # start with 3s on the diagonal M[1, 2:5] <- 1       # first row (cols 2-5) become 1 M[2:5, 1] <- 2       # first column (rows 2-5) become 2 M Output:  > # 1) Matrices A and B > 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 ...

Module # 5 Doing Math

Github link:  https://github.com/rayhankhan-svg/r-programming-assignments R Code: # Create matrices A <- matrix(1:100, nrow=10) B <- matrix(1:1000, nrow=10) # Check dimensions dim(A) dim(B) # Determinants detA <- det(A) detA # Inverses invA <- tryCatch(solve(A), error=function(e) e) invB <- tryCatch(solve(B), error=function(e) e) invA invB # Determinant of B detB <- tryCatch(det(B), error=function(e) e) detB Output:  > # 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 >  > # Determinants > detA <- det(A) > detA [1] 0 >  > # Inverses > invA <- tryCatch(solve(A), error=function(e) e) > invB <- tryCatch(solve(B), error=function(e) e) >  > invA <simpleError in solve.default(A): Lapack routine dgesv: system is exactly singular: U[3,3] = 0> > invB <simpleError in solve.def...

Module # 4 Programming structure assignment

Image
Github link:  https://github.com/rayhankhan-svg/r-programming-assignments R Code: # Create the data 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) First <- c("bad", "bad", "bad", "bad", "good", "good", "good", "good", NA, "bad") Second <- c("low", "low", "high", "high", "low", "low", "high", "high", "high", "high") FinalDecision <- c("low", "high", "low", "high", "low", "high", "low", "high", "high", "high") # Create data frame hospital_data <- data.frame(Frequency, BloodPressure, First, Second, FinalDecision) hospital_data # Create boxplot of Blood Pressure boxplot(BloodPressure,         main=...

Module # 3 data.frame

Code Repository: https://github.com/rayhankhan-svg/r-programming-assignments/blob/main/README.md R Code:  # Create vectors Name <- c("Jeb", "Donald", "Ted", "Marco", "Carly", "Hillary", "Bernie") ABC <- c(4, 62, 51, 21, 2, 14, 15) CBS <- c(12, 75, 43, 19, 1, 21, 19) # Create data frame poll_results <- data.frame(Name, ABC, CBS) # View data frame poll_results # Compare polls poll_results$Difference <- poll_results$CBS - poll_results$ABC poll_results Output:  > # Compare polls > poll_results$Difference <- poll_results$CBS - poll_results$ABC > poll_results      Name ABC CBS Difference 1     Jeb   4  12          8 2  Donald  62  75         13 3     Ted  51  43         -8 4   Marco  21  19         -2 5   Carly   2   1...