Posts

Showing posts from March, 2026

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