To initialize a vector in R, you can use the c()
function, which combines values into a vector.
In this example,
c()
function to create a numeric vector. The c()
function combines its arguments into a vector.c()
function, we provide numeric values separated by commas.num_vector
.print()
function to display its contents.num_vector <- c(1, 2, 3, 4, 5)
print(num_vector)
[1] 1 2 3 4 5
In this example,
c()
function to create a character vector. The c()
function combines its arguments into a vector.c()
function, we provide character values enclosed in double quotes and separated by commas.char_vector
.print()
function to display its contents.char_vector <- c("apple", "banana", "cherry")
print(char_vector)
[1] "apple" "banana" "cherry"
In this example,
c()
function to create a logical vector. The c()
function combines its arguments into a vector.c()
function, we provide logical values (TRUE
or FALSE
) separated by commas.log_vector
.print()
function to display its contents.log_vector <- c(TRUE, FALSE, TRUE)
print(log_vector)
[1] TRUE FALSE TRUE
In this tutorial, we learned How to Initialize a Vector in R language with well detailed examples.