R – Create List
To create a list in R programming, call list()
function. We may pass initial values for the list items to this list()
function as arguments.
In this tutorial, we will learn how to create a list in R, using list()
function, with the help of example programs.
Syntax
The syntax to create a list in R is
myList <- list(item1, item2, item3)
list()
function returns a new list formed with the items given, and is stored in myList
.
A list can contain elements of any type, and is mutable.
Examples
In the following program, we will create a list containing some string values using list()
function.
example.R
myList <- list("Sunday", "Monday", "Tuesday", "Wednesday")
print(myList)
Output
[[1]]
[1] "Sunday"
[[2]]
[1] "Monday"
[[3]]
[1] "Tuesday"
[[4]]
[1] "Wednesday"
Now, let us create a list containing some numbers and string values.
example.R
myList <- list(25, "Hello World")
print(myList)
Output
[[1]]
[1] 25
[[2]]
[1] "Hello World"
Conclusion
In this R Tutorial, we learned how to create a list with initial values in R programming using list() function, with the help of examples.