Lists – Heterogeneous Data Structure of R

In the following tutorial – we will cover one major data type called list. For those who have not seen the earlier part, we are covering R studio here,  vectors and matrices have been covered as well. Unlike the other tutorials on the internet – this is a hands on.

Get on to R studio and practice along – comment below for any questions:

# Lists

# Lists is technically a vector with heterogenous data existing at one point of time without coercion
# Lists can be created using vectors

x = c(1:3)
y = letters[1:3]

myList = list(x,y)
myList

# Lists can be created using vector() function
newList = vector(mode = ‘list’)
is.list(newList)

# Lists can be created directly using list() function

list1 = list(Name = ‘Nehal’, Cell = 9890, salaried = TRUE)
list1

# Lists are also known as recursive vectors because a list can have other lists

x = list(list(list()))
x

# We can see the structure of the list using ‘str’ function

str(x)

# We can convert an existing list to a vector using unlist() function

unlist(list1)

dim(list1)

is.list(list1)

# Since list is a vector, it will turn out TRUE when is.vector() function is applied to it
is.vector(list1)

# Can we convert an existing list using as.vector function

a = as.vector(list1)
str(a)

# Hence we have to use unlist() to convert an existing list to a vector. Laws of coercion applies

# .—————————————Indexing Lists—————————————-

list1 = list(Name = ‘Nehal’, Cell = 9890, salaried = TRUE)

str(list1)

attributes(list1)

# Indexing, observe the difference between the two

list1[1]

list1[[1]]

a = list1[1]
b = list1[[1]]

class(a)

class(b)

# Clearly single square brackets preserve the list structure

# Double square brackets simplifies the structure to a vector

# Accessing lists using ‘$’ notation

c = list1$Name

class(c)

# Hence even ‘$’ notation simplifies and does not preserve the structure of the list

list1[‘Name’]

list1[[‘Name’]]
# Partial Matching
list1$N

# Partial matching does not work in the following case

list2 = list(name = ‘nehal’, number = 9890)
list2$n

# For partial matching to work, unique attributes required in terms of their spelling

# ————————-Adding Elements to a list—————————————-
list1 = list(Name = ‘Nehal’, Cell = 9890, salaried = TRUE)

# Adding elements using ‘$’ sign notation

list1$Email = ‘nehal@gmail.com’

list1

# Adding elements using []

list1[‘Address’] = ‘New Delhi’

list1

# Adding elements using [[]]

list1[[‘Passport’]] = NA

list1

# Modifying existing content of the list

x = list(m = c(‘a’,’b’,’c’), n = c(1,2,3))

# Modifying 1st element of m

x[[1]][1] = ‘o’

x

x$m[1] = ‘a’

x

# This does not work

x[1][1] = ‘o’

x

# ————————–Removing elements from a list——————————–

# Removing elements using ‘$’ notation

list1$Passport = NULL

list1

# Removing elements using ‘[]’ notation

list1[‘Address’] = NULL

# Removing elements using ‘[[]]’ notation

list1[[‘Email’]] = NULL

list1

#———————– Using lapply and sapply———————————————

list2 = list(x = 1:10, y = 2:11)

a = lapply(list2, sum)

b = sapply(list2, sum)

class(b)
b
list3 = list(x = c(1,2,3,2,1,2,3,4,2,3), y = c(2,4,2,3,4,2,5))

c = lapply(list3, unique)

d = sapply(list3, unique)

class(d)

Leave a Reply

Your email address will not be published. Required fields are marked *