List Matrix
List Matrix
Creating List
Naming elements in a list(Assigning Names to
elements in a list)
Accessing elements in a list
Manipulating elements in a list-Addition,Deletion and
updation of elements in a list
Subsetting the list elements
Converting List to Vector:
Lists:
A list in R is a type of R object which
contains different types of elements like
- numbers, vectors, strings, and another
list within it. A list can also contain a
function or a matrix as its elements
--> Single dimensional object
with hetrogeneous data types
--> To create a list use function
list().
# Create a list containing character, complex,
double, integer and logical.
a <- list(“R-STUDIO",2+3i,10,20L,TRUE)
sales1 <-
list(10,"DataScience","Udemy",1800,"3Months")
length(sales1)#output is 5
sales1[1]
sales1[c(1,2,3)]
sales1[1:3]
sales1[-4]
sales1[c(-1,-5)]
sales1[-c(1,5)]
sales1 <-
list(10,"DataScience","Udemy",1800,"3Months")
names(sales1) <-
c("cid","cname","trainer","fee","duration")
names(sales1)
sales1[1]
sales1["cid"]
sales1$cid
sales1[3]
sales1["trainer"]
sales1$trainer
sales1[T]
sales1[c(T,F,T,F,T)]
Manipulating List Elements:
in vector we can add, delete & update elements.
a <- c(10,20,30)
print(a)
# Add a new element at the end of the vector by using position
a[4] <- 40
print(a)
# Update the 2nd element of the vector by using position
a[2] <- 2
print(a)
# Delete the 2nd element of the vector by using position
a <- a[-2]
print(a)
a <- c(10,20,30)
names(a) <- c("A","B","C")
# Add a new element at the end of the vector by using name
a["D"] <- 40
print(a)
# Update the 2nd element of the vector by using name
a["B"] <- 2
print(a
Similary in list also we can add, delete & update
elements.
sales1 <-
list(10,"DataScience",“Udemy",1800,"3Months")
names(sales1) <-
# Add a new element at the end of the list by using
c("cid","cname","trainer","fee","duration")
position
sales1[6] <- “Dubai"
sales1[6]
print(sales1)
# Update the 2nd element of the list by using
position
sales1[2] <- "DataAnalysis"
sales1[2]
print(sales1)
# Add a new element at the end of the list by
using name
sales1$Address <- “Dubai"
sales1$Address
print(sales1)
# Delete the last element of the list by using name
sales1$Address <- NULL
sales1$Address
print(sales1)
# Update the 2nd element of the list by using name
sales1$cname <- "MachineLearning"
sales1$cname
print(sales1)
Merging Lists:
==============
--> We can merge many lists into one list by using
list() function
# First create the two lists
a <- list(10,20,30,40)
b <- list("A","B","C","D")
e <- c(a[c(1,4)],b[c(1,4)])
# Merge the a and b lists
print(e)
c <- c(a,b)
f <- c(a[1:3],b[2:4])
print(c)
print(f)
typeof(c)
g <- c(a[-4],b[-1])
d <- c(a[1],b[2])
print(g)
print(d)
Converting List to Vector:
==========================
--> for doing future manipulation it is better to convert
the list into a vector.
--> Arithmetic operations can be applied on the
vectors.
--> to convert the list into vector we have to use
unlist() function.
--> unlist() takes the inputs as list and produces a
a <- list(seq(10,50,10)) # Convert the lists to vectors
b <- list(seq(60,100,10)) c <- unlist(a)
print(a) d <- unlist(b)
print(b) print(c)
print(d)
typeof(a)
typeof(c)
typeof(b)
typeof(d)
# Once observe this manipulation in vector, what will
happen
a <- c(10,20,30)
names(a) <- c("A","B","C")
a["D"] <- 40
print(a)
typeof(a)
a$E <- 50
print(a)
typeof(a)
a <- unlist(a)
# Return a list
a <- list(x=1,y=2)
a[1]
a["x"]
# Return only element in list
a[[1]]
a[["a"]]
YOU HAVE TWO LISTS, LIST1 CONTAINING NUMBERS 1
TO 3 AND LIST2 CONTAINING LETTERS "A", "B", "C".
COMBINE THESE LISTS INTO A NEW LIST COMBINED LIST
AND PRINT IT.
Answer:
list1<-list(1,2,3)
list2<-list("A","B","C")
com_list<-list(c(list1,list2))
com_list
You are given a list that contains information about employees in different departments
of a company. Each department has its own list, which includes the names and salaries
of the employees. Your task is to:
Extract the salaries of each department using unlist().
rownames<-c("Row1","Row2")
colnames<-c("col1","col2","col3","col4","col5")a<-
matrix(1:10,ncol=5,byrow=T,dimnames=list(rowna
mes,colnames))a
a <- matrix(1:12,3)
print(a)
# Access the element at 1st column and 1st row
a[1]
a[1,1]
3.Given a matrix M with integers from 1 to 9,
extract the element in
a. the second row and third column.
b. the entire second row
c. the third column
# Access the element at 2nd column, 1st row,2nd row and
3rd row
a[c(1,2,3),2]
a[1:3,2]
# Access the element at 2nd column, 1st row and 3rd row
a[c(1,3),2]
# Access the element at 1st row, 2nd & 3rd column
a[1,2:3]
a[1,c(2,3)]
# Access the element at 2nd & 3rd row, 2nd & 3rd column
a[2:3,2:3]
# Access the element at 1st & 3rd row, 1st & 3rd column
a[c(1,3),c(1,3)]
# Access only the 1st row
a[1,1:4]
a[1,]
# Access only the 3rd column
a[1:3,3]
a[,3]
# Access the element at 2nd & 3rd column, all rows
except 2nd row
a[-2,2:3]
Dimensions of a Matrix:
-----------------------
--> Retrieve or set the dimension of an object.
--> We have to use
dim(x)
dim(x) <- value
# to check the dimensions of a matrix
dim(a)
Matrix Manipulations:
=====================
--> We can perform mathematical operations on the
matrices.
--> The result of the operation is also a matrix
--> The number of rows and columns (dimensions) should
be same for
the matrices.
a <- matrix(1:9,3)
b <- matrix(11:19,3)
# Add the matrices
c <- a + b
print(c)