Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

R Programming - Lecture - 24-06-2023

The document discusses various matrix operations and logical operations in R. It covers how to create and manipulate matrices, handle missing data, use logical operators for comparisons, understand truth tables, and apply control structures like if/else statements and loops. Matrix operations covered include accessing elements, transpose, multiplication, addition/subtraction. Logical operators allow comparisons and control flow. Truth tables demonstrate logical operations. If/else statements and loops like for/while control program flow.

Uploaded by

Azuyi Xr
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

R Programming - Lecture - 24-06-2023

The document discusses various matrix operations and logical operations in R. It covers how to create and manipulate matrices, handle missing data, use logical operators for comparisons, understand truth tables, and apply control structures like if/else statements and loops. Matrix operations covered include accessing elements, transpose, multiplication, addition/subtraction. Logical operators allow comparisons and control flow. Truth tables demonstrate logical operations. If/else statements and loops like for/while control program flow.

Uploaded by

Azuyi Xr
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Basics of Calculations

Matrix Operations, Missing Data, Logical Operators, Truth Table and


Control Structures
Matrix Operations
In R, a 4 × 2-matrix
> x <- matrix( nrow=4, ncol=2, data=c(1,2,3,4,5,6,7,8) )
>x
Properties of Matrix
We can get specific properties of a matrix:
> dim(x) # tells the
[1] 4 2 dimension of matrix
> nrow(x) # tells
[1] 4 the number of rows
> ncol(x) # tells
[1] 2 the number of columns
Properties of Matrix
> mode(x) # Informs the type or storage
mode of an object, e.g.,
numerical, logical etc.

attributes provides all the attributes of an object


> attributes(x) #Informs the dimension of matrix
Matrix Operations
Assigning a specified number to all matrix elements:
> x <- matrix( nrow=4, ncol=2, data=2 )
>x
Construction of a diagonal matrix,
here the identity matrix of a
dimension 2:
> d <- diag(1, nrow=2, ncol=2)
>d
Matrix Operations
Transpose of a matrix X: X’
> x <- matrix(nrow=4, ncol=2, data=1:8, byrow=T )
>x

> xt <- t(x)


> xt
Matrix Operations
Multiplication of a matrix with a constant
> x <- matrix(nrow=4, ncol=2, data=1:8, byrow=T )
>x

> 4*x
Matrix Operations
Matrix multiplication: operator %*%
Consider the multiplication of X’ with X
> xtx <- t(x) %*% x
> xtx
Cross product of a matrix X, X’X, with a function crossprod
> xtx2 <- crossprod(x)
> xtx2
Note: Command crossprod()executes the
multiplication faster than the
conventional method with t(x)%*%x
Matrix Operations
Addition and subtraction of matrices (of same dimensions) can be
executed with the usual operators + and -
> x <- matrix(nrow=4, ncol=2, data=1:8, byrow=T)
>x
> 4*x
> x + 4*x
> X – 4*x
Matrix Operations
Access to rows, columns or submatrices:
> x <- matrix( nrow=5, ncol=3, byrow=T, data=1:15)
>x
> x[3,]
> x[,2]
> x[4:5, 2:3]
Matrix Operations
Inverse of a matrix:
solve() finds the inverse of a positive definite matrix
> y<- matrix( nrow=2, ncol=2, byrow=T, data=c(84,100,100,120))
>y
> solve(y)
Missing Data
R represents missing observations through the data value NA
We can detect missing values using is.na
> x <- NA # assign NA to variable x
> is.na(x) # is it missing?
Now try a vector to know if any value is missing?
> x <- c(11, NA, 13)
> is.na(x)
Missing Data
How to work with missing data?
> x <- c(11,NA,13) # vector
> mean(x)
> mean(x, na.rm = TRUE) # NAs can be removed

The null object, called NULL, is returned by some functions and


expressions.
Note that NA and NULL are not the same.
NA is a placeholder for something that exists but is missing.
NULL stands for something that never existed at all.
Logical Operators & Comparisons
The following table shows the operations and functions for logical
comparisons (True or False).
TRUE and FALSE are reserved words denoting logical constants.
Operator Executions
> Greater than
>= Greater than or equal
< Less than
<= Less than or equal to
== Exactly equal to
!= Not equal to
! Negation (Not)
&, && And
|, || or
Logical Operators & Comparisons
The shorter form performs element-wise comparisons in
almost the same way as arithmetic operators.
• The longer form evaluates left to right examining only the
first element of each vector. Evaluation proceeds only until
the result is determined.
• The longer form is appropriate for programming control-flow
and typically preferred in if clauses (conditional).
Logical Operators & Comparisons
TRUE and FALSE are reserved words denoting logical constants
Operator Executions
xor() Either…or (exclusive)
isTRUE(x) Test if x is TRUE
TRUE true
FALSE false
Logical Operators & Comparisons
Logical Operators & Comparisons
Truth Tables

Example of Standard logical operations


Statement 1 :: (x) Statement 2 :: (y) Outcome :: x and y Outcome :: x or y
True True True True
True False False True
False True False True
False False False False
Truth Tables
Control Structure in R

Control structures in R

Allow you to control the flow of execution of a series of R


expressions. Basically, control structures allow you to put some “logic”
into your R code, rather than just always executing the same R code
every time.

Control structures allow you to respond to inputs or to features of the


data and execute different R expressions accordingly.
Control Structure in R

Commonly used control structures are:


if and else: testing a condition and acting on it
for: execute a loop a fixed number of times
while: execute a loop while a condition is true
repeat: execute an infinite loop #not recommended
break: break the execution of a loop
next: skip an iteration of loop
Return: functions to do some processing and return back the result
if and else

Syntax
if (condition) {executes commands if condition is TRUE}
if (condition) {executes commands if condition is TRUE}
else { executes commands if condition is FALSE }

Please note:
• The condition in this control statement may not be vector valued
and if so, only the first element of the vector is used.

• The condition may be a complex expression where the logical


operators "and" (&&) and "or" (||) can be used.
if and else

Example:
> x <- 5
if ( x==3 ) { x <- x-1 } else { x <- 2*x }
Interpretation:
• If x = 3, then execute x = x – 1.
• If x ≠ 3, then execute x = 2*x.
In this case, x = 5, so x ≠ 3. Thus x = 2*5
>x
if and else

> x <- 3
> if ( x==3 ) { x <- x-1 } else { x <- 2*x }
Interpretation:
• If x = 3, then execute x = x – 1.
• If x ≠ 3, then execute x = 2*x.

In this case, x = 3, so x = 3 – 1
>x

You might also like