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

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

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.

Video Examples for the length Function

YouTube

By loading the video, you agree to YouTube’s privacy policy.
Learn more

Load video

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top