Contents

R Introduction memo

This is an exercising memo for R introduction in ISLR2

Setup

R introduction memo

  • dev.off(): indidates to R that we are done creating the plot.
  • image(): color-coded contour plot
  • persp(): three-dimensional plot
  • index of matrix: A[ row index, column index].
    • no index for the columns or rows indicates all columns or rows (省略すると全項目指定)。
    • negative sing - in th index tells R to keep all rows or colulmns.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
> A <- matrix(1:16, 4, 4)
> A
     [,1] [,2] [,3] [,4]
[1,]    1    5    9   13
[2,]    2    6   10   14
[3,]    3    7   11   15
[4,]    4    8   12   16

> A[c(1, 3), c(2,4)]
     [,1] [,2]
[1,]    5   13
[2,]    7   15

> c(1,3)
[1] 1 3

> A[3,]
[1]  3  7 11 15

> A[,3]
[1]  9 10 11 12
> A[-c(1,3),]
     [,1] [,2] [,3] [,4]
[1,]    2    6   10   14
[2,]    4    8   12   16

> A[-c(1,3),-c(1,3,4)]
[1] 6 8

> dim(A)
[1] 4 4

If cannot find variables from object, add object name before $ as delimiter.

1
2
3
4
5
> names(Auto)
[1] "mpg"          "cylinders"    "displacement" "horsepower"   "weight"      
[6] "acceleration" "year"         "origin"       "name" 

> plot(Auto$cylinders, Auto$mpg)

Or call attach()

1
2
> attach(Auto)
> plot(cylinders, mpg)

as.factor() function converts quantitative values into qualitative values.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 cylinders
  [1] 8 8 8 8 8 8 8 8 8 8 8 8 8 8 4 6 6 6 4 4 4 4 4 4 6 8 8 8 8 4 4 4 4 6 6 6 6
 [38] 6 8 8 8 8 8 8 8 6 4 6 6 4 4 4 4 4 4 4 4 4 4 4 4 4 8 8 8 8 8 8 8 8 8 3 8 8
 [75] 8 8 4 4 4 4 4 4 4 4 4 8 8 8 8 8 8 8 8 8 8 8 8 6 6 6 6 6 4 8 8 8 8 6 4 4 4
[112] 3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8 8 8 8 6 6 6 6 4 4 4 4 6 6 6 6 4 4 4 4 4 8 4 6 6 8 8 8 8 4 4 4 4 4 8
[223] 8 8 8 6 6 6 6 8 8 8 8 4 4 4 4 4 4 4 4 6 4 3 4 4 4 4 4 8 8 8 6 6 6 4 6 6 6
[260] 6 6 6 8 6 8 8 4 4 4 4 4 4 4 4 5 6 4 6 4 4 6 6 4 6 6 8 8 8 8 8 8 8 8 4 4 4
[297] 4 5 8 4 8 4 4 4 4 4 6 6 4 4 4 4 4 4 4 4 6 4 4 4 4 4 4 4 4 4 4 5 4 4 4 4 4
[334] 6 3 4 4 4 4 4 4 6 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 6 6 6 6 8 6 6 4 4 4
[371] 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 6 6 4 6 4 4 4 4 4 4 4 4
Levels: 3 4 5 6 8
> 

If the variable plotted on the x-axis is qualitative, then boxplots will automatically generated by the plot() function.

1
2
3
4
5
> jpeg("~/boxplot.jpeg")
> plot(cylinders, mpg, col = "red", varwidth = T, horizontal = T, xlab = "cylinders", ylab = "mpg")
> dev.off()
quartz 
     2 

../../../images/omsa/boxplot.jpeg

pairs() create a scatterplot matrix.

1
>pairs(Auto)

../../../images/omsa/scatterplots.jpeg

identify() provides useful method on interactive mode on plot.

1
2
> plot(horsepower, mpg)
> identify(horsepower, mpg, name)

Then click the points, then hit Escape will cause R to print the values.

summary(Auto) provides metrics like median, 1st/3rd Qu.

For finishing,

1
2
> savehistory()
> q()

we can load history by loadhistory()