To create a list of size N in R, you can use the rep
function or the vector
function.
In this example,
rep
function to create a list of size N with each element initialized to a default value.rep
function repeats the default value N times, creating a list of that size.size <- 5
default_value <- 0
list_rep <- rep(default_value, size)
cat('List of size', size, 'with default values:', list_rep, '\n')
List of size 5 with default values: 0 0 0 0 0
In this example,
vector
function to create a list of size N with each element initialized to a default value.vector
function initializes a list of the specified type and length, filled with default values.size <- 5
default_value <- 0
list_vector <- vector(mode = 'numeric', length = size)
list_vector <- rep(default_value, size)
cat('List of size', size, 'with default values:', list_vector, '\n')
List of size 5 with default values: 0 0 0 0 0
In this tutorial, we learned How to create a List of size N in R language with well detailed examples.