Posts

Showing posts from February, 2026

Doing Math in R Part 2

 1.  Finding the sum and difference of these two matrices.  A = matrix(c(2,0,1,3), ncol=2) and B = matrix(c(5,2,4,-1), ncol=2) a) Find A + B First, assign the matrices to a value, then assign a new value to represent the sum of both matrices. Run that value to get the new matrix of A + B. A <- matrix(c(2,0,1,3), ncol=2) B <- matrix(c(5,2,4,-1), ncol=2) AB_sum <- A + B AB_sum Result:      [,1] [,2] [1,]    7    5 [2,]    2    2  b) Find A - B First, assign the matrices to a value, then assign a new value to represent the difference between the two matrices. Run that value to get the new matrix of A - B. A <- matrix(c(2,0,1,3), ncol=2) B <- matrix(c(5,2,4,-1), ncol=2) AB_dif <- A - B AB_dif Result:      [,1] [,2] [1,]   -3   -3 [2,]   -2    4  2.   Using the  diag()   function to build a matrix of size 4 with the follo...

Doing Math on Matrices

I used the following values to find the inverse and determinants of two matrices. A = matrix(1:100, nrow=10) B = matrix(1:1000, nrow=10) First, I assigned the matrices in R.  A <- matrix(1:100, nrow=10) B <- matrix(1:1000, nrow=10) To obtain the determinant of each matrix, I used the det function. det(A) Result:  0 det(B) Result: Error in determinant.matrix(x, logarithm = TRUE, ...) :    'x' must be a square matrix Then, to obtain the inverse of each matrix, I used the solve function. solve(A) Result:  Error in solve.default(A) :    Lapack routine dgesv: system is exactly singular: U[3,3] = 0 solve(B) Result: Error in solve.default(B) : 'a' (10 x 100) must be square Looking at the matrices, we can see that A is a square matrix because it has the same number of rows and columns. There are 100 values in the matrix; dividing that by 10 rows gives me 10 columns. This makes matrix A a 10x10 square matrix. With B, there are 1000 values in the matrix, a...

Results of Patient Blood Pressure and MD Decision

Image
Using the data collected for ten patients by a local hospital, including the f requency of their visits, blood pressure, first assessment, second assessment, and final decision, I was able to create five vectors.  Freq <- c(0.6, 0.3, 0.4, 0.4, 0.2, 0.6, 0.3, 0.4, 0.9, 0.2) bloodp <- c(103, 87, 32, 42, 59, 109, 78, 205, 135, 176) first <- c(1, 1, 1, 1, 0, 0, 0, 0, NA, 1) second <- c(0, 0, 1, 1, 0, 0, 1, 1, 1, 1) finaldecision <- c(0, 1, 0, 1, 0, 1, 0, 1, 1, 1) With these vectors, I made a side-by-side box plot comparing the low and high blood pressures, then a histogram of the frequency of each blood pressure range.  R Code: boxplot(bloodp ~ finaldecision,          main = "Patients BPs & MD’s Ratings",         names = c("Low","High"),         xlab = "Final Decision",         ylab = "Blood Pressure") hist(bloodp,      main = "Histogram of Patient's Blood Pressur...

Election Data Frame

Image
Errors in the original data set: a missing comma between Marco and Carly in Name, I fixed the spelling of Berine to Bernie, a missing comma between 62 and 51 in ABC, quotation marks around names had to be changed to " ", the variable names for ABC and CBS political poll results had spaces, which I changed to _. After fixing all the errors in the data set, I used the three variables Name, ABC_political_poll_results,  and CBS_political_poll_results to create the data frame. Since the data set is based on the 2016  presidential election, I named the data frame election_2016. Here is my corrected code in R: Name <- c("Jeb", "Donald", "Ted", "Marco", "Carly", "Hillary", "Bernie") ABC_political_poll_results <- c(4, 62, 51, 21, 2, 14, 15) CBS_political_poll_results <- c(12, 75, 43, 19, 1, 21, 19)  election_2016 <- data.frame(Name, ABC_political_poll_results, CBS_political_poll_results) election_2016 Af...