Posts

Building an R Package for Student Data Analysis: Final Project

GitHub:  https://github.com/nbrown022/gradeTools-package.git  For this project, I created an R package designed to perform basic data analysis tasks on student data. The goal of the package is to provide simple, easy-to-use functions that can clean data, calculate averages, generate summary statistics, visualize values, and identify maximum scores. The package includes several functions that each serve a specific purpose. The clean_data() function removes missing values from a dataset, which is important for ensuring accurate analysis. The calc_average() function calculates the mean of a numeric vector while ignoring missing values. The summary_stats() function provides a quick statistical overview of a dataset, including measures such as minimum, maximum, median, and quartiles. The max_score() function returns the highest value in a dataset, which can be useful for identifying top performance. Finally, the plot_grades() function creates a simple scatter plot to visualiz...

Assignment 12

Image
  In this assignment, I used R Markdown to create a document that combines written text, R code, and output in one place. R Markdown is useful because it allows for reproducible analysis, meaning the results automatically update whenever the code is changed. The introduction and narrative sections were written using Markdown, which is a simple formatting language that helps structure text clearly within the document. I also included both inline and displayed mathematical expressions using LaTeX. The inline expression ($\alpha + \beta = \gamma$) appears within a sentence, while the displayed equation ($y = mx + b$) is centered on its own line, demonstrating how R Markdown can present mathematical concepts effectively. The Summary Statistics Code: The first code chunk loads the built-in mtcars dataset and uses the summary() function to generate descriptive statistics. This includes values such as the minimum, maximum, mean, and median for each variable. The output is automatically ...

Assignment 11: Debugging and Defensive Programming in R

Image
  The tukey.outlier function identifies outliers in a numeric vector using the Tukey method. It calculates the first (Q1) and third (Q3) quartiles, computes the interquartile range (IQR), and then flags values below Q1 – 1.5*IQR or above Q3 + 1.5*IQR as outliers. The function returns a logical vector indicating which elements are outliers. The tukey_multiple function determines which rows of a numeric matrix are outliers in all columns . It begins with defensive checks to ensure the input is a numeric matrix, which helps prevent errors during execution. The function initializes a matrix of TRUE values with the same dimensions as x . It then loops through each column, applying the tukey.outlier function. The element-wise AND operator & ensures that only rows that are outliers in every column remain marked as TRUE . The function is tested on a 10×5 random numeric matrix generated with rnorm . The set.seed ensures reproducibility. Running tukey_multiple on the test matrix...

Assignment #10: Building Your Own R Package

Image
  The plot_summary() function is used to automatically generate visualizations for a dataset. It begins by loading the ggplot2 package, which is used for creating graphs. The function then separates the dataset into numeric and categorical variables using sapply() with is.numeric and is.factor . This allows the function to treat each type of data appropriately. For numeric variables, a loop is used to create histograms that display the distribution of values. For categorical variables, another loop creates bar charts to show the frequency of each category. Each plot is printed using print(p) , which ensures that the graphs appear immediately in the RStudio Plots pane without needing additional commands. The interactive_chart() function creates an interactive scatter plot between two selected variables. It loads both ggplot2 and plotly , where ggplot2 is used to build the initial plot and plotly adds interactivity. The function takes a dataset and two column names as inputs,...

Module 9: Visualization in R - Base Graphic, Lattice, and ggplot2

Image
  The Base R graphics provide a simple and direct way to create visualization. The plot() function creates a scatter plot showing the relationship between weight and fuel efficiency. The hist() function displays the distribution of horsepower. These functions are straightforward but require manual adjustments for styling. Lattice graphics are useful for creating grouped or conditioned plots. The xyplot() function creates multiple scatter plots based on the number of cylinders, allowing for easy comparison. The bwplot() function shows how horsepower varies across cylinder groups. Lattice uses formulas, making it efficient for grouped data visualization. The ggplot2 graphics uses a layered approach based on the grammer of graphics. The ggplot() function builds plots layer by layer. geom_point() adds data points, while geom_smooth() adds a regression line. Faceting allows us to split the histogram by cylinder groups. This system is highly customizable and produces professional-q...

Module 8 Input/ Output, String Manipulation and pylr package

Image
  First, the required package plyr is installed and loaded. This package allows grouped operations on datasets. The dataset is then imported using read.table() with file.choose() , which opens a prompt to select the file from the computer. Next, the ddply() function groups the dataset by the Sex variable and calculates the mean of the Grade column for each group. A new variable called Grade.Average is created containing the calculated means. Finally, the results are written to a text file using write.table() so they can be saved outside of R. The line y <- ddply(x, "Sex", transform, Grade.Average = mean(Grade)) calculates the average grade grouped by the Sex category. The ddply() function separates the dataset by the values in the Sex column and calculates the mean of the Grade column for each group. The result is stored in a new variable called y , and a new column called Grade.Average is added to display the calculated mean values. The command write.table(y,...

Module 7 R Object: S3 vs. S4 Assignment

Image
  I used the built-in dataset mtcars to answer all the questions. I used the head() function to display the first six rows, so we see the structure of the data. This will help us understand what type of object we are working with before applying the functions. The summary() function is a generic function that will detect that mtcars is a data frame. The output that it gives us will provide a descriptive statistic (min, max, mean, quartiles) for each variable. I had an error about the "figure margins too large" and that is because the plot window was too small. I had to use the par() function to reset the plot margins to default size. The plot() function creates a graph of miles per gallon values. The graph shows the results after using the plot() function. I used the class(mtcars) to make sure the object is "data.frame" and the isS4() function to check whether the object is S4. Since the output returned FALSE, it confirms that mtcars uses the S3 system. Next, I use ...