Posts

Showing posts from February, 2024

Module 7

Exploring Object-Oriented Systems in R In this assignment, I will download a dataset and explore whether generic functions, S3, and S4 object-oriented systems can be assigned to it. #### Step 1: Downloading Dataset I will use the `iris` dataset available in R. ```R # Load the iris dataset data("iris") head(iris) ``` #### Step 2: Generic Functions Assignment To determine if generic functions can be assigned to the `iris` dataset, I will attempt to assign a generic function. ```R # Define a generic function my_summary <- function(data) {   summary(data) } # Attempt to assign the generic function to the iris dataset my_summary(iris) ``` Generic functions can be assigned to the `iris` dataset since it is a standard R data frame, and generic functions can operate on any R object. #### Step 3: S3 and S4 Assignment Next, I will explore if S3 and S4 object-oriented systems can be assigned to the `iris` dataset. ```R # Attempt to assign S3 class to the iris dataset class(iris) <...

Module # 6 Doing math in R part 2

  Doing Math in R: Part 2 In this blog post, we'll delve into some matrix operations and matrix generation in R. We'll use basic functions and operations to manipulate matrices and create new ones based on specific requirements. 1. Matrix Operations Consider two matrices: \( A = \begin{pmatrix} 2 & 0 \\ 1 & 3 \end{pmatrix} \) and \( B = \begin{pmatrix} 5 & 2 \\ 4 & -1 \end{pmatrix} \) (a) Addition: To find the sum of matrices \( A \) and \( B \), we'll use the `+` operator in R. ```R A <- matrix(c(2, 0, 1, 3), ncol = 2) B <- matrix(c(5, 2, 4, -1), ncol = 2) A + B ``` The result of \( A + B \) is: \( \begin{pmatrix} 2+5 & 0+2 \\ 1+4 & 3+(-1) \end{pmatrix} = \begin{pmatrix} 7 & 2 \\ 5 & 2 \end{pmatrix} \) (b) Subtraction: To find the difference between matrices \( A \) and \( B \), we'll use the `-` operator in R. ```R A - B ``` The result of \( A - B \) is: \( \begin{pmatrix} 2-5 & 0-2 \\ 1-4 & 3-(-1) \end{pmatrix} = \begin{p...

Module 4 Programming Structure in R

 Here's how you can create side-by-side boxplots and histograms for the given data: ```R # Create vectors for the data Frequency <- c(0.6, 0.3, 0.4, 0.4, 0.2, 0.6, 0.3, 0.4, 0.9, 0.2) BP <- c(103, 87, 32, 42, 59, 109, 78, 205, 135, 176) First <- c(1, 1, 1, 1, 0, 0, 0, 0, NA, 1)  # Assuming "bad" as 1 and "good" as 0 Second <- c(0, 0, 1, 1, 0, 0, 1, 1, 1, 1)  # Assuming "low" as 0 and "high" as 1 FinalDecision <- c(0, 1, 0, 1, 0, 1, 0, 1, 1, 1)  # Assuming "low" as 0 and "high" as 1 # Combine into a data frame hospital_data <- data.frame(Frequency, BP, First, Second, FinalDecision) # Plot side-by-side boxplots par(mfrow = c(1, 2))  # Set up the plotting area boxplot(BP ~ First, data = hospital_data, xlab = "First Assessment", ylab = "Blood Pressure", main = "Blood Pressure by First Assessment") boxplot(BP ~ Second, data = hospital_data, xlab = "Second Assessment", yl...