Posts

Assignment 12

Image
For this assignment, I learned that R Markdown is a useful tool for combining writing, code, and math in one document. I practiced using Markdown syntax to create headings and paragraphs, and I also learned how to include LaTeX math expressions both inline and as displayed equations. This helped me understand how R Markdown can take a simple source file and turn it into a clean, professional-looking report. I also learned how code chunks and narrative sections work together in R Markdown. The code chunks allow analysis and plots to be generated directly in the document, which makes everything more organized and reduces errors from copying results manually. One challenge I faced was making sure the formatting was correct, especially with code chunks and symbols, because small mistakes can stop the document from knitting. Overall, this assignment showed me how R Markdown supports reproducible analysis and is more efficient than writing plain reports. r-programming-assignments/README.md ...

Assignment 11

Image
For this assignment, I debugged a function that was supposed to identify rows in a numeric matrix where every column is flagged as a Tukey outlier. When I tested the original function on a sample matrix, I got this error: Error in outliers[, j] && tukey.outlier(x[, j]) : 'length = 10' in coercion to 'logical(1)' . This happened because the function used && inside the loop. In R, && is not vectorized, so it only checks the first value of each logical vector instead of comparing all elements. Since outliers[, j] and tukey.outlier(x[, j]) are both logical vectors of length 10, using && caused R to try forcing that full vector into a single logical value. I fixed the bug by replacing && with & , which performs element-wise logical comparison across the whole vector. After that change, the corrected function ran successfully and returned a logical vector of length 10 with no error. I also added defensive programming checks t...

Assignment 10

Image
 For this assignment, I created my first R package called Friedman , which is designed to provide simple tools for data visualization and basic analysis. The goal of this package is to make working with data easier, especially for students or beginners who may find R overwhelming at first. Instead of writing long lines of code, users can quickly generate visuals and explore datasets using simple functions. One of the main functions included is plot_summary() , which creates a scatter plot using ggplot2 with minimal input. Overall, the package focuses on simplicity, efficiency, and making data exploration more accessible. The DESCRIPTION file plays an important role in defining the structure and purpose of the package. I included key fields such as the package name, version (0.0.0.9000 for development), and a short description explaining what the package does. I listed myself as both the author and creator using the Authors@R field, and included dependencies like ggplot2 and dplyr ...

Assignment #9

Image
For this assignment, I used the mtcars dataset to compare three visualization systems in R: base graphics, lattice, and ggplot2. Using base R, I created a scatter plot of MPG vs horsepower and a histogram of MPG, which were simple and quick to generate using functions like plot() and hist() . Next, I used the lattice package to create a conditional scatter plot and a boxplot grouped by the number of cylinders, which made it easier to compare patterns across categories. Finally, with ggplot2, I created a scatter plot with a regression line and a faceted histogram, allowing for a more structured and layered approach to visualization. The main difference between the three systems is how they handle syntax and workflow. Base R is the most straightforward but can become limited when creating more complex visuals. Lattice is useful for grouped data but has less intuitive syntax. ggplot2, while slightly more complex at first, provided the most control and produced the most polished, profes...

Assignment #8

Image
  For this assignment, I worked with a dataset that included the variables Name, Age, Sex, and Grade. Assignment 6 Dataset The first step was importing the dataset into R using the read.table() function. This allowed the data from the text file to be loaded into R so it could be analyzed. Next, I used the plyr package to calculate the mean grade grouped by Sex. The ddply() function split the dataset by gender and calculated the average grade for each group. After generating the results, I saved the output to a file using the write.table() function. The next step was filtering the dataset for names that contain the letter “i.” I used the subset() function with grepl() to search for the letter in the Name column. This created a new dataset containing only those students. Finally, I exported the filtered dataset to a CSV file. This assignment showed how R can import data, perform grouped calculations, filter text values, and export results to files. macauleywilson/r-programming...

Assignment #7

Image
For this assignment, I used the built-in mtcars dataset in R. I loaded it using data("mtcars") and checked the structure with str(mtcars) . The class shows it is a data.frame , and typeof(mtcars) shows its base type is actually a list. When I ran isS4(mtcars) , it returned FALSE, meaning it is not an S4 object. So mtcars is an S3 object. A generic function is a function that works differently depending on the object’s class. Examples are summary() , print() , and plot() . When I run summary(mtcars) , R automatically uses the method made for data frames. That is method dispatch. To create an S3 example, I made a list with name, age, and GPA, then assigned it a class using class(student_s3) <- "student" . I also created a custom print method. This shows how simple and flexible S3 is. For S4, I used setClass() to formally define a class with slots and their data types. Then I created an object using new() . S4 is stricter because you must define everything firs...

Assignment #6

Image
In this module I practiced basic matrix math in R. I created two matrices, A and B, using the matrix function. Since R fills matrices by column, I checked how they looked first and then did the operations. To find A + B and A − B, I just used A + B and A - B in R. Matrix addition and subtraction are done element by element, so each number lines up with the same position in the other matrix. R printed the results correctly for both operations. Next, I used the diag() function to build a 4x4 diagonal matrix with the values 4, 1, 2, and 3 on the diagonal. A diagonal matrix has zeros everywhere except the diagonal from top left to bottom right. I created it with diag(c(4,1,2,3), 4, 4) and the output matched what the question asked for. For the last question, I had to generate a specific 5x5 matrix. I started with a diagonal matrix of 3s using diag(3, 5) . Then I edited the first column to be 3, 2, 2, 2, 2 and changed the first row to 3, 1, 1, 1, 1. After those small edits, the matrix...