p42

Basic Commands

#getwd()
#setwd("../chapter02/")
# http://faculty.marshall.usc.edu/gareth-james/ISL/bios.html

x <- c(1,3,2,5) # Vector
x
## [1] 1 3 2 5
x = c(1,6,2)
y = c(1,4,3)
length(x)
## [1] 3
length(y)
## [1] 3

List and Remove objects

ls() # List all objects
## [1] "x" "y"
rm(list = ls()) # Remove all objects

Matrix

#?matrix
x = matrix(data=c(1,2,3,4), nrow = 2, ncol = 2)
x
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
x = matrix(c(1,2,3,4), 2, 2)
x
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
matrix(c(1,2,3,4), 2, 2, byrow = TRUE)
##      [,1] [,2]
## [1,]    1    2
## [2,]    3    4

Square Root

sqrt(x)
##          [,1]     [,2]
## [1,] 1.000000 1.732051
## [2,] 1.414214 2.000000

Correlation

x = rnorm(50)
y = x + rnorm(50, mean = 50, sd = .1)
y
##  [1] 50.61508 49.78288 49.98360 52.24826 50.72951 48.92897 49.82495 50.03336
##  [9] 49.90008 51.32371 50.38523 47.90603 50.79670 48.46852 50.07275 50.36893
## [17] 50.89561 49.58554 50.64008 50.51077 51.49055 50.69971 50.47124 52.45478
## [25] 49.99722 50.48574 49.22556 50.69357 49.63148 51.42444 50.24742 50.47353
## [33] 49.77522 49.64214 49.09333 50.51206 49.58210 50.76035 50.76565 50.50143
## [41] 50.16230 50.69265 48.19904 52.20604 51.39416 52.11487 49.33432 49.65721
## [49] 49.06231 47.82384
cor(x,y)
## [1] 0.9951339
set.seed(1303) # Produce same random results by initializing the seed.
rnorm(50) # First 50
##  [1] -1.1439763145  1.3421293656  2.1853904757  0.5363925179  0.0631929665
##  [6]  0.5022344825 -0.0004167247  0.5658198405 -0.5725226890 -1.1102250073
## [11] -0.0486871234 -0.6956562176  0.8289174803  0.2066528551 -0.2356745091
## [16] -0.5563104914 -0.3647543571  0.8623550343 -0.6307715354  0.3136021252
## [21] -0.9314953177  0.8238676185  0.5233707021  0.7069214120  0.4202043256
## [26] -0.2690521547 -1.5103172999 -0.6902124766 -0.1434719524 -1.0135274099
## [31]  1.5732737361  0.0127465055  0.8726470499  0.4220661905 -0.0188157917
## [36]  2.6157489689 -0.6931401748 -0.2663217810 -0.7206364412  1.3677342065
## [41]  0.2640073322  0.6321868074 -1.3306509858  0.0268888182  1.0406363208
## [46]  1.3120237985 -0.0300020767 -0.2500257125  0.0234144857  1.6598706557
rnorm(50) # Next 50
##  [1]  1.00185188  0.26300143 -0.02835910 -0.55625904 -0.11956114 -1.03629594
##  [7] -0.65663801  0.53071490  0.11239650 -2.07756129  0.42047788  0.34127692
## [13] -1.11146959  0.84377453 -0.85525778  2.24788116 -1.37211474  0.93599500
## [19]  0.54973757  0.51758744 -0.56054669 -0.63876770 -0.06500831  0.37530956
## [25]  1.30692614 -0.61058086  0.32282993  1.75126495  1.55928971  0.64713105
## [31] -1.93202340 -0.96938200  1.00148882  0.15220012 -0.04515586 -0.50296757
## [37] -0.25911284  1.01738122 -1.72582568  0.93284077  0.02782077  1.58725296
## [43]  0.23574669 -0.21068373 -0.16983068  0.76280099  0.43017948  1.37181976
## [49]  1.57143594  0.13737399
set.seed(3) # Matches book
y=rnorm(100)
mean(y)
## [1] 0.01103557
var(y)
## [1] 0.7328675
sqrt(var(y)) # sd()
## [1] 0.8560768
sd(y)
## [1] 0.8560768

Graphics

x=rnorm(100)
y=rnorm(100)

plot(x,y, xlab = "x-axis", ylab = "y-axis", main = "Title")

Save chart as PDF

pdf("Figure.pdf") # Filename
plot(x,y,col="green")
dev.off()
## quartz_off_screen 
##                 2

Sequences

x = seq(1,10)
x
##  [1]  1  2  3  4  5  6  7  8  9 10
x = 1:10
x
##  [1]  1  2  3  4  5  6  7  8  9 10
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

Contour Plot

y=x
f=outer(x,y,function(x,y) cos(y) / (1+x^2))
contour(x,y,f)
contour(x,y,f,nlevels = 45, add = T)