[go: up one dir, main page]

Open In App

Named List in R Programming

Last Updated : 22 Jun, 2020
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.




# Defining a list with names
x <- list(mt = matrix(1:6, nrow = 2),
          lt = letters[1:8],
          n = c(1:10))
  
# Print whole list
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.




# Defining list
x <- list(matrix(1:6, nrow = 2),
          letters[1:8],
          c(1:10))
  
# Print whole list
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:




# Defining a list with names
x <- list(mt = matrix(1:6, nrow = 2),
          lt = letters[1:8],
          n = c(1:10))
  
# Print list elements using the names given
# Prints element of the list named "mt"
cat("Element named 'mt':\n")
print(x$mt)
cat("\n")
  
# Print element of the list named "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:




# Defining a named list
lt <- list(a = 1,
           let = letters[1:8],
           mt = matrix(1:6, nrow = 2))
  
cat("List before modifying:\n")
print(lt)
  
# Modifying element named 'a'
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:




# Defining a named list
lt <- list(a = 1,
           let = letters[1:8],
           mt = matrix(1:6, nrow = 2))
  
cat("List before deleting:\n")
print(lt)
  
# Modifying element named 'a'
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

Subtracting similarly named elements in a list of vectors in R
When working with vectors in R, you might encounter a situation where you need to perform operations on elements with similar names across different vectors within a list. One common operation is subtraction. This article will guide you through the process of subtracting similarly named elements in a list of vectors in the R Programming Language. I
3 min read
Convert an Object to List in R Programming - as.list() Function
as.list() function in R Programming Language is used to convert an object to a list. These objects can be Vectors, Matrices, Factors, and dataframes. Syntax: as.list(object) Parameters: object: Vector, Matrix, factor, or data frame R - as.list() Function ExampleExample 1: Converting Vector to list using as.list() function in R Language C/C++ Code #
2 min read
Check if the Object is a List in R Programming - is.list() Function
is.list() function in R Language is used to return TRUE if the specified data is in the form of list, else returns FALSE. Syntax: is.list(X) Parameters: x: different types of data storage Example 1: # R program to illustrate # is.list function # Initializing some list a <- list(1, 2, 3) b <- list(c("Jan", "Feb", "Mar
1 min read
What is the Relation Between R Programming and Hadoop Programming?
The relation between R programming and Hadoop revolves around integrating R with the Hadoop ecosystem to analyze large datasets that are stored in a Hadoop environment. R, primarily used for statistical analysis and data visualization, is not inherently built for handling big data. However, when combined with Hadoop, it can leverage Hadoop's distri
3 min read
Plotting Graphs using Two Dimensional List in R Programming
List is a type of an object in R programming. Lists can contain heterogeneous elements like strings, numeric, matrices, or even lists. A list is a generic vector containing other objects. Two-dimensional list can be created in R programming by creating more lists in a list or simply, we can say nested lists. The list() function in R programming is
2 min read
Two Dimensional List in R Programming
A list in R is basically an R object that contains within it, elements belonging to different data types, which may be numbers strings or even other lists. Basically, a list can contain other objects which may be of varying lengths. The list is defined using the list() function in R. A two-dimensional list can be considered as a "list of lists". A
5 min read
Apply a Function over a List of elements in R Programming - lapply() Function
lapply() function in R Programming Language is used to apply a function over a list of elements. lapply() function is used with a list and performs the following operations: lapply(List, length): Returns the length of objects present in the list, List.lapply(List, sum): Returns the sum of elements held by objects in the list, List.lapply(List, mean
2 min read
Get a List of all the Attached Packages in R Programming - search() Function
search() function in R Language is used to get the list of all the attached packages in the R search path. Syntax: search() Parameters: This function takes no parameters. Example 1: # R program to list the packages # attached to R search path # Calling search() function search() Output: [1] ".GlobalEnv" "package:stats" "package:graphics" [4] "packa
1 min read
Get the List of Arguments of a Function in R Programming - args() Function
args() function in R Language is used to get the required arguments by a function. It takes function name as arguments and returns the arguments that are required by that function. Syntax: args(name) Parameters: name: Function name Returns: For a closure: Formal Argument list but with NULL body For a Primitive Function: A closure with usage and a N
1 min read
Recursively apply a Function to a List in R Programming - rapply() function
rapply() function in R Language is used to recursively apply a function to a list. Syntax: rapply(object, f, classes = "ANY", deflt = NULL, how = c("unlist", "replace", "list")) Parameters: object: represents list or an expression f: represents function to be applied recursively classes: represents class name of the vector or "ANY" to match any of
3 min read
List all the Objects present in the Current Working Directory in R Programming - ls() Function
ls() function in R Language is used to list the names of all the objects that are present in the working directory. Syntax: ls() Parameters: This function needs no argument Example 1: # R program to list all the object names # Creating a vector vec <- c(1, 2, 3) # Creating a matrix mat <- matrix(c(1:4), 2) # Creating an array arr <- array(
1 min read
Coercing an Object of mode "list" to mode "call" in R Programming - as.call() Function
as.call() function in R Language is used to coerce the object of mode "list" to mode "call". The first element of the list becomes the function part of the call. Syntax: as.call(x) Parameters: x: an arbitrary R object Example 1: # R program to illustrate # as.call function # Calling the as.call() function as.call(list(list, 5, 10)) as.call(list(as.
1 min read
Get a List of Numbers in the Specified Range in R Programming - seq.int() Function
seq.int() function in R Language is used to return a list of numbers in the specified range. Syntax: seq.int(from, to, by) Parameters: from: specified range from to: specified range to by: specified number by which it jumps through returned number Example 1: # R program to illustrate # seq.int function # Calling seq.int() function seq.int(0, 5) seq
1 min read
Get a List of all the 657 colors in R Programming - colors() Function
colors() function in R Language is used to show all the 657 color names that are contained in R programming. Syntax: colors()Parameters: Does not accept any parameters. Example 1: C/C++ Code # R program to illustrate # colors function # Calling the colors() function # to show all the 657 colors that # names are contained in # R programming colors()
1 min read
Get a List of points obtained by Interpolation in R Programming - spline() and splinefun() Function
In R programming, spline() and splinefun() function is used to create a list of points obtained by interpolation. It performs cubic spline interpolation of given data points. Syntax: spline(x, y, method) and splinefun(x, y, method) Parameters: x, y: represents vectors giving the points for interpolation method: represents the type of spline interpo
1 min read
Getting the Modulus of the Determinant of a Matrix in R Programming - determinant() Function
determinant() function in R Language is a generic function that returns separately the modulus of the determinant, optionally on the logarithm scale, and the sign of the determinant. Syntax: determinant(x, logarithm = TRUE, ...) Parameters: x: matrix logarithm: if TRUE (default) return the logarithm of the modulus of the determinant Example 1: # R
2 min read
tidyr Package in R Programming
Packages in the R language are a collection of R functions, compiled code, and sample data. They are stored under a directory called “library” in the R environment. By default, R installs a set of packages during installation. One of the most important packages in R is the tidyr package. The sole purpose of the tidyr package is to simplify the proc
14 min read
Get Exclusive Elements between Two Objects in R Programming - setdiff() Function
setdiff() function in R Programming Language is used to find the elements which are in the first Object but not in the second Object. Syntax: setdiff(x, y) Parameters: x and y: Objects with sequence of itemsR - setdiff() Function ExampleExample 1: Apply setdiff to Numeric Vectors in R Language C/C++ Code # R program to illustrate # the use of setdi
2 min read
Add Leading Zeros to the Elements of a Vector in R Programming - Using paste0() and sprintf() Function
paste0() and sprintf() functions in R Language can also be used to add leading zeros to each element of a vector passed to it as argument. Syntax: paste0("0", vec) or sprintf("%0d", vec)Parameters: paste0: It will add zeros to vector sprintf: To format a vector(adding zeros) vec: Original vector dataReturns: Vectors by addition of leading zeros Exa
1 min read
Clustering in R Programming
Clustering in R Programming Language is an unsupervised learning technique in which the data set is partitioned into several groups called clusters based on their similarity. Several clusters of data are produced after the segmentation of data. All the objects in a cluster share common characteristics. During data mining and analysis, clustering is
6 min read
Compute Variance and Standard Deviation of a value in R Programming - var() and sd() Function
var() function in R Language computes the sample variance of a vector. It is the measure of how much value is away from the mean value. Syntax: var(x) Parameters: x : numeric vector Example 1: Computing variance of a vector # R program to illustrate # variance of vector # Create example vector x <- c(1, 2, 3, 4, 5, 6, 7) # Apply var function in
1 min read
Compute Density of the Distribution Function in R Programming - dunif() Function
dunif() function in R Language is used to provide the density of the distribution function. Syntax: dunif(x, min = 0, max = 1, log = FALSE) Parameters: x: represents vector min, max: represents lower and upper limits of the distribution log: represents logical value for probabilities Example 1: # Create vector of random deviation u <- runif(20)
1 min read
Compute Randomly Drawn F Density in R Programming - rf() Function
rf() function in R Language is used to compute random density for F Distribution. Syntax: rf(N, df1, df2) Parameters: N: Sample Size df: Degree of Freedom Example 1: # R Program to compute random values # of F Density # Setting seed for # random number generation set.seed(1000) # Set sample size N <- 20 # Calling rf() Function y <- rf(N, df1
1 min read
Data Handling in R Programming
R Programming Language is used for statistics and data analytics purposes. Importing and exporting of data is often used in all these applications of R programming. R language has the ability to read different types of files such as comma-separated values (CSV) files, text files, excel sheets and files, SPSS files, SAS files, etc. R allows its user
5 min read
Return a Matrix with Lower Triangle as TRUE values in R Programming - lower.tri() Function
lower.tri() function in R Language is used to return a matrix of logical values with lower triangle as TRUE. Syntax: lower.tri(x, diag) Parameters: x: Matrix object diag: Boolean value to include diagonal Example 1: # R program to print the # lower triangle of a matrix # Creating a matrix mat <- matrix(c(1:9), 3, 3, byrow = T) # Calling lower.tr
1 min read
Print the Value of an Object in R Programming - identity() Function
identity() function in R Language is used to print the value of the object which is passed to it as argument. Syntax: identity(x) Parameters: x: An Object Example 1: # R program to print # the value of an object # Creating a vector x <- c(1, 2, 3, 4) # Creating a list list1 <- list(letters[1:6]) # Calling the identity function identity(x) ide
1 min read
Check if Two Objects are Equal in R Programming - setequal() Function
setequal() function in R Language is used to check if two objects are equal. This function takes two objects like Vectors, dataframes, etc. as arguments and results in TRUE or FALSE, if the Objects are equal or not. Syntax: setequal(x, y) Parameters: x and y: Objects with sequence of items Example 1: # R program to illustrate # the use of setequal(
1 min read
Random Forest with Parallel Computing in R Programming
Random Forest in R Programming is basically a bagging technique. From the name we can clearly interpret that this algorithm basically creates the forest with a lot of trees. It is a supervised classification algorithm. In a general scenario, if we have a greater number of trees in a forest it gives the best aesthetic appeal to all and is counted as
4 min read
R - Object Oriented Programming
In this article, we will discuss Object-Oriented Programming (OOPs) in R Programming Language. We will discuss the S3 and S4 classes, the inheritance in these classes, and the methods provided by these classes. OOPs in R Programming Language:In R programming, OOPs in R provide classes and objects as its key tools to reduce and manage the complexity
7 min read
Check for Presence of Common Elements between Objects in R Programming - is.element() Function
is.element() function in R Language is used to check if elements of first Objects are present in second Object or not. It returns TRUE for each equal value. Syntax: is.element(x, y) Parameters: x and y: Objects with sequence of items Example 1: # R program to illustrate # the use of is.element() function # Vector 1 x1 <- c(1, 2, 3) # Vector 2 x2
1 min read
Article Tags :
three90RightbarBannerImg