Posts

Module 12. Assignment

Image
This task was more challenging than I anticipated. I needed to do further study and troubleshooting before comprehending the functionality of R Markdown. I acquired foundational knowledge of Markdown formatting and the composition of basic LaTeX mathematics; nonetheless, the most arduous aspect was ensuring that each code segment was properly started and terminated. A single missed backtick resulted in problems while knitting. I also learnt how narrative text and code segments collaborate to produce a coherent, repeatable report. Despite requiring time and patience, the completion of this exercise significantly enhanced my comprehension of the R Markdown structure. R-CODE  --- title: "My R Markdown Primer" author: "Shanzay Khan" date: "`r Sys.Date()`" output: html_document --- ## Introduction R Markdown is a document format that lets you combine **narrative text**, **R code**, and **math formulas** in a single file with the `.Rmd` extension. Instead o...

Module 11 Debugging Assignment

Image
tukey_multiple <- function(x) {   outliers <- array(TRUE, dim = dim(x))   for (j in 1:ncol(x)) {     outliers[, j] <- outliers[, j] && tukey.outlier(x[, j])   }   outlier.vec <- vector("logical", length = nrow(x))   for (i in 1:nrow(x)) {     outlier.vec[i] <- all(outliers[i, ])   }   return(outlier.vec) } set.seed(123) test_mat <- matrix(rnorm(50), nrow = 10) tukey_multiple(test_mat) corrected_tukey <- function(x) {   outliers <- array(TRUE, dim = dim(x))   for (j in seq_len(ncol(x))) {     # FIX: use element-wise '&' instead of scalar '&&'     outliers[, j] <- outliers[, j] & tukey.outlier(x[, j])   }   outlier.vec <- logical(nrow(x))   for (i in seq_len(nrow(x))) {     outlier.vec[i] <- all(outliers[i, ])   }   outlier.vec } corrected_tukey(test_mat) https://github.com/shanzay28/r-programming-assignments/blob/...

First -R- Package

 Purpose and Scope https://github.com/shanzay28/Friedman/tree/master The Friedman package was developed for my LIS4370 R Programming course to explore how R packages are structured, documented, and shared. It is designed for students, data analysts, and beginners in R who want to understand how functions, metadata, and documentation fit together in a reusable library. This package provides simple utility functions that demonstrate clean coding, proper documentation, and reproducibility practices. Users can install it directly from GitHub and test small, practical functions for everyday data analysis tasks. Key Functions The current version (0.0.0.9000) includes four basic but well-documented functions: • hello() – Prints a friendly “Hello, world!” message confirming that the package loads correctly. • add_numbers(x, y) – Adds two numeric values and returns their sum with input validation. • calculate_mean(x, na.rm = TRUE) – Computes the mean of a numeric vector, safely handli...

Module 9. Assignment

Image
  1.How does the syntax and workflow differ between base, lattice, and ggplot2? You call plot() and hist() in Base R and specify labels, colors, and other things by yourself.Lattice has a formula interface (y ~ x | group) that makes tiny multiples simple to construct without having to write  loops by hand.ggplot2 employs a layered syntax that is consistent and may be added to across plots.The layers include data, aesthetics, geoms, and facets. 2. Which system gave you the most control or produced the most “publication‑quality” output with minimal code? Layering (geom_point() + geom_smooth()) and consistent theming in ggplot2 provide professional pictures rapidly.Lattice is an excellent way to create conditioned views with very little code. Base is the quickest rapid inspections, but it requires more manual styling to look goodfor publishing. 3. Any challenges or surprises you encountered when switching between systems The mental model evolves every time:  Base is easy to ...

Assignment 8

Image
 I learned how to load data into R, summarize it, filter it, and save the results in different file types for this project.First, I set up my working location so that R could find files quickly and save them in a neat place. After that, I used read.table() to add the information to a data frame. This helped me learn how heads and divisions work when reading real data. Next, I used the ddply() method and the plyr package to find the average grade and age for each gender. This taught me how to do grouped reports in R.  Lastly, I used the subset() and grepl() methods to practice filtering data. I got all the student names that started with "i" and saved both the names-only and full filtered results. By following these steps, I learned how to use R to automatically clean data, analyze it, and send it to a file. These are important skills for working with datasets quickly.  # R Code  setwd("C:/Users/shanz/OneDrive/Documents/Assigment 6") x <- read.table("Assignme...

Module 7. Assignment

Image
This week, I studied Object-Oriented Programming (OOP) in R and discovered that R perceives all entities as objects, including integers, vectors, data frames, and functions. The talk addressed two main systems used in R: S3 and S4. S3 is the preliminary, more accessible way that allows for the rapid addition of a class to a list and the creation of custom print methods. S4 is the more recent and systematically structured framework that has inherent validation and explicit class definitions with slots. We further examined the use of common methods such as summary(), print(), and plot() to see their distinct functionalities across different object kinds. Ultimately, I was able to create my own S3 and S4 objects, implement fundamental methods, and understand how R determines which function version to use, a process known as method dispatch. # Download Data for Mtcar data("mtcars") # Show the first few rows head(mtcars) # Describe its structure str(mtcars) # Test Generic Function...

Module 6 – Linear Algebra in R (Part 2)

Image
  # 1. Matrix Addition & Subtraction A <- matrix(c(2, 0, 1, 3), ncol = 2) B <- matrix(c(5, 2, 4, -1), ncol = 2) # Addition A_plus_B <- A + B A_plus_B # Subtraction A_minus_B <- A - B A_minus_B Explanation : Matrix addition and subtraction work element-by-element on matrices of the same size, combining or contrasting values at the same positions. This is useful for quickly aggregating or comparing structured numeric data # 2.Create a Diagonal Matrix D <- diag(c(4, 1, 2, 3)) D Explanation :  diag() places the supplied numbers along the main diagonal and fills all other entries with zeros. Diagonal matrices are commonly used for identity/scaling operations and as building blocks in linear algebra. # 3.Construct a Custom 5 × 5 Matrix M <- diag(3, 5, 5)           M[1, 2:5] <- 1               M[2:5, 1] <- 2     M Explanation :  started with a diagonal of 3’s and then...