Week5:
a) ImplementRScripttoperformvariousoperationsonmatrices
Source Code:
Sample matri
ces
,M
B <- matrix(c(5, 3,15, 6), ncol = 2, byrow = TRUE)
B
Output
#A #B
[, 1] [, 2] [, 1] [, 2]
[1, ] 10 8 [1, ] 5 3
[2, ] 5 12 [2, ] 15 6
A+B
#Addition of A and B
[, 1] [, 2]
[1, ] 15 11
[2, ] 20 18
A-B
#Substraction of A and B
[, 1] [, 2]
[1, ] 5 5
[2, ] -10 6
Transpose a matrix in R
============================
To find the transpose of a matrix in R you just need to use the t function as
follows:
t(A)
#Transpose of A
[, 1] [, 2]
[1, ] 10 5
[2, ] 8 12
t(B)
#Transpose of B
[, 1] [, 2]
[1, ] 5 15
[2, ] 3 6
Matrix multiplication in R
============================
There are different types of matrix multiplications:
by a scalar, element-wise multiplication, matricial multiplication, exterior and
Kronecker product.
Multiplication by a scalar
--------------------------------
In order to multiply or divide a matrix by a scalar you can make use of the * or /
operators, respectively:
2*A
Output
[, 1] [, 2]
[1, ] 20 16
[2, ] 10 24
A/2
Output
[, 1] [, 2]
[1, ] 5.0 4
[2, ] 2.5 6
Element-wise multiplication
----------------------------
The element-wise multiplication of two matrices of the same dimensions can also
be computed with the * operator.
The output will be a matrix of the same dimensions of the original matrices.
A*B
Element-wise multiplication of A and B
[, 1] [, 2]
[1, ] 50 24
[2, ] 75 72
Power of a matrix in R
There is no a built-in function in base R to calculate the power of a matrix, so we
will provide two different alternatives.
On the one hand, you can make use of the %^% operator of the expm package as
follows:
# install.packages("expm")
library(expm)
A %^% 2
Power of A
[, 1] [, 2]
[1, ] 140 176
[2, ] 110 184
On the other hand the matrixcalc package provides the matrix.power function:
# install.packages("matrixcalc")
library(matrixcalc)
matrix.power(A, 2)
Power of A
[, 1] [, 2]
[1, ] 140 176
[2, ] 110 184
Determinant of a matrix in R
The determinant of a matrix
�
A, generally denoted by
∣
�
∣
∣A∣, is a scalar value that encodes some properties of the matrix.
In R you can make use of the det function to calculate it.
det(A) # 80
det(B) # -15
Inverse of a matrix in R
In order to calculate the inverse of a matrix in R you can make use of the solve
function.
M <- solve(A)
M
Inverse of A
[, 1] [, 2]
[1, ] 0.1500 -0.100
[2, ] -0.0625 0.125
Rank of a matrix in R
The rank of a matrix is maximum number of columns (rows) that are linearly
independent.
qr(A)$rank # 2
qr(B)$rank # 2
# Equivalent to:
library(Matrix)
rankMatrix(A)[1] # 2
b) ImplementRScripttoextractthedatafromdataframes.
Source Code:
# R program to illustrate dataframe
# A vector which is a character vector
Name = c("Amiya", "Raj", "Asish")
# A vector which is a character vector
Language = c("R", "Python", "Java")
# A vector which is a numeric vector
Age = c(22, 25, 45)
# To create dataframe use data.frame command and
# then pass each of the vectors
# we have created as arguments
# to the function data.frame()
df = data.frame(Name, Language, Age)
print(df)
Output:
=========
Name Language Age
1 Amiya R 22
2 Raj Python 25
3 Asish Java 45
# R program to illustrate operation on a data frame
# Creating a dataframe
df = data.frame(
"Name" = c("Amiya", "Raj", "Asish"),
"Language" = c("R", "Python", "Java"),
"Age" = c(22, 25, 45)
)
cat("Before adding row\n")
print(df)
# Add a new row using rbind()
newDf = rbind(df, data.frame(Name = "Sandeep",
Language = "C",
Age = 23
))
cat("After Added a row\n")
print(newDf)
Output:
Before adding row
Name Language Age
1 Amiya R 22
2 Raj Python 25
3 Asish Java 45
After Added a row
Name Language Age
1 Amiya R 22
2 Raj Python 25
3 Asish Java 45
4 Sandeep C 23
Adding extra columns: We can add extra column using the command cbind(). The syntax
for this is given below,
newDF = cbind(df, the entries for the new column you have to add )
df = Original data frame
c) WriteRscripttodisplayfilecontents.
Source Code:
# R program to read a csv file
# Get content into a data frame
data <- read.csv("CSVFileExample.csv",
header = FALSE, sep = "\t")
# Printing content of Text File
print(data)
Output:
V1 V2 V3
1 100 AB ab
2 200 CD cd
3 300 EF ef
4 400 GH gh
5 500 IJ ij
d) WriteRscripttocopyfilecontentsfromonefiletoanother
Source Code:
# Get list of file names
my_files<- list.files("C:/Desktop/User/Murali/my directory A")
my_files
# [1] "file no 1.docx" "file no 2.txt" "file no 3.xlsx"
# Copy files
file.copy(from = paste0("C:/Desktop/User/Murali/my directory A/", my_files),
to = paste0("C:/Desktop/User/Murali/my directory B/", my_files))
Output:
# [1] TRUE TRUETRUE