Creating a List
To create a List in R you need to use the function called “list()“.
In other words, a list is a generic vector containing other objects. To illustrate how a list looks, we take an example here. We want to build a list of employees with the details. So for this, we want attributes such as ID, employee name, and the number of employees.
Example:
R
# R program to create a List # The first attributes is a numeric vector # containing the employee IDs which is created # using the command here empId = c (1, 2, 3, 4) # The second attribute is the employee name # which is created using this line of code here # which is the character vector empName = c ( "Debi" , "Sandeep" , "Subham" , "Shiba" ) # The third attribute is the number of employees # which is a single numeric variable. numberOfEmp = 4 # We can combine all these three different # data types into a list # containing the details of employees # which can be done using a list command empList = list (empId, empName, numberOfEmp) print (empList) |
[[1]] [1] 1 2 3 4 [[2]] [1] "Debi" "Sandeep" "Subham" "Shiba" [[3]] [1] 4
R – Lists
A list in R programming is a generic object consisting of an ordered collection of objects. Lists are one-dimensional, heterogeneous data structures.
The list can be a list of vectors, a list of matrices, a list of characters, a list of functions, and so on.
A list is a vector but with heterogeneous data elements. A list in R is created with the use of the list() function.
R allows accessing elements of an R list with the use of the index value. In R, the indexing of a list starts with 1 instead of 0.
Contact Us