Posts

Assignment #10: Building Your Own R Package

Github Link: https://github.com/rayhankhan-svg/Friedman I developed the framework for the Friedman R package for Module #10. I gained a better understanding of a R package's structure and the function of the DESCRIPTION file thanks to this assignment. R uses the machine-readable metadata found in the DESCRIPTION file to install, document, and verify the package. The package name, title, version, author details, description, dependencies, license, and additional optional elements like URL and BugReports are all included. My package's goal is to assist users in organizing, summarizing, and visualizing workplace and educational data in R. Students, novice analysts, and researchers looking for basic tools for data cleansing and producing understandable summaries and graphs are the target audience for my package. A function for cleaning datasets, a method for creating summary statistics, and a function for creating ggplot2 visualizations are some of the main features I intend to cre...

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

Image
Github Link: https://github.com/rayhankhan-svg/r-programming-assignments R Code:  data <- read.csv(file.choose(), stringsAsFactors = FALSE) head(data) str(data) data$rownames <- as.numeric(data$rownames) data$education <- as.numeric(data$education) plot(data$rownames, data$education,      main = "Base: Education by Observation",      xlab = "Observation",      ylab = "Education",      col = "blue") hist(data$education,      main = "Base: Distribution of Education",      xlab = "Education",      col = "lightgreen") library(lattice) xyplot(education ~ rownames | gender,        data = data,        main = "Lattice: Education by Observation and Gender") bwplot(education ~ job,        data = data,        main = "Lattice: Education by Job Type") library(ggplot2) ggplot(data, aes(x = rownames, ...

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

Github Link:  https://github.com/rayhankhan-svg/r-programming-assignments R Code:  # Step 1: Import dataset x <- read.table(file.choose(), header = TRUE, sep = ",") # View dataset x # Step 2: Install/load plyr and calculate mean Grade by Sex install.packages("plyr") library(plyr) y <- ddply(x, "Sex", transform, Grade.Average = mean(Grade)) # View result y # Step 3: Write the mean result to a CSV-style file write.table(y, "Sorted_Average", sep = ",", row.names = FALSE) # Step 4: Filter names containing the letter i or I newx <- subset(x, grepl("[iI]", x$Name)) # View filtered dataset newx # Step 5: Write the filtered subset to a CSV-style file write.table(newx, "DataSubset", sep = ",", row.names = FALSE) Output:  > # Step 1: Import dataset > x <- read.table(file.choose(), header = TRUE, sep = ",") >  > # View dataset > x         Name Age    Sex Grade 1       Raul  25   Ma...

Module # 7 R Object: S3 vs. S4 assignment

Github Link:  https://github.com/rayhankhan-svg/r-programming-assignments R Code:  # Step 1: Load a dataset data(mtcars) head(mtcars, 6) # Step 2: Check if generic functions can be used on this dataset class(mtcars) typeof(mtcars) isS4(mtcars) # Generic functions examples summary(mtcars) plot(mtcars$mpg, mtcars$hp) # What is a generic function? mean # Step 3: Create an S3 example s3 <- list(name = "Myself", age = 29, GPA = 3.5) class(s3) <- "student" s3 class(s3) typeof(s3) isS4(s3) # Step 4: Create an S4 example setClass("student",          slots = list(            name = "character",            age  = "numeric",            GPA  = "numeric"          )) s4 <- new("student", name = "Myself", age = 29, GPA = 3.5) s4 class(s4) typeof(s4) isS4(s4) Output:  > # Step 1: Load a dataset > data(mtcars) > head(mtcars...

Module # 6 Doing math in R part 2

Github Link: https://github.com/rayhankhan-svg/r-programming-assignments R Code:  # 1) Matrices A and B A <- matrix(c(2,0,1,3), ncol=2) B <- matrix(c(5,2,4,-1), ncol=2) A B # a) A + B A_plus_B <- A + B A_plus_B # b) A - B A_minus_B <- A - B A_minus_B # 2) diag() matrix size 4 with diagonal values 4,1,2,3 D <- diag(c(4,1,2,3)) D # 3) Generate the 5x5 matrix using diag() M <- diag(3, 5)      # start with 3s on the diagonal M[1, 2:5] <- 1       # first row (cols 2-5) become 1 M[2:5, 1] <- 2       # first column (rows 2-5) become 2 M Output:  > # 1) Matrices A and B > 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 ...

Module # 5 Doing Math

Github link:  https://github.com/rayhankhan-svg/r-programming-assignments R Code: # Create matrices A <- matrix(1:100, nrow=10) B <- matrix(1:1000, nrow=10) # Check dimensions dim(A) dim(B) # Determinants detA <- det(A) detA # Inverses invA <- tryCatch(solve(A), error=function(e) e) invB <- tryCatch(solve(B), error=function(e) e) invA invB # Determinant of B detB <- tryCatch(det(B), error=function(e) e) detB Output:  > # 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 >  > # Determinants > detA <- det(A) > detA [1] 0 >  > # Inverses > invA <- tryCatch(solve(A), error=function(e) e) > invB <- tryCatch(solve(B), error=function(e) e) >  > invA <simpleError in solve.default(A): Lapack routine dgesv: system is exactly singular: U[3,3] = 0> > invB <simpleError in solve.def...

Module # 4 Programming structure assignment

Image
Github link:  https://github.com/rayhankhan-svg/r-programming-assignments R Code: # Create the data vectors Frequency <- c(0.6, 0.3, 0.4, 0.4, 0.2, 0.6, 0.3, 0.4, 0.9, 0.2) BloodPressure <- c(103, 87, 32, 42, 59, 109, 78, 205, 135, 176) First <- c("bad", "bad", "bad", "bad", "good", "good", "good", "good", NA, "bad") Second <- c("low", "low", "high", "high", "low", "low", "high", "high", "high", "high") FinalDecision <- c("low", "high", "low", "high", "low", "high", "low", "high", "high", "high") # Create data frame hospital_data <- data.frame(Frequency, BloodPressure, First, Second, FinalDecision) hospital_data # Create boxplot of Blood Pressure boxplot(BloodPressure,         main=...