R List

The R List is one of the most powerful and useful data structure in real-time. It allow us to store different types of elements such as integer, string, Vector, matrix, list (nested), Data Frames, etc. Because of this, most people called list an advanced vector. Here, we show you how to create, Access Elements, Manipulate them, 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 it.

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

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

# 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") 

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

in this R Programming example, First, we created two vectors vect.a and vect.b and assigned some random values. At line 6, we create a List from the above-specified vectors.

Next, we declared a 3 * 4 Matrix, and a Vector of random values. In the 12th line, it creates the one that contains Matrix, Vector, and a List (Nested)

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

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 )

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 elements using this Character Vectors Index values. For example, print(listname.data$Company)

R LIST 4

Creating Named List in R using names function

Named function is very useful to assign names. 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”).

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. The Index value starts at 1 and ends at n where n is the number of elements.

For example, if we declare one 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:

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 items, then we can use those names to access the elements. Let’s access the Items present below using the Index names. Syntax behind this is: <listname>$Index_Name

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 Elements
print(list.mixed$Nested_List)
R LIST 7

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

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 Element using Boolean Vector
print(list.data[c(FALSE, FALSE, TRUE, FALSE, TRUE, TRUE)])

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

# Accessing All 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 elements in the following ways:

Line 15 accesses the element of name Company and replaces the existing name (i.e., Tutorial Gateway) with tutorialgateway.org.

In Line 18, we are accessing the item of name Random_Vector, and replace the existing vector (i.e., vect.z) with new vector values c(22, 44, 66, 88).

The line 21 removes the item of name Numeric_Matrix

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

We can Merge two into a single large list.

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

# Create 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 with one String, Matrix, and a vector
list.y <- list("Tutorial Gateway", A, c(5, 10, 15) )
print(list.y)

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

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

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