1

library(ISLR)
x = 2:9
x
## [1] 2 3 4 5 6 7 8 9
length(x)
## [1] 8

Similar to lab1 the : produces a range within a variable and the length function prints the length of a variable.

2

y = seq(2, 9, length = 8)
y
## [1] 2 3 4 5 6 7 8 9

seq works the same but is a little more customizable with the by and to params

3

x = rnorm(20)
x
##  [1]  0.39281316 -0.27261836  0.69410644 -1.09267511 -0.20169529  1.26589064
##  [7] -0.18746347  0.66429020 -1.27324936  2.25019213 -1.11859866 -0.30282257
## [13]  0.54077262  0.07415775 -0.56735587 -0.06341961 -0.35256118  0.33483296
## [19]  0.17398340  0.40672390

4

set.seed(2021)
x = rnorm(20)
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

Setting the seed creates a consistent set of values in a generation.

5

x = seq(-pi, pi, length = 50)
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

There are several constants that can be used in R

6

x = rep(0, 10)
x
##  [1] 0 0 0 0 0 0 0 0 0 0

7

x = rep(NA, 10)
x
##  [1] NA NA NA NA NA NA NA NA NA NA

GRAPHICS

8

set.seed(2021)
x = rnorm(100)
y = rnorm(100)
plot(x,y)

The plot function can pass many params and is useful in creating visible data.

9

plot(x,y, xlab = "X-Axis", ylab = "Y-Axis", main = "Plot of X vs Y")

MATRICES AND INDEXED DATA

x = matrix(1:10, 2, 5)
x
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    3    5    7    9
## [2,]    2    4    6    8   10
x = matrix(nrow = 2, data=1:10, ncol=5)
x
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    3    5    7    9
## [2,]    2    4    6    8   10
x=t(x)
x
##      [,1] [,2]
## [1,]    1    2
## [2,]    3    4
## [3,]    5    6
## [4,]    7    8
## [5,]    9   10

10

x = matrix(1:16, 4, 4)
x
##      [,1] [,2] [,3] [,4]
## [1,]    1    5    9   13
## [2,]    2    6   10   14
## [3,]    3    7   11   15
## [4,]    4    8   12   16
x[2,3]
## [1] 10
x[1,]
## [1]  1  5  9 13
x[,4]
## [1] 13 14 15 16
x[c(1,2), c(1,2)]
##      [,1] [,2]
## [1,]    1    5
## [2,]    2    6
dim(x)
## [1] 4 4

EXPLORING DATASETS

dim(Wage)
## [1] 3000   11

Have to check the package on the side after installing to use it

12

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

13

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  
## 
quantile(Wage$age, .75)
## 75% 
##  51
table(Wage[Wage$age > quantile(Wage$age, 0.75), "maritl"])
## 
## 1. Never Married       2. Married       3. Widowed      4. Divorced 
##               34              548               10               68 
##     5. Separated 
##               14

Better way to look at the data in the set than the description. Many different ways to query the dataset. Feels a bit like SQL

14

quantile(Wage$wage, .90)
##      90% 
## 154.7036

15

table(Wage[Wage$wage > quantile(Wage$wage, .90), "education"])
## 
##       1. < HS Grad         2. HS Grad    3. Some College    4. College Grad 
##                  0                 18                 28                105 
## 5. Advanced Degree 
##                149

16

plot(Wage)

I don’t really see any normal relation between the variables and wage. This plot is also kind of hard to read for me.

17

plot(Wage$age, Wage$wage, xlab = "Age", ylab = "Wage", main = "Age vs Wage")