Named List in R Programming
Last Updated :
22 Jun, 2020
A list is an object in R Language which consists of heterogeneous elements. A list can even contain matrices, data frames, or functions as its elements. The list can be created using list() function in R. Named list is also created with the same function by specifying the names of the elements to access them. Named list can also be created using names() function to specify the names of elements after defining the list. In this article, we’ll learn to create named list in R using two different methods and different operations that can be performed on named lists.
Syntax: names(x) <- value
Parameters:
x: represents an R object
value: represents names that has to be given to elements of x object
Creating a Named List
A Named list can be created by two methods. The first one is by allocating the names to the elements while defining the list and another method is by using names() function.
Example 1:
In this example, we are going to create a named list without using names() function.
x <- list (mt = matrix (1:6, nrow = 2),
lt = letters [1:8],
n = c (1:10))
cat ( "Whole List:\n" )
print (x)
|
Output:
Whole List:
$mt
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
$lt
[1] "a" "b" "c" "d" "e" "f" "g" "h"
$n
[1] 1 2 3 4 5 6 7 8 9 10
Example 2:
In this example, we are going to define the names of elements of the list using names() function after defining the list.
x <- list ( matrix (1:6, nrow = 2),
letters [1:8],
c (1:10))
cat ( "Whole list:\n" )
print (x)
|
Output:
Whole list:
[[1]]
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
[[2]]
[1] "a" "b" "c" "d" "e" "f" "g" "h"
[[3]]
[1] 1 2 3 4 5 6 7 8 9 10
Accessing components of Named List
Components of a named list can be easily accessed by $ operator.
Example:
x <- list (mt = matrix (1:6, nrow = 2),
lt = letters [1:8],
n = c (1:10))
cat ( "Element named 'mt':\n" )
print (x$mt)
cat ( "\n" )
cat ( "Element named 'n':\n" )
print (x$n)
|
Output:
Element named 'mt':
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
Element named 'n':
[1] 1 2 3 4 5 6 7 8 9 10
Modifying components of Named List
Components of named list can be modified by assigning new values to them.
Example:
lt <- list (a = 1,
let = letters [1:8],
mt = matrix (1:6, nrow = 2))
cat ( "List before modifying:\n" )
print (lt)
lt$a <- 5
cat ( "List after modifying:\n" )
print (lt)
|
Output:
List before modifying:
$a
[1] 1
$let
[1] "a" "b" "c" "d" "e" "f" "g" "h"
$mt
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
List after modifying:
$a
[1] 5
$let
[1] "a" "b" "c" "d" "e" "f" "g" "h"
$mt
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
Deleting components from Named List
To delete elements from named list, we’ll use within() function and the result will be assigned to the named list itself.
Example:
lt <- list (a = 1,
let = letters [1:8],
mt = matrix (1:6, nrow = 2))
cat ( "List before deleting:\n" )
print (lt)
lt <- within (lt, rm (a))
cat ( "List after deleting:\n" )
print (lt)
|
Output:
List before deleting:
$a
[1] 1
$let
[1] "a" "b" "c" "d" "e" "f" "g" "h"
$mt
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
List after deleting:
$let
[1] "a" "b" "c" "d" "e" "f" "g" "h"
$mt
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
Similar Reads
Operations on Lists in R Programming
Lists in R language, are the objects which comprise elements of diverse types like numbers, strings, logical values, vectors, list within a list and also matrix and function as its element. A list is generated using list() function. It is basically a generic vector that contains different objects. R
4 min read
Functions in R Programming
A function accepts input arguments and produces the output by executing valid R commands that are inside the function. Functions are useful when you want to perform a certain task multiple times. In R Programming Language when you are creating a function the function name and the file in which you a
8 min read
Label Encoding in R programming
The data that has to be processed for performing manipulations and Analyses should be easily understood and well denoted. The computer finds it difficult to process strings and other objects when data training and predictions based on it have to be performed. Label encoding is a mechanism to assign
3 min read
File Handling in R Programming
In R Programming, handling of files such as reading and writing files can be done by using in-built functions present in R base package. In this article, let us discuss reading and writing of CSV files, creating a file, renaming a file, check the existence of the file, listing all files in the worki
4 min read
Learn R Programming
R is a Programming Language that is mostly used for machine learning, data analysis, and statistical computing. It is an interpreted language and is platform independent that means it can be used on platforms like Windows, Linux, and macOS. In this R Language tutorial, we will Learn R Programming La
15+ min read
R Programming Language - Introduction
R was created for statistical analysis and data visualization. It started in the early 1990s when researchers needed a tool that could handle large datasets, run complex computations and display results clearly in graphs and charts. R provides a user-friendly environment and when used with tools lik
5 min read
Plyr Package in R Programming
The plyr is a R package used for data manipulation based on the split-apply-combine strategy. It allows to split a dataset into subsets, apply a function to each subset, and then combine the results into a single output. This approach is helpful for tasks like data aggregation, summarization, and tr
5 min read
Lexical Scoping in R Programming
Lexical scoping means R decides where to look for a variable based on where the function was written (defined), not where it is called. When a function runs and it sees a variable, R checks: Inside the function, is the variable there?If not, it looks in the environment where the function was created
4 min read
Environments in R Programming
The environment is a virtual space that is triggered when an interpreter of a programming language is launched. Simply, the environment is a collection of all the objects, variables, and functions. Or, Environment can be assumed as a top-level object that contains the set of names/variables associat
3 min read
dplyr Package in R Programming
The dplyr package for R offers efficient data manipulation functions. It makes data transformation and summarization simple with concise, readable syntax. Key Features of dplyrData Frame and TibbleData frames in dplyr in R is organized tables where each column stores specific types of information, l
4 min read