Introduction - 15 Feb 204

R Studio Interface

Window 1: Command Window

Window 2: Console Window

Window 3: Environment / History Window

Window 4: Graphics Window

Intro to S-Language

Note Data is always in vector notation using c(...) (combine / concatenate)

W=c("Welcome to SPQT Lab")

Code and Outputs

Welcome Script

# Welcome
w = c("Welcome", " to SPQT Lab")
w

h = "Hello"
print(h)
> # Welcome
> w = c("Welcome", " to SPQT Lab")
> w
[1] "Welcome"      " to SPQT Lab"
> 
> h = "Hello"
> print(h)
[1] "Hello"
> 

Scalar Algebra

Basic

# Scalar algebra
a = 5
a

b = 10
b

r1 = a + b
r1

r2 = a - b
r2

r3 = a * b
r3

r4 = a / b
r4

r5 = a ** b;
r5

r6 = a ^ b
r6

r7 = a %% b
r7
> # Scalar algebra
> a = 5
> a
[1] 5
> 
> b = 10
> b
[1] 10
> 
> r1 = a + b
> r1
[1] 15
> 
> r2 = a - b
> r2
[1] -5
> 
> r3 = a * b
> r3
[1] 50
> 
> r4 = a / b
> r4
[1] 0.5
> 
> r5 = a ** b;
> r5
[1] 9765625
> 
> r6 = a ^ b
> r6
[1] 9765625
> 
> r7 = a %% b
> r7
[1] 5
> 

Associative

# Scalar Algebra - Association

a1 = 10
a1

a2 = 15
a2

a3 = 6
a3

r1 = a1 * (a2 + a3)
r1

r2 = a2 * (a3 + a1)
r2

r3 = a3 * (a1 + a2)
r3

r4 = a1 + (a2 - a3)
r4

r5 = a3 - (a1 * a2)
r5

r6 = a1 / (a2 * a3)
r6

r7 = a2 * (a3 / a1)
r7
> # Scalar Algebra - Association
> 
> a1 = 10
> a1
[1] 10
> 
> a2 = 15
> a2
[1] 15
> 
> a3 = 6
> a3
[1] 6
> 
> r1 = a1 * (a2 + a3)
> r1
[1] 210
> 
> r2 = a2 * (a3 + a1)
> r2
[1] 240
> 
> r3 = a3 * (a1 + a2)
> r3
[1] 150
> 
> r4 = a1 + (a2 - a3)
> r4
[1] 19
> 
> r5 = a3 - (a1 * a2)
> r5
[1] -144
> 
> r6 = a1 / (a2 * a3)
> r6
[1] 0.1111111
> 
> r7 = a2 * (a3 / a1)
> r7
[1] 9
> 

Vector Algebra

Creating Vector

# Create vector and its algebraic operations

x1 = c(-3, -6, 11, 17, 26, 35, 41)
x1

x2 = c(11, 14, 26, 35, 51, 60, 72)
x2

n1 = length(x1)
n1

n2 = length(x2)
n2

R1 = x1 + x2
R1

R2 = x2 - x1
R2

R3 = x1 * x2
R3

R4 = x2 / x1
R4

R5 = x1 ^ 5
R5

R6 = x2 ^ (1/2)
R6

R7 = x1 %% x2
R7

R8 = x1 * 0.9
R8

x3 = c(1, 2, 3)
x3

x4 = c(1, 2, 3, 4, 5, 6)
x4

R9 = x3 + x4
R9
> # Create vector and its algebraic operations
> 
> x1 = c(-3, -6, 11, 17, 26, 35, 41)
> x1
[1] -3 -6 11 17 26 35 41
> 
> x2 = c(11, 14, 26, 35, 51, 60, 72)
> x2
[1] 11 14 26 35 51 60 72
> 
> n1 = length(x1)
> n1
[1] 7
> 
> n2 = length(x2)
> n2
[1] 7
> 
> R1 = x1 + x2
> R1
[1]   8   8  37  52  77  95 113
> 
> R2 = x2 - x1
> R2
[1] 14 20 15 18 25 25 31
> 
> R3 = x1 * x2
> R3
[1]  -33  -84  286  595 1326 2100 2952
> 
> R4 = x2 / x1
> R4
[1] -3.666667 -2.333333  2.363636  2.058824  1.961538  1.714286  1.756098
> 
> R5 = x1 ^ 5
> R5
[1]      -243     -7776    161051   1419857  11881376  52521875 115856201
> 
> R6 = x2 ^ (1/2)
> R6
[1] 3.316625 3.741657 5.099020 5.916080 7.141428 7.745967 8.485281
> 
> R7 = x1 %% x2
> R7
[1]  8  8 11 17 26 35 41
> 
> R8 = x1 * 0.9
> R8
[1] -2.7 -5.4  9.9 15.3 23.4 31.5 36.9
> 
> x3 = c(1, 2, 3)
> x3
[1] 1 2 3
> 
> x4 = c(1, 2, 3, 4, 5, 6)
> x4
[1] 1 2 3 4 5 6
> 
> R9 = x3 + x4
> R9
[1] 2 4 6 5 7 9
> 

Sequences

y11 = c(1:500)
y11

y12 = c(501:1000)
y12

r1 = y11 + y12
r1

r2 = y12 - y11
r2

r3 = (y11 ^ 2) + (y12 ^ (1/2))
r3

r14 = (y11 ^ (1/3)) + (y12 ^ 3)
r14

r15 = y11 * y12
r15

r16 = (y11/10) * (y12/100)
r16

Matrices

Creation

# Create matrix and operations

A = matrix(c(1,3,-6,7), nrow=2, ncol=2, byrow=TRUE, dimnames = list(m1=c("R1", "R2"), c("C1", "C2")))
A

rownames(A) = c("", "")
rownames(A)

colnames(A) = c("", "")
colnames(A)

print(A)

R_s = rowSums(A)
R_s

C_s = colSums(A)
C_s

T_r = sum(R_s)
T_r

T_c = sum(C_s)
T_c
> # Create matrix and operations
> 
> A = matrix(c(1,3,-6,7), nrow=2, ncol=2, byrow=TRUE, dimnames = list(m1=c("R1", "R2"), c("C1", "C2")))
> A
    
m1   C1 C2
  R1  1  3
  R2 -6  7
> 
> rownames(A) = c("", "")
> rownames(A)
[1] "" ""
> 
> colnames(A) = c("", "")
> colnames(A)
[1] "" ""
> 
> print(A)
  
m1     
    1 3
   -6 7
> 
> R_s = rowSums(A)
> R_s
    
4 1 
> 
> C_s = colSums(A)
> C_s
      
-5 10 
> 
> T_r = sum(R_s)
> T_r
[1] 5
> 
> T_c = sum(C_s)
> T_c
[1] 5
> 

Use of Graph window

Histogram of graph

graph = c(10, 50, 120, 300, 4, 11)
graph

hist(graph)