Posts

Assignment #10: Building Your Own R Package

The Friedman package is designed to help users clean, analyze, and model airline delay data. It is intended for students and analysts working with transportation datasets who want a simple and reusable way to explore delay patterns and trends. The package will include key functions such as clean_delay_data() for preparing datasets, summarize_delays() for calculating summary statistics, plot_delay_trends() for visualizing delay patterns, and predict_delays() for building basic predictive models. These functions support a complete workflow from data preparation to analysis. The DESCRIPTION file was structured to clearly define the package and its dependencies. The version 0.0.0.9000 indicates the package is still in development. The Authors@R field identifies the creator, while Depends specifies the required R version. The Imports field includes packages like dplyr and rlang for data manipulation. The CC0 license was chosen to allow open academic use, and the URL and BugReports fields li...

Assignment #9: Visualization in R – Base Graphics, Lattice, and ggplot2

Image
# Load dataset from Rdatasets collection data("iris", package = "datasets") head(iris) # Base R Graphics # Scatter plot plot(iris$Sepal.Length, iris$Petal.Length,      main = "Base R: Sepal Length vs Petal Length",      xlab = "Sepal Length",      ylab = "Petal Length",      col = as.numeric(iris$Species),      pch = 19) legend("topleft",        legend = levels(iris$Species),        col = 1:3,        pch = 19) # Histogram hist(iris$Sepal.Width,      main = "Base R: Distribution of Sepal Width",      xlab = "Sepal Width",      col = "lightblue",      border = "white") # Lattice Graphics library(lattice) # Conditional scatter plot xyplot(Petal.Length ~ Sepal.Length | Species,        data = iris,        main = "Lattice: Petal Length vs Sepal Length by Species...

Module # 8 Input/Output, string manipulation and plyr package

> # Load package > library(plyr) > > # Step 1: Import the dataset > students <- read.table("Assignment 6 Dataset.txt", header = TRUE, sep = ",") > > # Mean Grade using Sex as the category > students_gendered_mean <- ddply(students, "Sex", transform, + Grade.Average = mean(Grade)) > > # View result > students_gendered_mean Name Age Sex Grade Grade.Average 1 Lauri 21 Female 90 86.9375 2 Leonie 21 Female 91 86.9375 3 Sherlyn 22 Female 85 86.9375 4 Mikaela 20 Female 69 86.9375 5 Aiko 24 Female 97 86.9375 6 Tiffaney 21 Female 78 86.9375 7 Corina 23 Female 81 86.9375 8 Petronila 23 Female 98 86.9375 9 Alecia 20 Female 87 86.9375 10 Shemika 23 Female 97 86.9375 11 Fallon 22 Female 90 86.9375 12 Deloris 21 Female 67 86.9375 13 Randee 23 Female 91 ...

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...