## Saturday, 27 January 2018

### What is List and How to Create it in R Programming

The Lists in R Programming
A list is an ordered collection of objects (components). Lists are referred to as the recursive vectors. In a list we can store different modes or types of data.
A list allows you to gather a variety of (possibly unrelated) objects under one name. A list may contain a combination of vectors,matrices, data frames, and even other lists.

Lists are important R structures for two reasons. First,they allow you to organize and recall disparate information in a simple way. Second,the results of many R functions return lists.

Creating a List :

We create a list using the list() function
mylist <- list(object1, object2, ...)
here the objects are any of the structures (
vectors,matrices..). Optionally, you can name the objects in a list:
mylist <- list(name1=object1, name2=object2, ...)

Example 1:
let’s consider an employee database. For each employee, we wish to store the name, salary , and a Boolean indicating union membership. Since we have three different modes here—character, numeric,and logical. So that we can use the list here to store different modes of this data.

We could create a list to represent our employee, Joe, this way:
j<- list(name="Joe", salary=55000, unionms=T)

#The result of the list displays all its components

> j
\$name
 "Joe"
\$salary
 55000
\$unionms
 TRUE

Also we can create the list without specifying the object names.
j<- list("Joe",55000,T)
>j
[]
 "Joe"
[]
 55000
[]
 TRUE

It could be ideal to use the names instead of numeric indices.
We can access the specific object of a list as follows..

> j\$sal
 55000

Since Lists are also a kind of vectors, they can be created via vector():
> z <- vector(mode="list")
> z[["abc"]] <- 3
> z
\$abc
 3

Example 2:
In this example, you create a list with four components: a string, a numeric vector, a matrix, and a character vector. You can combine any number of objects and save them as a list.
> g <- "My Sample List"

> h <- c(25, 26, 18, 39)
> j <- matrix(1:10, nrow=5)
> k <- c("one", "two", "three")
> mylist <- list(title=g, ages=h, j, k)

> mylist
\$title
 "My Sample List"

\$ages
 25 26 18 39

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

[]
 "one" "two" "three"

You can also specify elements of the list by indicating a component number or a name within double brackets. In this example, mylist[] and mylist[["ages"]] both refer to the same four-element numeric vector. For named components,mylist\$ages would also work.

> mylist[]
 25 26 18 39

> mylist[["ages"]]
[ 25 26 18 39

Notes:
The period (.) has no special significance in object names. The dollar sign (\$) has a some what analogous meaning to the period in other object-oriented languages and can be used to identify the parts of a data frame or list. For example, A\$x refers to variable x in data frame A.

R doesn’t provide multi line or block comments. You must start each line of a multi line comment with #. For debugging purposes, you can also surround code that you want the interpreter to ignore with the statement if(FALSE){...}.Changing the FALSE to TRUE allows the code to be executed.
■ Assigning a value to a nonexistent element of a vector, matrix, array, or list expands that structure to accommodate the new value. For example, consider the following:
> x <- c(8, 6, 4)
> x <- 10

> x
 8 6 4 NA NA NA 10

The vector x has expanded from three to seven elements through the assignment. x<- x[1:3] would shrink it back to three elements.
■ R doesn’t have scalar values. Scalars are represented as one-element vectors.
■ Indices in R start at 1, not at 0. In the vector earlier, x is 8.
■ Variables can’t be declared. They come into existence on first assignment.

Thanks, TAMATAM