R Markdown
##Part 1
# Generate a sequence of integers 2 - 9 using the colon command :
x <- 2:9
# Display the sequence to the screen
print(x)
## [1] 2 3 4 5 6 7 8 9
# Display the length of x to the screen
print(length(x))
## [1] 8
##Part 2
# Generate a sequence of integers 2 - 9 using the seq() function
x <- seq(from = 2, to = 9)
# Display the sequence to the screen
print(x)
## [1] 2 3 4 5 6 7 8 9
##Part 3
# Set the seed to ensure reproducibility
set.seed(2023)
# Generate a vector of 20 random normal values using the rnorm() function
x <- rnorm(n = 20)
# Display the vector
print(x)
## [1] -0.08378436 -0.98294375 -1.87506732 -0.18614466 -0.63348570 1.09079746
## [7] -0.91372727 1.00163971 -0.39926660 -0.46812305 0.32696208 -0.41274690
## [13] 0.56203647 0.66335826 -0.60289728 0.69837769 0.59584645 0.45209183
## [19] 0.89674396 0.57221651
##Part 4
# Set the seed to ensure reproducibility
set.seed(2021)
# Generate a vector of 20 random normal values
x <- rnorm(n = 20)
# Display the vector
print(x)
## [1] -0.12245998 0.55245663 0.34864950 0.35963224 0.89805369 -1.92256952
## [7] 0.26174436 0.91556637 0.01377194 1.72996316 -1.08220485 -0.27282518
## [13] 0.18199540 1.50854179 1.60447011 -1.84147561 1.62331021 0.13138902
## [19] 1.48112247 1.51331829
##Part 5
# Generate a sequence with a lower limit of -pi, an upper limit of pi, and a length of 50
x <- seq(from = -pi, to = pi, length.out = 50)
# Display the sequence
print(x)
## [1] -3.14159265 -3.01336438 -2.88513611 -2.75690784 -2.62867957 -2.50045130
## [7] -2.37222302 -2.24399475 -2.11576648 -1.98753821 -1.85930994 -1.73108167
## [13] -1.60285339 -1.47462512 -1.34639685 -1.21816858 -1.08994031 -0.96171204
## [19] -0.83348377 -0.70525549 -0.57702722 -0.44879895 -0.32057068 -0.19234241
## [25] -0.06411414 0.06411414 0.19234241 0.32057068 0.44879895 0.57702722
## [31] 0.70525549 0.83348377 0.96171204 1.08994031 1.21816858 1.34639685
## [37] 1.47462512 1.60285339 1.73108167 1.85930994 1.98753821 2.11576648
## [43] 2.24399475 2.37222302 2.50045130 2.62867957 2.75690784 2.88513611
## [49] 3.01336438 3.14159265
##Part 6
# Create a vector of 10 zeros using the rep() function
x <- rep(0, times = 10)
# Display the vector
print(x)
## [1] 0 0 0 0 0 0 0 0 0 0
##Part 7
# Generate a vector of 10 values of NA using the rep() function
x <- rep(NA, times = 10)
# Display the vector
print(x)
## [1] NA NA NA NA NA NA NA NA NA NA
##Part 8
# Set the seed to ensure reproducibility
set.seed(2021)
# Generate a random normal vector of length 100
x <- rnorm(n = 100)
# Generate a second vector of length 100 using the same seed
y <- rnorm(n = 100)
# Plot the graph of the vectors x and y using plot()
plot(x = x, y = y, main = "Random Normal Vectors", xlab = "x", ylab = "y")

##Part 9
# Plot the graph of the vectors x and y with a title and x- and y-axis labels
plot(x = x, y = y, main = "Plot of X vs Y", xlab = "x-axis", ylab = "y-axis")

##Part 10
# Construct a 4x4 matrix with the values 1-16, arranged by row
matrix <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16), nrow = 4, ncol = 4)
# Display the matrix
print(matrix)
## [,1] [,2] [,3] [,4]
## [1,] 1 5 9 13
## [2,] 2 6 10 14
## [3,] 3 7 11 15
## [4,] 4 8 12 16
# Access the value "10" using array notation
matrix[2, 2]
## [1] 6
# Display just the first row
matrix[1, ]
## [1] 1 5 9 13
# Display just the last column
matrix[ , 4]
## [1] 13 14 15 16
# Display a submatrix of just the first two rows and the first two columns
matrix[1:2, 1:2]
## [,1] [,2]
## [1,] 1 5
## [2,] 2 6
# Display the dimension of the original matrix using the dim() function
dim(matrix)
## [1] 4 4
##Part 11
#Load the library
library(ISLR)
#Get the dimensions of the wage datasets
dim(Wage)
## [1] 3000 11
##Part 12
# Load the ISLR package
suppressWarnings(library(ISLR))
# Get the Wage dataset
Wage <- Wage
# Print the structure of the Wage dataset
str(Wage)
## 'data.frame': 3000 obs. of 11 variables:
## $ year : int 2006 2004 2003 2003 2005 2008 2009 2008 2006 2004 ...
## $ age : int 18 24 45 43 50 54 44 30 41 52 ...
## $ maritl : Factor w/ 5 levels "1. Never Married",..: 1 1 2 2 4 2 2 1 1 2 ...
## $ race : Factor w/ 4 levels "1. White","2. Black",..: 1 1 1 3 1 1 4 3 2 1 ...
## $ education : Factor w/ 5 levels "1. < HS Grad",..: 1 4 3 4 2 4 3 3 3 2 ...
## $ region : Factor w/ 9 levels "1. New England",..: 2 2 2 2 2 2 2 2 2 2 ...
## $ jobclass : Factor w/ 2 levels "1. Industrial",..: 1 2 1 2 2 2 1 2 2 2 ...
## $ health : Factor w/ 2 levels "1. <=Good","2. >=Very Good": 1 2 1 2 1 2 2 1 2 2 ...
## $ health_ins: Factor w/ 2 levels "1. Yes","2. No": 2 2 1 1 1 1 1 1 1 1 ...
## $ logwage : num 4.32 4.26 4.88 5.04 4.32 ...
## $ wage : num 75 70.5 131 154.7 75 ...
##Part 13
# Load the ISLR package
suppressWarnings(library(ISLR))
# Get the Wage dataset
Wage <- Wage
# Print the summary of the Wage dataset
summary(Wage)
## year age maritl race
## Min. :2003 Min. :18.00 1. Never Married: 648 1. White:2480
## 1st Qu.:2004 1st Qu.:33.75 2. Married :2074 2. Black: 293
## Median :2006 Median :42.00 3. Widowed : 19 3. Asian: 190
## Mean :2006 Mean :42.41 4. Divorced : 204 4. Other: 37
## 3rd Qu.:2008 3rd Qu.:51.00 5. Separated : 55
## Max. :2009 Max. :80.00
##
## education region jobclass
## 1. < HS Grad :268 2. Middle Atlantic :3000 1. Industrial :1544
## 2. HS Grad :971 1. New England : 0 2. Information:1456
## 3. Some College :650 3. East North Central: 0
## 4. College Grad :685 4. West North Central: 0
## 5. Advanced Degree:426 5. South Atlantic : 0
## 6. East South Central: 0
## (Other) : 0
## health health_ins logwage wage
## 1. <=Good : 858 1. Yes:2083 Min. :3.000 Min. : 20.09
## 2. >=Very Good:2142 2. No : 917 1st Qu.:4.447 1st Qu.: 85.38
## Median :4.653 Median :104.92
## Mean :4.654 Mean :111.70
## 3rd Qu.:4.857 3rd Qu.:128.68
## Max. :5.763 Max. :318.34
##
##Part 14
# Load the ISLR package
suppressWarnings(library(ISLR))
# Get the Wage dataset
Wage <- Wage
# Calculate the 90th percentile for "wage"
quantile(Wage$wage, 0.9)
## 90%
## 154.7036
##Part 15
# Load the ISLR package
suppressWarnings(library(ISLR))
# Get the Wage dataset
Wage <- Wage
# Calculate the 90th percentile for "wage"
wage_90th_percentile <- quantile(Wage$wage, 0.9)
#using dplyr package
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
# Filter the Wage dataset to only include workers whose wage is above the 90th percentile
Wage_filtered <- Wage %>% filter(wage > wage_90th_percentile)
# Create a table to show the tabulation of education level for workers whose wage is above the 90th percentile
table(Wage_filtered$education)
##
## 1. < HS Grad 2. HS Grad 3. Some College 4. College Grad
## 0 18 28 105
## 5. Advanced Degree
## 149
##Part 16
# Load the ISLR package
suppressWarnings(library(ISLR))
# Get the Wage dataset
Wage <- Wage
# Plot the Wage dataset
plot(Wage)

##Part 17
# Load the ISLR package
suppressWarnings(library(ISLR))
# Get the Wage dataset
Wage <- Wage
# Plot the wage vs education
plot(Wage$education, Wage$wage, main = "Wage vs. Education", xlab = "Education", ylab = "Wage")
