R List

The R List is one of the most powerful and useful data structure in real-time. Lists allow us to store different types of elements such as integer, string, Vector, matrix, list (nested List), Data Frames, etc. Because of this, most people called list an advanced vector. Here, we show you how to create a List, How to Access List Elements, Manipulate the List Elements, and To create Named Lists in R Programming with example.

Create R List

In this example, we will create a List of a different element. The most common way to create a List.

# Create R List
list.data <- list("Tutorial", "Gateway", TRUE, c(10, 20, 30), 95, 105.61)
print(list.data)

Here, we created a list with two strings, one Boolean value, one vector, and numeric values.

R LIST 1

Creating R List using Vectors

In this example, we will create a List in R Programming using vector.

# Create R List

# Creating three vectors
vect.a <- c(10.25, 20.45, 30.75, 40.85)
vect.b <- c(25, 50, 75, 100, 125)
vect.c <- c("India", "China", "Japan", "Russia", "USA") 

# Creating list 
list.data <- list(vect.a, vect.b, vect.c )
print(list.data)
R LIST 2

Creating List using Matrix, Vectors

In this example, we will create a List using Matrix and vector

# Create R List

vect.a <- c(10.25, 30.75, 20.45, 40.85)
vect.b <- c("India", "Japan", "Russia", "China", "USA") 

list.data <- list(vect.a, vect.b )
print(list.data)

A <- matrix(c(1:12), nrow = 3)
vect.c <- c(50, 75, 25, 100, 125)

list.mixed <- list(A, list.data, vect.c )
print(list.mixed)
R LIST 3

in this R Programming example, First, we created two vectors vect.a and vect.b and assigned some random values

vect.a <- c(10.25, 30.75, 20.45, 40.85)
vect.b <- c("India", "Japan", "Russia", "China", "USA")

It will create a List from the above-specified vectors.

list.data <- list(vect.a, vect.b )

Next, we declared a 3 * 4 Matrix, and a Vector of random values

A <- matrix(c(1:12), nrow = 3)
vect.c <- c(50, 75, 25, 100, 125)

It creates a List that contains Matrix, Vector, and a List (Nested List)

list.mixed <- list(A, list.data, vect.c )

Creating Named List in R

It shows the steps involved in creating named list in R programming and the syntax is: list_Name <- c(“index_Name1” = Value1, “index_Name2″ = Value2,… ,”index_NameN” = ValueN )

# Create Named List in R Programming

list.data <- list("Company" = "Tutorial Gateway", "Flag" = TRUE, "prod" = c(10, 20, 30), "val" = 95, "sale" = 105.61)
print(list.data)

As you can see, we are assigning names to list elements. This is very helpful to access the List elements using this Character Vectors Index values. For example, print(list.data$Company)

R LIST 4

Creating Named List in R using names function

Named function is very useful to assign names to Lists. Let us see how to assign names to the already created list in R programming. Syntax behind this is: names(<list name>) <- c(“name1”, “name2″…., “nameN”).

# Create Named List in R Programming

vect.x <- c(10, 30, 50, 70)
vect.y <- c("India", "Japan", "UK", "Russia", "China", "USA") 

list.a <- list(vect.x, vect.y )
# Assigning Names
names(list.a) <- c("Num_Vector", "Country") 
print(list.a)

matrix.A <- matrix(c(1:12), 3, 4)
vect.z <- c(55, 75, 25, 105, 125)

list.mixed <- list(matrix.A, list.a, vect.z, "Tutorial Gateway")
names(list.mixed) <- c("Num_Matrix", "Inner_List", "Rand_vector", "Company")
print(list.mixed)
R LIST 5

Accessing R List Elements

In R programming, we can access the elements in a List using the index position. List Index value starts at 1 and ends at n where n is the number of elements in a list.

For example, if we declare a List that stores 10 elements (of different types or the same type), then the index starts at 1 and ends at 10. To access or alter 1st value, use List_Name[1], and to alter or access the 10th value, use List_Name[10]. Let’s see the example for better understanding:

# Accessing R List Elements

vect.a <- c(10.25, 30.75, 20.45, 40.85)
vect.b <- c("India", "Japan", "Russia", "China", "USA") 
vect.c <- c(50, 75, 25, 100, 125)

A <- matrix(c(1:12), 3, 4)

list.data <- list(A, vect.a, "Tutorial Gateway", vect.b, 95, vect.c )
print(list.data)

# Accessing First Element
print(list.data[1])

# Accessing Fourth Element
print(list.data[4])
R LIST 6

Accessing R List items using Names

If we declared the list items with names or we assigned the names to list items, then we can use those names to access the List items. Let’s access the Items present in the List using the Index names. Syntax behind this is: <list name>$Index_Name

# Accessing R List Elements

vect.x <- c(10, 30, 50, 70)
vect.y <- c("India", "Russia", "Japan", "UK", "China", "USA") 

list.data <- list(vect.x, vect.y )
names(list.data) <- c("Numeric_Vector", "Country") 

matrix.A <- matrix(c(1:12), 3, 4)
vect.z <- c(55, 75, 25, 105, 125)

list.mixed <- list(matrix.A, list.data, vect.z, "Tutorial Gateway")
names(list.mixed) <- c("Numeric_Matrix", "Nested_List", "Random_vector", "Company")
print(list.mixed)

# Accessing Vector.z Elements
print(list.mixed$Random_vector)

# Accessing Vector.z Elements
print(list.mixed$Numeric_Matrix)

# Accessing Nested List Elements
print(list.mixed$Nested_List)
R LIST 7

Accessing List items using Boolean vector

Negative index position is used to omit those values from List in R programming. This example access List items using Boolean Vector.

# Accessing R List Elements

vect.a <- c(10.25, 30.75, 20.45, 40.85)
vect.b <- c("India", "Japan", "Russia", "China", "USA") 
vect.c <- c(50, 75, 25, 100, 125)

A <- matrix(c(1:12), 3, 4)

list.data <- list(A, vect.a, "Tutorial Gateway", vect.b, 95, vect.c )
print(list.data)

# Accessing List Element using Boolean Vector
print(list.data[c(FALSE, FALSE, TRUE, FALSE, TRUE, TRUE)])

# Accessing All Element except 1
print(list.data[-1])

# Accessing All Element except 4
print(list.data[-4])

# Accessing All Element except 1 and 6th element
print(list.data[c(-1, -6)])
R LIST 8

OUTPUT 2

R LIST 9

Manipulate R List Elements

We can manipulate the List elements in the following ways:

# Manipulating R List Elements

vect.x <- c(10, 30, 50, 70)
vect.y <- c("India", "Russia", "Japan", "UK", "China", "USA") 

list.data <- list(vect.x, vect.y )
names(list.data) <- c("Numeric_Vector", "Country") 

matrix.A <- matrix(c(1:12), 3, 4)
vect.z <- c(55, 75, 25, 105, 125)

list.mixed <- list(matrix.A, list.data, 95, vect.z, "Tutorial Gateway")
names(list.mixed) <- c("Numeric_Matrix", "Nested_List", "favNum", "Random_vector", "Company")
print(list.mixed)

list.mixed$Company <- "Tutortialgateway.org"
print(list.mixed$Company)

list.mixed$Random_vector <- c(22, 44, 66, 88)
print(list.mixed$Random_vector)

list.mixed$Numeric_Matrix <- NULL
print(list.mixed)
R LIST 10

It accesses the list item of name Company and replaces the existing name (i.e., Tutorial Gateway) with tutorialgateway.org.

list.mixed$Company <- "Tutortialgateway.org"
print(list.mixed$Company)

Accessing the list item of name Random_Vector, and replace the existing vector (i.e., vect.z) with new vector values c(22, 44, 66, 88)

list.mixed$Random_vector <- c(22, 44, 66, 88)

It removes the list item of name Numeric_Matrix

list.mixed$Numeric_Matrix <- NULL

Merge two lists

We can Merge two List into a single large list.

# Merging two Lists in R Programming

#Declared Two vector
vect.a <- c(10.25, 30.75, 20.45, 40.85)
vect.b <- c("India", "Japan", "Russia", "China", "USA") 

# Creating List 1 from those two Vectors Vect.a, Vect.b
list.x <- list(vect.a, 95, vect.b )
print(list.x)

# Declared One 4 * 3 matrix
A <- matrix(c(1:12), 4, 3)

# Creating second list with one String, Matrix, and a vector
list.y <- list("Tutorial Gateway", A, c(5, 10, 15) )
print(list.y)

# Combining or Merging two List
list.z <- c(list.x, list.y)
print(list.z)
R LIST 11

Convert List to Vector

We can convert the Lists into vectors. This conversion can help us to perform operations at least level.

# Converting Lists to vector in R Programming

list.x <- list(1:15)
list.y <- list(25:40)
print(list.x)
print(list.y)
typeof(list.x)

vect.a <- unlist(list.x)
print(vect.a)

vect.b <- unlist(list.y)
print(vect.b)
R LIST 12