length Function in R (Example)
This page shows how to get the number of vector or list elements with the length() function in the R programming language.
Example Data
We use a simple vector of character strings as example data:
vec <- c("yo", "x", "example", "abcd", "gggg") # Create example vector |
vec <- c("yo", "x", "example", "abcd", "gggg") # Create example vector
Application of length Function in R
If we want to know the number of vector elements, we can use the length function as follows:
length(vec) # Apply length function # 5 |
length(vec) # Apply length function # 5
The RStudio console returns the value 5, i.e. our vector contains 5 vector elements.
Note that we could apply the length function exactly the same way to a list object.