Lists in R Language

In this tutorial, we are going to learn about the Lists in R Language, creating a list, naming the list elements, accessing list elements, manipulating the list elements, etc.
Submitted by Bhavya Sri Khandrika, on December 06, 2020

Lists

Lists contain several types of objects. However, the following can be included or taken as the R objects. They are Numbers, Strings, Vectors.

We need to note that R has a provision such that a list present in another list is also considered as an element. Apart from the above stated, the lists may also contain the functions or matrices as it's elements.

Creating a list

The lists are created with the help of the inbuilt function known as a list() function.

Creating a list using list() function:

The below code will enable you to create a list by using the list() function:

#creating a list of vectors
vect.x <- c(1.85, 45.29, 28.3, 5.8)
vect.y <- c("pink", "yellow", "black", "lavender", "red")
vect.z <- c("B01", "A02", "C09", "M06", "P04")
vect.p <- c(TRUE, FALSE, FALSE, TRUE)

list.output <- list(vect.x, vect.y ,vect.z,vect.p )

print(list.output)

Output:

[[1]]
[1]  1.85 45.29 28.30  5.80

[[2]]
[1] "pink"     "yellow"   "black"    "lavender" "red"     

[[3]]
[1] "B01" "A02" "C09" "M06" "P04"

[[4]]
[1]  TRUE FALSE FALSE  TRUE

Including

One can create the list by including the matrix numbers in them.

The below code will demonstrate how to include the matrix in a list. In addition to that let us combine the above list with the matrix being considered and finally print the value.

#here the below is a matrix and the 
#following is a vector
B <- matrix(seq.int(2.4, 1.3, -0.1), nrow = 6,
   	byrow =TRUE)
vect.q <- c(218.44, 107.87, 98.82, 90.68, 256.09)

#here we are combining the above considered matrix 
#with the vector taken
list.combined<- list(B, vect.q)
print(list.combined)

Output:

[[1]]
     [,1] [,2]
[1,]  2.4  2.3
[2,]  2.2  2.1
[3,]  2.0  1.9
[4,]  1.8  1.7
[5,]  1.6  1.5
[6,]  1.4  1.3

[[2]]
[1] 218.44 107.87  98.82  90.68 256.09

Naming the list elements

To make the programmers witness the beauty of the R when compared to the other competitive programming languages here is one of the benefits of using the R language.

The users can actually name the list that is being declared by them. For that, the users need to follow the syntax that is shown in the below example code.

# Creating a Named List in the R Programming
list.input <- list("website name" = "IncludeHelp", "Well-Recognised?" = TRUE, "Dwell time in the past 3 years(in min)" = c(30,45,60), "Shares received" =1598 , "Employees" = 700)
print(list.input)

Output:

$`website name`
[1] "IncludeHelp"

$`Well-Recognised?`
[1] TRUE

$`Dwell time in the past 3 years(in min)`
[1] 30 45 60

$`Shares received`
[1] 1598

$Employees
[1] 700

Accessing list elements

Earlier, we have seen how to create a list using the inbuilt attribute i.e., list() function. Later we have come across the concept of naming the elements present in the list. Now it's time to learn the next topic i.e., how to access the declared list elements.

The list elements can be accessed by making use of the index which is quite similar to the arrays in the basic C programming language.

In case if the user desires to access the named list then they will be using the names of the list elements as the accessing tools.

Considering the above-quoted example only let us extend it by using the concept of accessing the list parameters.

# Accessing R List Elements
# creating a list with the help of vectors
vect.x <- c(1.85, 45.29, 28.3, 5.8)
vect.y <- c("pink", "yellow", "black", "lavender", "red")
vect.z <- c("B01", "A02", "C09", "M06", "P04")
vect.p <- c(TRUE, FALSE, FALSE, TRUE)

list.output <- list(vect.x, vect.y ,vect.z,vect.p )
print(list.output)

#here the below is a matrix and the following is a vector
B <- matrix(seq.int(2.4, 1.3, -0.1), nrow = 6,
   	byrow =TRUE)
vect.q <- c(218.44, 107.87, 98.82, 90.68, 256.09)

# Here, we are combining the above considered 
# matrix with the vector taken
list.combined<- list(B, vect.q)
print(list.combined)

# Accessing the second Element
print(list.combined[2])

# Accessing the fifth Element
print(list.combined[5])

Output:

[[1]]
[1]  1.85 45.29 28.30  5.80

[[2]]
[1] "pink"     "yellow"   "black"    "lavender" "red"     

[[3]]
[1] "B01" "A02" "C09" "M06" "P04"

[[4]]
[1]  TRUE FALSE FALSE  TRUE

[[1]]
     [,1] [,2]
[1,]  2.4  2.3
[2,]  2.2  2.1
[3,]  2.0  1.9
[4,]  1.8  1.7
[5,]  1.6  1.5
[6,]  1.4  1.3

[[2]]
[1] 218.44 107.87  98.82  90.68 256.09

[[1]]
[1] 218.44 107.87  98.82  90.68 256.09

[[1]]
NULL

Here, in the above-considered list, there is no fifth element present therefore while printing the fifth element it shows as NULL.

One might observe from where this NULL has come. This is because in the above list input we did not mention the data in the form of a matrix. Therefore, while accessing the elements of the matrix the compiler finds that there is no data present in the form of the matrix. Hence it returns a NULL value.

Manipulating the list elements

In the R language, one can add, delete the elements, or update the list. All the manipulating actions must be performed only at the end of the list. However, we can update any of the elements in the available list.

Let us perform all the manipulation tasks on the above-considered example only. So that the programmer can identify the difference between each and every modification very clearly.

# Manipulations the R Programming Language
vect.p <- c(62.8, 42.80, 15.610, 159.870)
vect.q <- c("PINK", "BLACK", "ORANGE", "LAVENDER", "BLUE", "YELLOW")

list.input <- list(vect.p, vect.q )

names(list.input) <- c("float_data", "fav_Colours")

B <- matrix(seq.int(2.4, 1.3, -0.1), nrow = 6,
   	byrow =TRUE)

vect.r <- c('a', 'b','c','d','e','f','g','f')

list.mix <- list(B, list.input, 1596, vect.r, "INCLUDEHELP")

names(list.mix) <- c("float_values_Matrix", "combined_List", "user_num", "alphabet_input", "website_name")
print(list.mix)

list.mix$website_name <- "IncludeHelp.in"
print(list.mix$website_name)

list.mix$alphabet_input <- c('h','i','j')
print(list.mix$alphabet_input)

list.mix$float_values_Matrix <- NULL
print(list.mix)

Output:

$float_values_Matrix
     [,1] [,2]
[1,]  2.4  2.3
[2,]  2.2  2.1
[3,]  2.0  1.9
[4,]  1.8  1.7
[5,]  1.6  1.5
[6,]  1.4  1.3

$combined_List
$combined_List$float_data
[1]  62.80  42.80  15.61 159.87

$combined_List$fav_Colours
[1] "PINK"     "BLACK"    "ORANGE"   "LAVENDER" "BLUE"     "YELLOW"  


$user_num
[1] 1596

$alphabet_input
[1] "a" "b" "c" "d" "e" "f" "g" "f"

$website_name
[1] "INCLUDEHELP"

[1] "IncludeHelp.in"
[1] "h" "i" "j"
$combined_List
$combined_List$float_data
[1]  62.80  42.80  15.61 159.87

$combined_List$fav_Colours
[1] "PINK"     "BLACK"    "ORANGE"   "LAVENDER" "BLUE"     "YELLOW"  


$user_num
[1] 1596

$alphabet_input
[1] "h" "i" "j"

$website_name
[1] "IncludeHelp.in"

Merging the lists

In the above lines, we have come across the concept of manipulating the list elements like adding and deleting the elements of the list in order to update the list. Now let us study the concept of merging the various lists.

  • Step 1: Declare two different vectors.
  • Step 2: To create a combined list with the above two declared individual vectors.
  • Step 3: To declare a 2 * 6 matrix with the float values in it.
  • Step 4: To create another list with one float value, String, a boolean value, Matrix, integer, and a vector.
  • Step 5: To combine or merge the above finally obtained two individual lists.

Code:

# The following code will demonstrate the operation 
# of merging on the R elements.
vect.r <- c(5298, 352891, 19726, 372271)
print("R vector")
print(vect.r)

vect.s <- c("D15EC002", "D18EI005", "D14CE027", "D16ME089", "D17EE056")
print("S vector")
print(vect.s)

list.e <- list(vect.r, 26853, vect.s )
print("First combined lists with both vectors R and vector S along with another integer value")
print(list.e)

B <- matrix(seq.int(2.4, 1.3, -0.1), nrow = 6,
   	byrow =TRUE)

list.z <- list(86.2,"IncludeHelp",TRUE,B,16, c(2.8,5.4,6.9,1.99,7.367) )
print("Second combined lists with one float value, String, boolean value, Matrix, integer and a vector")
print(list.z)

Output:

[1] "R vector"
[1]   5298 352891  19726 372271
[1] "S vector"
[1]   5298 352891  19726 372271
[1] "First combined lists with both vectors R and vector S along with another integer value"
[[1]]
[1]   5298 352891  19726 372271

[[2]]
[1] 26853

[[3]]
[1] "D15EC002" "D18EI005" "D14CE027" "D16ME089" "D17EE056"

[1] "Second combined lists with one float value, String, boolean value, Matrix, integer, and a vector"
[[1]]
[1] 86.2

[[2]]
[1] "IncludeHelp"

[[3]]
[1] TRUE

[[4]]
     [,1] [,2]
[1,]  2.4  2.3
[2,]  2.2  2.1
[3,]  2.0  1.9
[4,]  1.8  1.7
[5,]  1.6  1.5
[6,]  1.4  1.3

[[5]]
[1] 16

[[6]]
[1] 2.800 5.400 6.900 1.990 7.367

Converting the list into a vector

The lists have an extra provision where they can be converted into vectors such that in the future time the vector can be further used for any manipulations. We all know that the arithmetic calculations or manipulations can be made on vectors. There is an inbuilt function in the R that allows the programmers to convert the lists into vectors, it is unlist() function. The input of this typical function is a list whereas the output will be a vector.

Code to convert list to vector:

list1 <- list((seq.int(2.4, 1.3, -0.2)*3))
print(list1)

list2 <-list((seq.int(3.5,5.0,+0.3)))
print(list2)

# Convert the lists to vectors.
v1 <- unlist(list1)   
print(v1)
v2 <- unlist(list2)
print(v2)

# Perform subtraction operation on the vectors
result <- v2-v1
print(result)

Output:

[[1]]
[1] 7.2 6.6 6.0 5.4 4.8 4.2

[[1]]
[1] 3.5 3.8 4.1 4.4 4.7 5.0

[1] 7.2 6.6 6.0 5.4 4.8 4.2
[1] 3.5 3.8 4.1 4.4 4.7 5.0
[1] -3.7 -2.8 -1.9 -1.0 -0.1  0.8



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.