Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

function
r
length()
communitycreator

How to get the length of a vector in R

Onyejiaku Theophilus Chidalu

Overview

In R, the length() function is used to get the length of the vector. In simpler terms, it is used to find out how many items are present in a vector.

Syntax

length(x)

Parameter value

The length function takes a vector object as its single parameter value.

Return value

The length() function returns the length of the vector object that is passed to it in integer form.

Code example

In the code given below, we will use the length() function to determine the length of a vector:

# creating a vector variable
my_vector <- c(1, 2, 3, 4, 5) 

# using the length() function 
length(my_vector)
Calling the "length()" function on a vector object

Code explanation

  • Line 2: We create a vector, using the c() function.
  • Line 5: We use the length() function to determine the number of items that are present in the my_vector vector, which is 5.

RELATED TAGS

function
r
length()
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu
RELATED COURSES

View all Courses

Keep Exploring