Create Empty List of Specific Length
To create an empty list of specific length in R programming, call vector(mode, length) function and pass the value of “list” for mode, and an integer for length.
The default values of the items in the list would be NULL.
In this tutorial, we will learn how to create an empty list of specific length in R, using vector() function, with the help of example programs.
Syntax
The syntax to create an empty list of specific length in R is
myList <- vector("list", n)
where n
is the required length of this list.
Examples
In the following program, we will create an empty list of length 5
using vector() function. We print the type of the object returned by vector() function with “list” as mode, and the length of the vector.
example.R
x <- vector("list", 5)
print(typeof(x))
print(length(x))
Output
[1] "list"
[1] 5
Clearly, we created an empty list of length 5.
Print Contents
Now, let us print the contents of an empty list with specific length.
example.R
x <- vector("list", 5)
print(x)
Output
[[1]]
NULL
[[2]]
NULL
[[3]]
NULL
[[4]]
NULL
[[5]]
NULL
Conclusion
In this R Tutorial, we learned how to create an empty list of specific length in R programming using vector() function, with the help of examples.