R – Vector Length
To get length of a vector in R programming, call length() function and pass the vector to it. length() function returns an integer, representing the length of vector.
In this tutorial, we will learn how to use length() function to find the length of a vector, with examples.
Syntax
The syntax to use length() function to get the length of a vector x is
</>
Copy
length(x)
Return Value
The function returns an integer.
Examples
In the following program, we take a vector in x
, and find its length using length() function.
example.R
</>
Copy
x <- c(2, 4, 6, 8)
vectorLength = length(x)
cat("Length of vector x is :", vectorLength)
Output
Length of vector x is : 4
Now, let us take a vector of strings, and find its length.
example.R
</>
Copy
x <- c("az", "bc", "mn")
vectorLength = length(x)
cat("Length of vector x is :", vectorLength)
Output
Length of vector x is : 3
Conclusion
In this R Tutorial, we learned how to find the length of a vector, using length() function, with the help of examples.