R Lists: Create, Append, and Remove

A data structure is a specialized format for storing, retrieving, and processing data so that it can be used efficiently in the main memory. Arrays, Stack, Trees, etc are some handy data structures.

A list in R is a data structure that is mutable and has an ordered sequence of elements. Each element or value that is inside a list is known as an item.

Lists in R can contain elements of different data types (numbers, strings, vectors, and another list inside it). A list in R can be created by using the list() function.

Syntax

The parameters are passed inside the braces '( )' of list() function.

list(...)

Parameters

The parameters can be any number of items that we need to add/append to the list.

Return value

The list() function will return a list containing all the items that we passed to it as a parameter.

Explanation

mylist <- list(1, 2022, 0.05, "Python") 
print(mylist)
  • Line#1: Initializing a list containing integers, a float number, and a string.
  • Line#2: Printing the list initialized in line 1.

Output

R Lists
R Lists

Accessing the Elements of a List

Accessing the elements of a list in R is extremely simple. One can access an element by simply passing the index of the element inside the square brackets '[ ]'.

mylist <- list(1.6, "Python","Fox")
Accessed_item <- mylist[3]
print(Accessed_item)
  • Line#1: Initializing a list with a float number and 2 strings.
  • Line#2: Accessing the item at the 3rd index of the list.
  • Line#3: Printing the item accessed in Line 2.

Output

accessing elements in R lists
Accessing elements in R lists

Operations on an R List

Append Operation

In order to append an item to an existing list, we use the append() function. Inside append() we pass the name of the list in which we want to append something as the first argument. The item to be appended is passed as the second argument.

mylist <- list(1.6, "Python")
mylist <- append(mylist, 5)
print(mylist)
  • Line#1: Initializing a list containing a float number and a string.
  • Line#2: Adding a new string to the list using the append() function and reassigning the returned list to the original list.
  • Line#3: Printing the updated list.

Output

append in R lists
append in R lists

Remove Operation

In order to remove an item from an existing list, we pass the index number of the item that we want to remove followed by the minus(-) sign to the list. The returned list is stored back in the original list.

mylist <- list(1.6, "Python", 123)
mylist <- mylist[-2]
print(mylist)
  • Line#1: Initializing a list containing a float number and a string and an integer.
  • Line#2: Removing the 2nd element from the list by passing the index 2 followed by – sign and assigning the resulting list to the original list.
  • Line#3: Printing the updated list.

Output

remove from R lists
remove elements from R lists

Length of List in R

length() function in R is used to calculate the length of the list country. It will return the total number of entries in an R list.

country <- list("United States", "Canada", "Austria", "Germany", "France")
# print list on console
print(length(country))
  • Line#1: list() function will create a list named country with argument values.
  • Line#3: length() will calculate the total number of entries in a country list. Besides, print() will print length count on the console.

Output

r list length
r list length

R Lists Indexing

In contrast to conventional lists indexing. The indexing of R lists is quite different starting from 1 instead of 0.

Indexing in R lists
Indexing in R lists

Stay in the Loop

Get the daily email from Algoideas that makes reading the news actually enjoyable. Join our mailing list to stay in the loop to stay informed, for free.

Latest stories

- Advertisement -

You might also like...