0% found this document useful (0 votes)
29 views

Lab3-Lists, Matrices and Arrays

1. Lists and arrays can store elements of different classes/types, unlike vectors. 2. Matrices are two dimensional arrays where all elements are of the same type, arranged in rows and columns. They can be accessed, modified, and operated on similar to lists and vectors. 3. Lists, matrices, and arrays allow flexible storage of multi-dimensional and heterogeneous data in R and elements can be accessed, modified, and new elements added using various indexing operators and functions.

Uploaded by

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

Lab3-Lists, Matrices and Arrays

1. Lists and arrays can store elements of different classes/types, unlike vectors. 2. Matrices are two dimensional arrays where all elements are of the same type, arranged in rows and columns. They can be accessed, modified, and operated on similar to lists and vectors. 3. Lists, matrices, and arrays allow flexible storage of multi-dimensional and heterogeneous data in R and elements can be accessed, modified, and new elements added using various indexing operators and functions.

Uploaded by

roliho3769
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Lists

Unlike vectors, lists can have objects/elements of different classes.


Example:
s<-list(1:3,T,F)
>s
[[1]]
[1] 1 2 3

[[2]]
[1] TRUE

[[3]]
[1] FALSE

> length(s)
[1] 3

• Accessing/subsetting the list:


List can be indexed with the help of [[]] operator. [[]] is member access.
For example:
▪ >S[[1]] ## accessing the first member of list s [1] 1 2 3
▪ > s[[1]][1] ## accessing the first element of first member of list s
[1] 1
▪ S[1] #to get the list slice containing first member
[[1]]
[1] 1 2 3
▪ Note that “[“ operator returns the object of the same class as original (which is list in this case) while “[[“ operator
extracts single elements from the list(which may not be a list itself)
• Extracting multiple elements:
“[“ operator is used to extract multiple elements.
>s[c(1,3)] #extracts the first and third element of list.
[[1]]
[1] 1 2 3
[[2]]
[1] FALSE
• Extracting nested elements(elements within elements)
• “[[“ operator is used to extract nested elements of a list.
It takes an integer sequence as argument to extract nested elements
>s[[c(1,3)]] #extracts the 3rd element of first element of list
[1]3
• Naming the elements of list:
The elements of list can be named and then those can be accessed with their names.
For example:
▪ >ls1<-list(a=1:5,b=c("red","blue"),d=c(T,F,T))
> ls1
$a
[1] 1 2 3 4 5
$b
[1] "red" "blue"
$d
[1] TRUE FALSE TRUE
• Modifying the list:
The list can be modified without reloading the elements. You can modify a particular element without disturbing the
other elements.
For example:
Let us say we want to modify the 4th element of $a to 6 i.e 4 to be replaced by 6.
▪ >ls1$a[4]<-6
>ls1$a
[1] 1 2 3 6 5

$b
[1] "red" "blue"

$d
[1] TRUE FALSE TRUE

Exercise:
Create a list named ls. The list ls contains following 6 vectors with their values as follows :
• Rollno- 1:4
• First name- Ravi,Om,Ajay,Shiv
• Last name-Dev,Gandhi,Pande,Rao
• Subject-AE,DS,ML,OS
• Marks-35,40,38,02
• Result –P,P,P,F.
##Task To be performed:
1. Print the list ls.
2. Print all the independent element of list ls.
3. Find the class of element of the list.
4. Print(ls[[2]][1])
5. Print(ls[[4]][4])
6. Print(ls[5])
7. It was found that the marks of Ajay were entered wrongly. The correct marks should be 45. Replace the marks of Ajay
without disturbing the other elements or without loading the complete list again.
8. 8. the subject name of OS to OE. Modify the required element only.
9. 9. It was decided that the data base should also have native place information. Ravi stays at Pune, Ajay at Mumbai, Shiv
at Nashik and Om at Nagpur. Please add this information also in the list.
10. 10.Add one more student information names Julie Gommes. The marks obtained by Julie in subject DS is 30 and the
result is P. Her native place is Hyderabad.

MATRICES
Matrices are the R objects where the elements are arranged in two dimensional formats. It contains rows and
columns. The elements are of same type. Matrices are vectors with “dimension” attribute. The dimension
attribute itself is a integer vector of length 2 (number of rows and number of columns).

Creating the matrix:


1. Matrix function:
A matrix can be created by “matrix” function as follows:
## It enters the data column wise i.e column is filled first and then row by default.
>m<-matrix(1:6,2,3)
> m
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
OR
>m<-matrix(1:6,nrow=2,ncol=3,byrow=FALSE) ## Enters column wise.
> m
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
> m<-matrix(1:6,nrow=2,ncol=3,byrow=TRUE) ## Enters row wise. Rows are
filled first.
> m
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6

>d<-c(2,5,6)
> e<-c(9,5,5)
> m<-matrix(c(d,e),2,3)
> m
[,1] [,2] [,3]
[1,] 2 6 5
[2,] 5 9 5
2. Dimension Attribute:
Matrix can be created by adding dimension attribute as follows:
>d<-1:8
> dim(d)<-c(2,4) ## Considers rows as 2 and columns as 4.
> print(d)
[,1] [,2] [,3] [,4]
[1,] 1 3 5 7
[2,] 2 4 6 8
> dim(d) ## It retrieves the assigned values of dimension.
[1] 2 4

3. Cbind and rbind function:


Matrix can be created using cbind() and rbind() function as follows:
a.cbind: The elements are placed columnwise.
>v<-c(3,5,6,6,7)
> w<-c(4,7,8,9,0)
> m<-cbind(v,w)
> m
v w
[1,] 3 4
[2,] 5 7
[3,] 6 8
[4,] 6 9
[5,] 7 0
b.rbind : The elements are placed row wise.
>m<-rbind(v,w)
> m
[,1] [,2] [,3] [,4] [,5]
v 3 5 6 6 7
w 4 7 8 9 0

2 Accessing the Matrix:


For a given matrices any one element or any one complete row or column can be extracted as follows:
>m<-matrix(1:16,4,4)
> m
[,1] [,2] [,3] [,4]
[1,] 1 5 9 13
[2,] 2 6 10 14
[3,] 3 7 11 15
[4,] 4 8 12 16
> z<-m[4,3] ## Gives 4th row 3rd column element
> z
[1] 12
> s<-m[,3] ## Gives all rows and 3rd column element
> s
[1] 9 10 11 12
> q<-m[1,] ## Gives 1st row and all column elements.
> q
[1] 1 5 9 13
3.Operations with Matrices:
>a<-matrix(1:4,2,2) ## declaring the matrix a
> a
[,1] [,2]
[1,] 1 3
[2,] 2 4
> b<-matrix(5:8,2,2) ## declaring the matrix b
> b
[,1] [,2]
[1,] 5 7
[2,] 6 8
> c<-a+b ## adding two matrices
> c
[,1] [,2]
[1,] 6 10
[2,] 8 12
>d<-a-b ## subtracting two matrices
> d
[,1] [,2]
[1,] -4 -4
[2,] -4 -4
>e<-a*b ## Element by element multiplication
> e
[,1] [,2]
[1,] 5 21
[2,] 12 32
>f<-a%*%b ## Matrix multiplication
> f
[,1] [,2]
[1,] 23 31
[2,] 34 46

>t(f) ## Gives transpose of matrix


[,1] [,2]
[1,] 23 34
[2,] 31 46
> solve(a) ## Gives inverse of matrix.
[,1] [,2]
[1,] -2 1.5
[2,] 1 -0.5

ARRAYS
Arrays are the R data objects which can store data in more than two dimensions.

Creating Arrays:
If we create an array of dimension (3, 4, 2) then it creates 2 rectangular matrices each with 3 rows and 4
columns. An array is created using the array() function. It takes vectors as input and uses the values in
the dim parameter to create an array.
> v1<-c(1,3,5)
> v2<-c(2,4)
> v3<-c(9,10,11,19)
> a1<-array(c(v1,v2,v3),dim=c(3,3,2))
> a1
, , 1

[,1] [,2] [,3]


[1,] 1 2 10
[2,] 3 4 11
[3,] 5 9 19

, , 2

[,1] [,2] [,3]


[1,] 1 2 10
[2,] 3 4 11
[3,] 5 9 19

Accessing Array:
Like matrix you can access either single element or complete row or complete column of any of the matrices.
For example:
>d<-a1[2,3,1] ## It gives 2nd row and third column element of 1st matrix
[1] 11

> f<-a1[2,2,] ## It gives 2nd row 2nd column element of both the matrices.

> h<-a1[2,,] ## It gives 2nd row all columns and all matrix data.
Array Operations:
Different arithmetic operations can be done on elements of arrays in the similar fashion as of matrices.

You might also like