To create a vector of size N in R, you can use functions like numeric()
, integer()
, character()
, or logical()
, which create vectors of specified lengths filled with default values.
In this example,
numeric()
function to create a numeric vector. This function takes a single argument, n
, which specifies the size of the vector.numeric()
function initializes a vector of length n
with all elements set to 0.num_vector
.print()
function to display its contents.n <- 5
num_vector <- numeric(n)
print(num_vector)
[1] 0 0 0 0 0
In this example,
integer()
function to create an integer vector. This function takes a single argument, n
, which specifies the size of the vector.integer()
function initializes a vector of length n
with all elements set to 0.int_vector
.print()
function to display its contents.n <- 5
int_vector <- integer(n)
print(int_vector)
[1] 0 0 0 0 0
In this example,
character()
function to create a character vector. This function takes a single argument, n
, which specifies the size of the vector.character()
function initializes a vector of length n
with all elements set to an empty string ""
.char_vector
.print()
function to display its contents.n <- 5
char_vector <- character(n)
print(char_vector)
[1] "" "" "" "" ""
In this example,
logical()
function to create a logical vector. This function takes a single argument, n
, which specifies the size of the vector.logical()
function initializes a vector of length n
with all elements set to FALSE
.log_vector
.print()
function to display its contents.n <- 5
log_vector <- logical(n)
print(log_vector)
[1] FALSE FALSE FALSE FALSE FALSE
In this tutorial, we learned How to create a Vector of Size N in R language with well detailed examples.