R language provides programmers with the facility to put elements of different types into a single container like numbers, strings, vectors etc. This flexibility of containing multiple types of elements is possible is R programming using Lists. In this chapter you will learn about the lists and its implementation within R program.



What are Lists in R programming?

A list, in lame word, a vector in which all the element can be of a diverse type. It can be handled by first creating lists then using index programmers can manipulate lists.

Creating Lists

Lists can be created using the list function and denoting the contents works a lot like the 'c' function which you have seen already in previous tutorials. You just list the contents, with each argument and each separated by a comma. List elements can be of any variable type—vectors, numbers or even functions. Here is a simple example of how to use list in R:

alist <- list ("Red", "Blue", c(42,36,01), FALSE, 73.91, 128.6)

print (alist)

Here, alist is the name of the list, list() is use to lists all the elements of different types. And the next is the print statement which prints the entire variable's value.

Atomic and Recursive Variables

Due to this capability of containing other lists within themselves (lists), lists are considered as the recursive variables. Vectors and arrays, by default, come under the category of atomic variables. Variables can either be recursive or atomic but never both. The functions is.recursive and is.atomic let programmers' test variables to see what their types are. Here is a simple example of using these 2 functions -

is.atomic (list ())

# [1] FALSE                  the output comes as False

is.recursive (list ())

# [1] TRUE                   the output comes as True

is.atomic (numeric ())

# [1] TRUE                   the output comes as True

Indexing Lists in R Language

Consider this test list:

ls <- list(

first = 2,

second = 4,

third = list(

fourth = 3.2,

fifth = 6.4

)

)

As with vectors, you can access every  element of the list by the use of square brackets - [], and by numeric indices or using element names or by the logical index. Here is an example of how o use these values

ls [1:2]

# $first

# [1] 2

# $second

# [1] 4

ls[-3]

# $ first

# [1] 2

# $ second

# [1] 4 

Or the lists values can also be invoked using element's name as:

ls [c ("first", "second")]

# $ first

# [1] 2

# $ second

# [1] 4

Manipulating List Elements

You can insert, delete, update and modify list elements within a list. You can insert and delete elements just at the end of any list. But list allows to update any element.

names (alist) <- c ("Ray", "Karl", "Steve")

# Now you will add element at the end of the list

alist[4] <- "Mark"

print (alist[4])

 

# Now you will remove the last element from the list

alist[4] <- NULL

 

# Now you will update the 2nd Element

alist [2] <- "Karlos"

print (alist[2])


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram