Lab: Introduction to R#

Basic Commands#

Assigning#

x <- c(1,3,2,5)
x
  1. 1
  2. 3
  3. 2
  4. 5

Concatenation#

x = c(1,6,2)
x
y = c(1,4,3)
  1. 1
  2. 6
  3. 2

Addition#

length(x)
length(y)
x + y
3
3
  1. 2
  2. 10
  3. 5

Managing namespace#

ls()
rm(x, y)
ls()
  1. 'x'
  2. 'y'

It’s also possible to remove all objects at once:

rm(list=ls())

Matrices#

?matrix
x = matrix(data=c(1,2,3,4), nrow=2, ncol=2)
x
A matrix: 2 × 2 of type dbl
13
24
x = matrix(c(1,2,3,4), 2, 2)
 matrix(c(1,2,3,4), 2, 2, byrow=TRUE)
A matrix: 2 × 2 of type dbl
12
34
sqrt(x)
x^2
A matrix: 2 × 2 of type dbl
1.0000001.732051
1.4142142.000000
A matrix: 2 × 2 of type dbl
1 9
416
x = rnorm(50)
y = x + rnorm(50, mean=50, sd=.1)
cor(x, y)
0.995246200573129
set.seed(1303)
rnorm(50) 
  1. -1.14397631447974
  2. 1.34212936561501
  3. 2.18539047574276
  4. 0.536392517923731
  5. 0.0631929664685468
  6. 0.502234482468979
  7. -0.000416724686432643
  8. 0.565819840539162
  9. -0.572522688962623
  10. -1.11022500727696
  11. -0.0486871233624514
  12. -0.695656217619366
  13. 0.828917480303335
  14. 0.206652855081802
  15. -0.235674509102427
  16. -0.556310491381104
  17. -0.364754357080585
  18. 0.862355034263622
  19. -0.63077153536771
  20. 0.313602125215739
  21. -0.931495317661393
  22. 0.823867618473952
  23. 0.523370702077482
  24. 0.706921411979056
  25. 0.420204325601679
  26. -0.269052154682033
  27. -1.51031729990999
  28. -0.69021247657504
  29. -0.143471952443572
  30. -1.0135274099044
  31. 1.57327373614751
  32. 0.0127465054882014
  33. 0.872647049887217
  34. 0.422066190530336
  35. -0.0188157916578866
  36. 2.61574896890584
  37. -0.693140174826871
  38. -0.266321780991085
  39. -0.720636441231524
  40. 1.36773420645149
  41. 0.264007332160512
  42. 0.632186807367191
  43. -1.33065098578719
  44. 0.0268888182209596
  45. 1.0406363207788
  46. 1.31202379854711
  47. -0.0300020766733214
  48. -0.250025712488174
  49. 0.0234144856913592
  50. 1.65987065574227
set.seed(3)
y = rnorm(100)
mean(y)
var(y)
sqrt(var(y))
sd(y)
0.0110355710943715
0.732867501277449
0.856076808047881
0.856076808047881

Graphics#

x = rnorm(100)
y = rnorm(100)
plot(x, y)
plot(x, y, xlab="this is the x-axis",
ylab="this is the y-axis", main="Plot of X vs Y")
../../_images/Ch2-statlearn-lab_20_0.png ../../_images/Ch2-statlearn-lab_20_1.png

Saving figures#

pdf("Figure.pdf")
plot(x, y, col="green")
dev.off()
pdf: 2
x = seq(1,10)
x
x = 1:10
x
x = seq(-pi, pi, length=50)
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
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)
fa = (f - t(f))/2
contour(x, y, fa, nlevels=15)
../../_images/Ch2-statlearn-lab_24_0.png ../../_images/Ch2-statlearn-lab_24_1.png
image(x, y, fa)
persp(x, y, fa)
persp(x, y, fa, theta=30)
persp(x, y, fa, theta=30, phi=20)
persp(x, y, fa, theta=30, phi=70)
persp(x, y, fa, theta=30, phi=40)
../../_images/Ch2-statlearn-lab_25_0.png ../../_images/Ch2-statlearn-lab_25_1.png ../../_images/Ch2-statlearn-lab_25_2.png ../../_images/Ch2-statlearn-lab_25_3.png ../../_images/Ch2-statlearn-lab_25_4.png ../../_images/Ch2-statlearn-lab_25_5.png

Indexing Data#

A = matrix(1:16, 4, 4)
A[2,3]
10
A[c(1, 3), c(2, 4)]
A[1:3, 2:4]
A[1:2,]
A[,1:2]
A[1,]
  1. 1
  2. 5
  3. 9
  4. 13
A[-c(1,3),]
A[-c(1,3), -c(1,3,4)]
A matrix: 2 × 4 of type int
261014
481216
  1. 6
  2. 8
dim(A)
  1. 4
  2. 4

Loading Data#

Auto = read.table("Auto.data")
Auto = read.table("Auto.data", header=T, na.strings="?")
Auto = read.csv("Auto.csv", header=T, na.strings="?")
dim(Auto)
Auto[1:4,]
Auto = na.omit(Auto)
dim(Auto)
  1. 392
  2. 9
names(Auto)
  1. 'mpg'
  2. 'cylinders'
  3. 'displacement'
  4. 'horsepower'
  5. 'weight'
  6. 'acceleration'
  7. 'year'
  8. 'origin'
  9. 'name'

Additional Graphical and Numerical Summaries#

plot(cylinders, mpg)
Error in plot(cylinders, mpg): object 'cylinders' not found
Traceback:

1. plot(cylinders, mpg)
plot(Auto$cylinders, Auto$mpg)
attach(Auto)
plot(cylinders, mpg)
../../_images/Ch2-statlearn-lab_41_0.png ../../_images/Ch2-statlearn-lab_41_1.png
cylinders = as.factor(cylinders)
plot(cylinders, mpg)
plot(cylinders, mpg, col="red")
plot(cylinders, mpg, col="red", varwidth=T)
plot(cylinders, mpg, col="red",
varwidth=T, horizontal=T)
plot(cylinders, mpg, col="red",
varwidth=T, xlab="cylinders", ylab="MPG")
../../_images/Ch2-statlearn-lab_43_0.png ../../_images/Ch2-statlearn-lab_43_1.png ../../_images/Ch2-statlearn-lab_43_2.png ../../_images/Ch2-statlearn-lab_43_3.png ../../_images/Ch2-statlearn-lab_43_4.png
hist(mpg)
hist(mpg, col=2)
hist(mpg, col=2, breaks=15)
../../_images/Ch2-statlearn-lab_44_0.png ../../_images/Ch2-statlearn-lab_44_1.png ../../_images/Ch2-statlearn-lab_44_2.png
pairs(Auto)
pairs(~ mpg + displacement + horsepower + weight + acceleration, Auto)
../../_images/Ch2-statlearn-lab_45_0.png ../../_images/Ch2-statlearn-lab_45_1.png
plot(horsepower, mpg)
identify(horsepower, mpg, name) 
summary(Auto)
      mpg          cylinders      displacement     horsepower        weight    
 Min.   : 9.00   Min.   :3.000   Min.   : 68.0   Min.   : 46.0   Min.   :1613  
 1st Qu.:17.00   1st Qu.:4.000   1st Qu.:105.0   1st Qu.: 75.0   1st Qu.:2225  
 Median :22.75   Median :4.000   Median :151.0   Median : 93.5   Median :2804  
 Mean   :23.45   Mean   :5.472   Mean   :194.4   Mean   :104.5   Mean   :2978  
 3rd Qu.:29.00   3rd Qu.:8.000   3rd Qu.:275.8   3rd Qu.:126.0   3rd Qu.:3615  
 Max.   :46.60   Max.   :8.000   Max.   :455.0   Max.   :230.0   Max.   :5140  
                                                                               
  acceleration        year           origin                      name    
 Min.   : 8.00   Min.   :70.00   Min.   :1.000   amc matador       :  5  
 1st Qu.:13.78   1st Qu.:73.00   1st Qu.:1.000   ford pinto        :  5  
 Median :15.50   Median :76.00   Median :1.000   toyota corolla    :  5  
 Mean   :15.54   Mean   :75.98   Mean   :1.577   amc gremlin       :  4  
 3rd Qu.:17.02   3rd Qu.:79.00   3rd Qu.:2.000   amc hornet        :  4  
 Max.   :24.80   Max.   :82.00   Max.   :3.000   chevrolet chevette:  4  
                                                 (Other)           :365  
summary(mpg)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   9.00   17.00   22.75   23.45   29.00   46.60