R List
List
- A List is an object consisting of an ordered collection of objects known as its Components
- The Components need not be of same type
where x can be a numeric vector, a logical value, a matrix, a complex vector, a character array, a function etc…
In the example shown, the List object consists of multiple classes like:
CODE/PROGRAM/EXAMPLE
lst <- list(name="Tony",
wife="Katy",
no.children=3,
child.ages=c(4,7,9))
print(Lst)
$name
[1] "Tony"
$wife
[1] "Katy"
$no.children
[1] 3
$child.ages
[1] 4 7 9
- There are 2 ways of checking, whether the variable is a list or not
Syntax
//Syntax:
>class()
>is.list(x)
CODE/PROGRAM/EXAMPLE
class(Lst)
[1] "list"
is.list(Lst)
[1] TRUE
str() function describes the components of the object
CODE/PROGRAM/EXAMPLE
str(Lst)
as.list() converts an object to a list
CODE/PROGRAM/EXAMPLE
Std <- c("John", "kerry", "Paul", "Tom")
newlst <- as.list(Std)
newLst
[[1]]
[1] "John"
[[2]]
[1] "kerry"
[[3]]
[1] "Paul"
[[4]]
[1] "Tom"
class(newLst)
[1] "list"
Note: Vectors can carry a single class whereas Lists can carry multiple classes