Posts

Showing posts from February, 2026

R Object: S3 vs. S4

For this week's assignment, I went and used a dataset I am familiar with, which is mtcars. This dataset contains information about 32 cars. After loading the dataset using the data() function, I tested to see if generic functions and OO systems applied to it.  After checking the class of mtcars using class(mtcars), R returned "data.frame", which in R is implemented using the S3 object system, which in turn makes mtcars an S3 object. Since it's an S3 object, other generic functions can also be applied to it, such as summary() and print() The main difference between S3 and S4 is the structure and formality. S3 is more informal and flexible; it allows for objects to be assigned a class without a strict definition. On the other hand, S4 is more formal, and it requires explicit class definitions using the setClass() function and object creation using new(). Overall, we can see that mtcars is an S3 object, which we can see by generic functions being applied to it; S4 would ...

Module 6: Doing Math Part 2

In this module, we are tasked with performing math with different matrices.  In step 1, we are tasked with making the 2 following matrices and performing A+B and A-B. A = matrix(c(2,0,1,3), ncol=2) B = matrix(c(5,2,4,-1), ncol=2) > A+B [,1] [,2] [1,] 7 5 [2,] 2 2 > A-B [,1] [,2] [1,] -3 -3 [2,] -2 4 After Running the code we get the following outputs for A+B and A-B. For step 2 we are tasked with building a matrix of size for with the following in a diagonal, 4,1,2,3 using the diag() function. We can use the following code to create this matrix using the diag() function. diag(c(4,1,2,3)) > diag(c(4,1,2,3)) [,1] [,2] [,3] [,4] [1,] 4 0 0 0 [2,] 0 1 0 0 [3,] 0 0 2 0 [4,] 0 0 0 3 After running the diag() function with the given numbers we in fact get a matrix with the numbers 4,1,2,3 in a diagonal like the instructions require. In step 3 we are tasked with generating a matrix of 5 with a diago...

Module 5: Doing Math

For this assingment we are tasked with solving the inverse of a matrix using the given values. As the hint stated, we can start with the following for step 1: Step 1: In this step, we are creating the matrices. A <- matrix(1:100, nrow=10)   B <- matrix(1:1000, nrow=10) Step 2: After creating the matrices, we can use the dim function to check their shape. > dim(A) [1] 10 10 > dim(B) [1] 10 100 After running this function, we can see that matrix A is a square 10x10 and matrix B is not becasue its 10x100. Step 3: Next, we can use the det() to find the determinant of matrix A  > det(A) [1] 0 After running the det() we can see that the result is 0 which means the matrix is singular which has no inverse. Step 4: I attempted to double check and try finding the inverse using the solve() function > solve(A) Error in solve.default(A) : Lapack routine dgesv: system is exactly singular: U[6,6] = 0 Step 5: I also attempted to double check matrix B even though is not ...

Module 4: Programming Structue

Image
  We can see in the first image is a boxplot that compares the patient's blood pressures based on the assessment made by the first doctor, which was categorized as good or bad. The plot shows that patients who were rated as having a higher assessment generally have more variable blood pressure compared to those who were rated as good.  The histogram in the second image displays the overall distribution of the blood pressure values across all the assessed patients. Most of the patients fall within a safe range of blood pressure, while some have very high readings. These higher assessments may need closer medical care.  Together, the boxplot and the histogram provide a good summary of both the different assessments by the first doctor as well as the overall pattern in blood pressure measurement. The R code used for this assignment and to generate these visualizations has been posted on the GitHub readme, and the link will be attached. https://github.com/jonathangonzalezz/r-...