Skip to content

Instantly share code, notes, and snippets.

@abdelaziz321
Last active November 6, 2021 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdelaziz321/00214a84bb702391c71b41c056b98d48 to your computer and use it in GitHub Desktop.
Save abdelaziz321/00214a84bb702391c71b41c056b98d48 to your computer and use it in GitHub Desktop.

CH 1: Introduction To R

1- Basic Functions

# install package
> install.packages()

# load the package into the current session
> library(splines)

# update package
> update.packages()

# close the session
> q()

# more info about a function
> ?mean
> help(mean)

# store a value in a variable
> x = 6				# the value of x is 6
> assign("x", 5)	# the value of x is 5
> x <- 4			# the value of x is 4
> 3 -> x			# the value of x is 3
# كانو قاعدين يقترحوا أكتر من سينتكس وعملوهم كلهم عشان محدش يزعل

# list all current objects (variables/arrays/defined-functions..)
> ls()
> objects ()

# remove object
> rm(y, z)

# working directory
> getwd()			# print the current working directory
> setwd("D:\\work")	# navigate to the given working directory

2- Arithmetic Operators

> 5 + 2		# 7
> 5 - 2		# 3
> 5 * 2		# 10
> 2 ^ 5		# 32	// to the power
> 5 / 2		# 2.5	// division
> 5 %/% 2	# 2		// integer division
> 2 %% 5	# 1		// remainder
Example
formula
> 5 * exp(-2) * (cos(2 * pi / 6) + atan(1))		# 0.8697986

3- Logical Operators

> 1 == 1	# True
> 1 != 1	# False
> 2 < 2		# False
> 2 <= 2	# True
> T & F		# False
> T | F		# True
> is.na(NA)	# True
> is.na(1)	# False
> is.na(c(1, NA))	# False True

4- Numerical Functions

> round(1.6)	# 2
> sqrt(9)		# 3
> sqrt(c(4, 9))	# 3
> abs(c(1, -1))	# 1 1
> exp(1)		# 2.718282	// Exponential function *e to the power 1*
> log(exp(1))	# 1			// base-e logarithm
> log10(100)	# 2			// base-10 logarithm
> sin(0)		# 0
> cos(0)		# 1
> tan(0)		# 0
> asin(0)		# 0
> acos(1)		# 0
> atan(0)		# 0
> min(c(1, 2))	# 1
> max(c(1, 2))	# 2
> min(1, 2)		# 1
> max(1, 2)		# 2
> length(c(1, 3, 8)) 	# 3
> sum(c(1, 3, 8))		# 12
> ceiling(c(1.8, -1.2))	# 2 -1
> floor(c(1.8, -1.2))	# 1 -2
> trunc(c(1.8, -1.2))	# 1 -1	// trunc always rounds toward 0
> pmin(c(1, 22, 3), c(5, 6, 7))	# 1 6 3		// the	min between each element
> pmax(c(1, 22, 3), c(5, 6, 7))	# 5 22 7	// the	max between each element

5- Statistical Functions

> x = c(3, 3, 1, 5)
> mean(x)			# 3
> median(x)			# 3
> range(c(1, 3, 8))	# 1 8
> var(x)			# 2.66667
> sd(x)				# 1.632993
> quantile(x, 0.5)	# 3
> cor(x, y)

Example:

# random generation for a random sample of size 6 from the normal distribution
> x = rnorm(6)
# mean
> sum(x) / length(x) 	# 0.0883872
> mean(x)	        	# 0.0883872
# standard deviation
> sqrt(sum((x - mean(x)) ^ 2) / (length(x) - 1))	# 0.9295404
> sd(x)												# 0.9295404

6- Vectors

Types of vector:

  • numeric vector (array of numbers)

    x = c(10.4, 5.6, 3.1, 6.4)
  • character vector (array of text strings)

    x = c("Jan","Apr", "Jul", "Oct")
  • logical vector (array of TRUE / FALSE value)

    x = c(T,T,F,T)

Creating vectors using concatenate function c:

# regular c
> a = c(1, 3, 4)
> a
1 2 3

# adding names
> b = c("i", "j", "k")
> names(a) = b
> a
i j k 
1 3 4 

# specifing vector names directly
> c(x=1, y=3, z=4)
x y z
1 3 4

Creating vectors using sequence function seq:

> seq(1, 12)		# from 1 to 12 including 1 and 12
1 2 3 4 5 6 7 8 9 10 11 12
> 1:12				# the same
1 2 3 4 5 6 7 8 9 10 11 12
> seq(1, 12, 2)		# step is +2 not the default +1
1 3 5 7 9 11
> seq(-1, 1, 0.2)	# step is +0.2
-1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 1.0

# working with letters vector
> letters			# print the vector letters from "a" to "z"
> letters[10:15]	# print "6" letters
"j" "k" "l" "m" "n" "o"

Creating vectors using replicate function rep:

# repeat the vector [1, 2, 2] => 2 times
> rep(c(1, 2, 2), 2)
1 2 8 1 2 8
# repeat the generated vector [1 2 3] => 3 times
> rep(seq(1, 3), 3)
1 2 3 1 2 3 1 2 3
# repeat each element in the vector [7 6 8] => [1 2 3] times
> rep(c(7, 8, 9), c(1, 2, 3))
7 8 8 9 9 9
# repeat the generated vector [9 10 11] => [2 3 4] times
> rep(9:11, 2:4)
9  9 10 10 10 11 11 11 11

Working with paste function:

> paste(c("X","Y"), 1:10, sep="")
"X1" "Y2" "X3" "Y4" "X5" "Y6" "X7" "Y8" "X9" "Y10"
Example:

print the following output using rep and paste functions:

"X1Y1" "X2Y2" "X3Y3" "X4Y4" "X5Y5" "X6Y6" "X7Y7" "X8Y8" "X9Y9" "X10Y10"

> Xs = paste(c("X"), 1:10, sep="")
> Ys = paste(c("Y"), 1:10, sep="")
> paste(Xs, Ys, sep="")

7- Matrices two-dimensional array

# by default the matrix will be filled in a columnwise fashion
> matrix(1:12, nrow=3)
		[,1] 	[,2]	[,3]	[,4]
[1,]	1		4		7		10
[2,] 	2 		5		8		11
[3,] 	3 		6		9		12

# to fill the matrix in a rowwise fashion we add byrow=T
> x = matrix(1:12, nrow=3, byrow=T)
> x
		[,1] 	[,2]	[,3]	[,4]
[1,]	1		2		3		4
[2,] 	5 		6		7		8
[3,] 	9 		10		11		12

# using transposition function
> y = t(x)
> y
		[,1] 	[,2]	[,3]
[1,]	1		5		9
[2,] 	2 		6		10
[3,] 	3 		7		11
[4,]	4		8		12

# accessing the matrix elements
> y[2, 2]
6
> y[2,]
2 6 10
> y[,2]
5 6 7 8

# matrix info
> dim(y)
4 2
> length(y)
12
> colMeans(y)	# means of the columns
2.5  6.5 10.5
> rowMeans(y)	# means of the rows
5 6 7 8

# using dim to generate matix (not recommended)
> m = 1:9
> dim(m) = c(3,3)
> m				# the matrix will be filled in a columnwise fashion
		[,1] 	[,2]	[,3]
[1,]	1		4		7
[2,] 	2 		5		8
[3,] 	3 		6		9

> det(m)		# calculate the determinant
0

# using the diagonal function
> diag(m)
1 5 9

> sum(diag(m))	# the trace of matrix m
15

> diag(c(1, 5, 9))
		[,1] 	[,2]	[,3]
[1,]	1		0		0
[2,] 	0 		5		0
[3,] 	0 		0		9

> diag(3)		# identity matrix of order 3
		[,1] 	[,2]	[,3]
[1,]	1		0		0
[2,] 	0 		1		0
[3,] 	0 		0		1

# combining matrices by joining rows / columns 
> a = matrix(1:6, nrow=2)
> b = matrix(11:16, nrow=2)
> a
		[,1] 	[,2]	[,3]
[1,]	1		3		5
[2,] 	2 		4		6

> b
		[,1] 	[,2]	[,3]
[1,]	11		13		15
[2,] 	12 		14		16

> rbind(a, b)
		[,1] 	[,2]	[,3]
[1,]	1		3		5
[2,] 	2 		4		6
[3,]	11		13		15
[4,] 	12 		14		16

> rbind(4, 1, b, a, c(22, 23, 24))
		[,1] 	[,2]	[,3]
[1,]	4		4		4
[2,]	1		1		1
[3,]	11		13		15
[4,] 	12 		14		16
[5,]	1		3		5
[6,] 	2 		4		6
[7,]	22		23		24

> cbind(a, b)
		[,1] 	[,2]	[,3]	[,4]	[,5]	[,6]
[1,]	1		3		5		11		13		15
[2,] 	2 		4		6		12 		14		16

# math operations
> a + b		# element by element addition
> a - b		# element by element subtraction
> a * b		# element by element product
> a / b		# element by element division

# matrix multiplication
> a %*% b		# can't multiply: size (2*3) %*% (2*3)
{Error in a %*% b : non-conformable arguments}

> a %*% t(b)	# it's ok: size (2*3) %*% (3*2)
		[,1] 	[,2]
[1,]	125		134
[2,]	164		176

Working with crossproduct & outer-product

# crossproduct
> crossprod(1:4, 4:7)	# 1*4 + 2*5 + 3*6 + 4*7
60
> crossprod(1:4) # same as crossprod(1:4, 1:4)

# outer-product
> a = c(1,2, 3)
> b = c(4, 5, 6, 7)
> outer(a, b, '*')	# each matrix[i,j] element = a[i] "*" b[j]
		[,1] 	[,2]	[,3]	[,4]
[1,]	4		5		6		7
[2,] 	8 		10		12		14
[3,] 	12 		15		18		21

# we can change the '*' operator by using a defined function with 2 parameters
> anotherFunction <- function(x, y) y^x
> outer(a, b, anotherFunction)
		[,1] 	[,2]	[,3]	[,4]
[1,]	4		5		6		7
[2,] 	16 		25		36		49
[3,] 	64 		125		216		343

Calculating the eigenvalues and eigenvectors of a symmetric matrix

> m = matrix(c(10, 2, 1, 2, 20, 6, 1, 6, 30), nrow=3)
> m
		[,1] 	[,2]	[,3]
[1,]	10		2		1
[2,] 	2 		20		6
[3,] 	1 		6		30

> resultObject = eigen(m)

# to get the vector of eigenvalues
> resultObject$val
32.94503 17.44151  9.61346

# to get the matrix of eigenvectors
> resultObject$vec
            [,1]       [,2]         [,3]
[1,] -0.07662051 -0.1785530  0.980942473
[2,] -0.42900733 -0.8822029 -0.194089574
[3,] -0.90004556  0.4357028  0.009005696

Solve Function: solves the equation a %*% x = b’ for ‘x’ where b can be either a vector or a matrix.

Example:

find the value of x & y

formula
formula

we can think of it as the following:

formula
formula
formula
> A = matrix(c(1, 1, 1, -1), 2, 2, byrow=T)
> b = c(5,1)
> solve(A) %*% b
3 2

# instead of solve(A) %*% b we can use
> solve(A, b)
3 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment