To get the length of a vector in R, you can use the length
function.
In this example,
num_vector
with a set of numeric values using the c
function, which combines the values into a vector.length
function to determine the number of elements in num_vector
. The length
function takes a vector as its argument and returns the number of elements in that vector.length
function to a variable named num_vector_length
.print
function to display the value of num_vector_length
on the console.num_vector <- c(10, 20, 30, 40, 50)
num_vector_length <- length(num_vector)
print(num_vector_length)
[1] 5
In this example,
char_vector
with a set of string values using the c
function, which combines the string values into a vector.length
function to determine the number of elements in char_vector
. The length
function takes a vector as its argument and returns the number of elements in that vector.length
function to a variable named char_vector_length
.print
function to display the value of char_vector_length
on the console.char_vector <- c("apple", "banana", "cherry")
char_vector_length <- length(char_vector)
print(char_vector_length)
[1] 3
In this tutorial, we learned How to Get Length of a Vector in R language with well detailed examples.