Professional Documents
Culture Documents
An Introduction To R
An Introduction To R
An Introduction To R
September 2018
R Installation
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
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 )
Exercise
What is the output of a[-1,] and a[,-2]?
Matrix cont’d: accessing elements
Exercise
What a[a > 3] <- 2 does?
Matrix cont’d: operations
Exercise
Why a[-1,] %*% b[-1,] doesn’t work? What the error says?
Matrix cont’d: operations
Exercise
Why solve(b) won’t work? What the error says?
Matrix cont’d: eigenvalues(vectors)
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?