Lists in R - Easy-to-Follow Tutorial *Subject to Terms and Condition

## What are Lists?

Lists are the R objects with numbers, strings, vectors and another list or matrix inside it.

## Creating  a List

Example to create a list containing numbers, strings, vectors, and logical values.

```#creating a list
list_info <- list("Blue", "Yellow", c(12, 13, 14), TRUE, 13.12, 103.4)
print(list_info)```

Output:

```[]
 "Blue"
[]
 "Yellow"
[]
 12 13 14
[]
 TRUE
[]
 13.12
[]
 103.4```

## Naming List Elements

Names can be given to list elements and can be accessed using the corresponding names.

Get familiar with the top R Interview Questions to get a head start in your career!

Example:

```#Creating  a list which contains a matrix and a vector
list_name <- list(matrix(c(1,2,3,4,5,6), nrow = 2), c("mon","tue","wed"))
#Naming elements in the list
names(list_name) <- c("Matrix", "half_week")
#displaying list
print(list_name)```

Output:

```\$Matrix
[,1] [,2] [,3]
[1,]    1    3   5
[2,]    2    4   6

\$ half_week
 "mon" "tue" "wed"```

## Accessing List Elements

Index of the element of the list can be given access to Elements of the list.

Syntax:

```list_name <- list(.,..,.)
names(list_name) <- c(.,.,.)
print(list_name)```

Still, have queries? Come to Intellipaat’s R Programming Community, clarify all your doubts, and excel in your career!

## Manipulating List Elements

Addition, subtraction or deleting and updating the list elements can be done;

Few examples are:

```#Creating a list which contains a vector, a matrix and a list
list_name <- list(c("Mon", Tue", "Wed"), matrix(c(2,1,1,1,5,6), nrow =2), list("milk", 1.2)
#Naming elements in the list
names(list_name) <- c("half week", "Matrix", "A simple list")
#Creating an element at the end of list
list_name  <- "An Element"
print(list_name)
#Withdrawing the last element
list_name <-NULL
#Output last element
print(list_name)```

Output:

```[]
 " An Element

\$
NULL``` For the best of career growth, check out Intellipaat’s R Programming training Course in Singapore and get certified!

## Merging Lists

Merging can be done by placing all lists into one list() function.

Example:

```#Creating lists
lista <- list(2,4,6)
listb <- list("Jan", "Feb", "Mar")
#Merging lists
merge.list <- c(lista. listb)
#output merged list
print(merge.list)```

Output:

```[]
 2
[]
 4
[]
 6
[]
 "Jan"
[]
 "Feb"
[]
 "Mar"```

## Converting List to Vector

Using unlist() function we can convert a list to a vector so that all the elements of the vector can be used for further data manipulation such as applying arithmetic operations.

Example:

```#Creating lists
lista <- list(1:3)
listb <- list(4:6)
#Converting lists to vector
cva <- unlist(lista)
cvb <- unlist(listb)
print(cva)
print(cvb)```

Output:

``` 1 2 3
 4 5 6```

Course Schedule

Name Date Details
R Programming Course 07 Oct 2023(Sat-Sun) Weekend Batch
View Details
R Programming Course 14 Oct 2023(Sat-Sun) Weekend Batch
View Details
R Programming Course 21 Oct 2023(Sat-Sun) Weekend Batch
View Details

Browse Categories