Lists

List is a data structure having elements of mixed data types. A vector is having all elements of the same type is called atomic vector but a vector having elements of different type is called list.

We can check the type with typeof() or class() function and find the length using length()function.

 

x <- list("stat",5.1, TRUE, 1 + 4i)
x
class(x)
typeof(x)
length(x)

Output:

> x
[[1]]
[1] "stat"

[[2]]
[1] 5.1

[[3]]
[1] TRUE

[[4]]
[1] 1+4i

> class(x)
[1] “list”
> typeof(x)
[1] “list”
> length(x)
[1] 4

You can  create an empty list of a prespecified length with the vector() function.

 

x <- vector("list", length = 10)

x

Output:

> x
[[1]]
NULL

[[2]]
NULL

[[3]]
NULL

[[4]]
NULL

[[5]]
NULL

[[6]]
NULL

[[7]]
NULL

[[8]]
NULL

[[9]]
NULL

[[10]]
NULL

We can create a little bit complex List like below.

 

l <-list(
a <- c(1, 2, 3, 4),
b <- FALSE,
c <- "Hello Statistics!",
d = function(arg = 42) {print("Hello World!")},
e = diag(10)
)

l

Output:

> l
[[1]]
[1] 1 2 3 4

[[2]]
[1] FALSE

[[3]]
[1] “Hello Statistics!”

$d
function (arg = 42)
{
print(“Hello World!”)
}

$e
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 0 0 0 0 0 0 0 0 0
[2,] 0 1 0 0 0 0 0 0 0 0
[3,] 0 0 1 0 0 0 0 0 0 0
[4,] 0 0 0 1 0 0 0 0 0 0
[5,] 0 0 0 0 1 0 0 0 0 0
[6,] 0 0 0 0 0 1 0 0 0 0
[7,] 0 0 0 0 0 0 1 0 0 0
[8,] 0 0 0 0 0 0 0 1 0 0
[9,] 0 0 0 0 0 0 0 0 1 0
[10,] 0 0 0 0 0 0 0 0 0 1

How to extract elements from a list?

Lists can be subset using two syntaxes, the $ operator, and square brackets []. The $ operator returns a named element of a list. The [] syntax returns a list, while the [[]] returns an element of a list.

 

# subsetting
l$e
l["e"]
l[1:2]

l[c(1:2)] #index using integer vector
l[-c(3:length(l))] #negative index to exclude elements from 3rd up to last.
l[c(T,F,F,F,F)] # logical index to access elements

Output:

> l$e
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 0 0 0 0 0 0 0 0 0
[2,] 0 1 0 0 0 0 0 0 0 0
[3,] 0 0 1 0 0 0 0 0 0 0
[4,] 0 0 0 1 0 0 0 0 0 0
[5,] 0 0 0 0 1 0 0 0 0 0
[6,] 0 0 0 0 0 1 0 0 0 0
[7,] 0 0 0 0 0 0 1 0 0 0
[8,] 0 0 0 0 0 0 0 1 0 0
[9,] 0 0 0 0 0 0 0 0 1 0
[10,] 0 0 0 0 0 0 0 0 0 1
> l["e"]
$e
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 0 0 0 0 0 0 0 0 0
[2,] 0 1 0 0 0 0 0 0 0 0
[3,] 0 0 1 0 0 0 0 0 0 0
[4,] 0 0 0 1 0 0 0 0 0 0
[5,] 0 0 0 0 1 0 0 0 0 0
[6,] 0 0 0 0 0 1 0 0 0 0
[7,] 0 0 0 0 0 0 1 0 0 0
[8,] 0 0 0 0 0 0 0 1 0 0
[9,] 0 0 0 0 0 0 0 0 1 0
[10,] 0 0 0 0 0 0 0 0 0 1

> l[1:2]
[[1]]
[1] 1 2 3 4

[[2]]
[1] FALSE

> l[c(1:2)] #index using integer vector
[[1]]
[1] 1 2 3 4

[[2]]
[1] FALSE

> l[-c(3:length(l))] #negative index to exclude elements from 3rd up to last.
[[1]]
[1] 1 2 3 4

[[2]]
[1] FALSE

l[c(T,F,F,F,F)]
[[1]]
[1] 1 2 3 4

Delete Elements from a List:

l["e"]<-NULL

l

Output:

[[1]]
[1] 1 2 3 4

[[2]]
[1] FALSE

[[3]]
[1] “Hello Statistics!”

$d
function (arg = 42)
{
print(“Hello World!”)
}

Modifying a List in R:

You can change components of a list through reassignment.

 

 l[["name"]] <- "Kalyan Nandi"

l

Output:

[[1]]
[1] 1 2 3 4

[[2]]
[1] FALSE

[[3]]
[1] “Hello Statistics!”

$d
function (arg = 42)
{
print(“Hello World!”)
}

$name
[1] “Kalyan Nandi”

Explicit Coercion

Factors