ISLR :: 2.3 Lab: Introduction to R

Published by onesixx on


2.3.1 Basic Commands

R 은 operation을 수행하기 위해 functions을 사용한다. 
To run a function called funcname, we type funcname(input1, input2),
where the inputs (or arguments) input1 and input2 tell R how to run the function.

A function는 여러개의 input을 가질수 있다. 
For example, to create a vector of numbers, we use the function c() (for concatenate). Any numbers inside the parentheses are joined together. The following command instructs R to join together the numbers 1, 3, 2, and 5, and to save them as a vector named x. When we type x, it gives us back the vector.



2.3.2 Graphics

We will now create some more sophisticated plots. The contour() function produces a contour plot in order to represent three-dimensional data;
it is like a topographical map. It takes three arguments:
1. A vector of the x values (the first dimension),
2. A vector of the y values (the second dimension), and
3. A matrix whose elements correspond to the z value (the third dimension)
for each pair of (x,y) coordinates.
As with the plot() function, there are many other inputs that can be used
to fine-tune the output of the contour() function. To learn more about
these, take a look at the help file by typing ?contour.

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

The image() function works the same way as contour(), except that it
produces a color-coded plot whose colors depend on the z value. This is
known as a heatmap, and is sometimes used to plot temperature in weather

forecasts. Alternatively, persp() can be used to produce a three-dimensional

plot. The arguments theta and phi control the angles at which the plot is
> 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)

x=seq(-pi ,pi ,length =50)
f=outer(x,y,function (x,y)cos(y)/(1+x^2))
contour (x,y,f)
contour (x,y,f,nlevels =45, add=T)
contour (x,y,fa,nlevels =15)
persp(x,y,fa ,theta =30)
persp(x,y,fa ,theta =30, phi =20)

persp3D(x,y,fa ,theta =30, phi =70)
persp3D(x,y,fa ,theta =30, phi =40)

plot3d(x, y, fa)
subid <- currentSubscene3d()




2.3.3 Indexing Data


A=matrix (1:16 ,4 ,4)

> A[c(1,3) ,c(2,4) ]

A[1:3 ,2:4]

A[1:2 ,]

A[ ,1:2]

A[-c(1,3) ,]

2.3.4 Loading Data


Auto=read.table (“ “)

2.3.5 Additional Graphical and Numerical Summaries





Categories: book:ISLR


Blog Owner

Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x