An Introduction To R

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Introduction to R

Tim Dosen Matematika Lanjut

Informatika, Fakultas Teknologi Industri

September 2018
R Installation

1. Download R from https://repo.bppt.go.id/cran/ &


install
2. Download (Free) RStudio Desktop from
https://www.rstudio.com & install

Note
Choose R and RStudio versions compatible with your operating
system (Windows, (Mac)OSX, Linux).
Ensure that the Step 1 is done prior to Step 2.
RStudio: an overview

I go to RStudio...
I create a project
R script overview

1 a <− 1
2 b <− 2
3 f <− 1 : 1 0
4
5 d <− ( a + b ) ∗ a / b
6
7 nama <− ”Ahmad”
8 e <− TRUE

”←” is used to assign value (number, character, result of


operations) to a variable.
Matrix

a <− matrix ( data = c ( 1 , 2 , 3 , 4 ) ,


nrow=2, n c o l =2, byrow=TRUE)

I function name: matrix()


I parameters: data (elements), nrow (number of rows), ncol
(number of columns), byrow (whether to fill by row).
I you can also write a matrix without mentioning its
parameters’ name. Note the order of parameters; type
?matrix at console for help.

Exercise
Write matrix a in an R script, run the current line (ctrl+enter or
cmd+enter), and call it from the R console.
Matrix cont’d: accessing elements
1 a <− matrix ( c ( 1 , 2 , 3 , 4 ) ,
2 2 , 2 , TRUE)
3
4 a [1 , 2]
5 a [1 , ]
6 a [ , 1]
7 a [1:2 , ]
8 a [ , 1:2]
9 diag ( a )

I Line 4 accesses a1,2 , line 5 accesses all elements of a at row 1,


line 7 accesses rows 1 and 2 of a, line 8 accesses the diagonal
elements of a.

Exercise
What is the output of a[-1,] and a[,-2]?
Matrix cont’d: accessing elements

1 a <− matrix ( c ( 1 , 2 , 3 , 4 ) , c2 , 2 , TRUE)


2 colnames ( a ) <− c ( ” s a t u ” , ” dua ” , ” t i g a ” )
3 rownames ( a ) <− c ( ” s a t u ” , ” dua ” , ” t i g a ” )
4
5
6 a [ ” s a t u ” , ” dua ” ]
7 a [ , ” satu ” ]
8 a [ c ( ” s a t u ” , ” dua ” ) , ]

I Lines 2 and 3 gives names to columns and rows, respectively.


I Lines 4 to 8 accessing, correspondingly.
Matrix cont’d: accessing elements

1 a <− matrix ( c ( 1 , 2 , 3 , 4 ) , c2 , 2 , TRUE)


2
3 which ( a [ , 1 ] > 2 )
4 which ( a [ 2 , ] < 5 )

I Lines 3 says: given the elements of column 1 of a, which


row(s) has element(s) greater than 2?
I Lines 4 says: given the elements of rows 2 of a, which
column(s) has element(s) smaller than 5?

Exercise
What a[a > 3] <- 2 does?
Matrix cont’d: operations

1 a <− matrix ( c ( 1 , 2 , 3 , 4 ) , 2 , 2 , TRUE)


2 b <− matrix ( c ( 4 , 6 , 2 , 7 ) , 2 , 2 , TRUE)
3 a + b
4 b − a
5 2 ∗ a
6 e <− a / 3
7 d <− a %∗% b

I Line 5 is a scalar multiplication and line 7 is a matrix


multiplication.

Exercise
Why a[-1,] %*% b[-1,] doesn’t work? What the error says?
Matrix cont’d: operations

1 a <− matrix ( c ( 1 , 2 , 3 , 4 ) , 2 , 2 , TRUE)


2 b <− matrix ( c ( 4 , 6 , 2 , 7 ) , 2 , 2 , TRUE)
3
4 d <− cbind ( a [ , 1 ] , b [ , 2 ] )
5 e <− r b i n d ( a [ 1 , ] , b [ 2 , ] )

I cbind() binds column 1 of a with column 2 of b.


I rbind() binds row 1 of a with row 2 of b
Matrix cont’d: determinant, etc.

1 a <− matrix ( c ( 1 , 2 , 3 , 4 ) , 2 , 2 , TRUE)


2 b <− matrix ( 1 : 9 , 3 , 3 , TRUE)
3
4 t(a)
5 det ( a )
6 solve (a)

I Line 3 transposes matrix a


I Line 4 computes the determinant of a
I Line 5 computes the inverse of a

Exercise
Why solve(b) won’t work? What the error says?
Matrix cont’d: eigenvalues(vectors)

1 a <− matrix ( c ( 1 , 2 , 3 , 4 ) , 2 , 2 , TRUE)


2
3 eigen ( a )

I Line 3 computes the eigenvalues (and vectors) of a


Exercise

1. Check ?sample
2. Sample 100 numbers in the range of [1,1000], without
replacement, and assign those to a variable named values.
3. Create a 10 × 10 matrix G , with elements from values, filling
by row.
4. Do the following tasks.
4.1 Create a 10 × 10 matrix H, which is a transpose of G .
4.2 Define a matrix J, which is an addition of G and H.
4.3 Compute the determinant of G , H, and J.
4.4 Create a matrix K , which is a combination of the first 5
columns of G and J
4.5 Compute G · G −1 , what do you see?

You might also like