# R List

The R List is one of the most powerful and useful data structures in real time. It allows us to store different types of elements such as integers, strings, Vectors, matrices, lists (nested), Data Frames, etc. Because of this, most people call the list an advanced vector. Here, we show you how to create, Access Elements, Manipulate them, and create Named Lists in R programming language with an example.

## Create R List

In this example, we will create a List of different elements. 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.

In this example, we will create a List in R Programming using a 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)```

### Creating List using Matrix and Vectors in R

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

Within this R Programming example, First, we created two vectors vect.a and vect.b and assigned some random values. In 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)
```

### Creating Named List

It shows the steps involved in creating a 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 these Character Vectors Index values. For example, print(listname.data\$Company)

### Creating a Named List using the names function

The named function is very useful for assigning names. Let us see how to assign names to the already created list in R. 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)```

## Accessing R List Elements

We can access the elements in an R List using the index position in this programming. 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 the 1st value, use List_Name, and to alter or access the 10th value, use List_Name. Let’s see the example for a 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)

# Accessing Fourth Element
print(list.data)```

### Accessing R List items using Names

If we declare the list items with names or we assign 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. The 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)```

A negative index position is used to omit those values from the List in R programming. This example accesses 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)])```

OUTPUT 2

## Manipulate R List Elements

We can manipulate the elements in the following ways:

Line 15 accesses the element of the 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 replacing the existing vector (i.e., vect.z) with new vector values c(22, 44, 66, 88).

Line 21 removes the item of the 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)```

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)```

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)```