Getting and Setting Length of the Vectors in R Programming – length() Function
Last Updated :
30 Apr, 2025
In R, the length()
function is used to determine the number of elements in a vector. Vectors can be of various types, such as numeric, character, or logical, and the length()
function provides a simple way to find out how many elements are contained in a vector. This function is versatile and can also be used to set the length of vectors (or lists) and other objects in R.
Here we are going to get the length of the vector in R Programming, for this, we will use the length() function.
Syntax:
length(x)
Parameters:
Example 1: Getting the length of the vector
We are creating two vectors, x
and y
. The vector x
contains a single number, 6, while the vector y
contains five numbers: 1, 2, 3, 4, and 5. After that, we are using the length()
function to check how many elements each vector has
R
x <- c(6)
y <- c(1, 2, 3, 4, 5)
cat("Number of elements in the x and y vectors are", length(x),"and",length(y),"respectively.")
Output :
Number of elements in the x and y vectors are 1 and 5 respectively.
Example 2: Getting the length of the matrix
We are creating a 3×3 matrix “A"
using the matrix()
function, where the elements are taken from the sequence 1 to 9
. The matrix will have 3 rows (nrow = 3
) and 3 columns (ncol = 3
). By setting byrow = TRUE
, we are specifying that the matrix should be filled with values row-wise. Then we use the length()
function to find the total number of elements in the matrix A
.
R
A = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9),
nrow = 3, ncol = 3, byrow = TRUE
)
return(A)
cat("Number of elemts in the the matrix are: ",length(A))
Output:

Matrix Length
Example 3: Getting the length of the Data frame
We are printing the built-in dataset BOD
in R, which contains the biochemical oxygen demand (BOD) data for 6 different days. The print(BOD)
function is used to displayt this dataset in the console. After that, we use the length()
function to check how many elements are in the BOD
dataset.
R
print(BOD)
cat("Number of elemts in BOD are:",length(BOD))
Output:

BOD dataset
Example 4: Getting the length of the list
We are creating a list empList
that contains three elements: empId
(a vector with employee IDs), empName
(a vector with employee names), and numberOfEmp
(a single number representing the total number of employees). The list()
function is used to combine these elements into a list. After creating the list, we use return(empList)
to return the list. Then, we use cat()
to display the length of the list using length(empList)
.
R
empId = c(1, 2, 3, 4)
empName = c("Debi", "Sandeep", "Subham", "Shiba")
numberOfEmp = 4
empList = list(empId, empName, numberOfEmp)
return(empList)
cat("Length of the list:",length(empList))
Output:

Employee lIst
Example 5: Getting the length of the string
We are defining a string
as “Geeks For Geeks”. Then, we use strsplit()
to split the string into individual characters and unlist()
to convert the list into a vector. The length()
function is then used to count the number of characters in the string, Finally, the cat()
function is used to print the number of characters in the string.
R
string <- "Geeks For Geeks"
l <- length(unlist(strsplit(string, "")))
cat("The Number of Characters in the string is :",l)
Output:
The Number of Characters in the string is : 15
Setting length of the object in R Programming
We can also set the length of the vector in R Programming using the length() function.
Syntax: length(x) <- value
Parameters:
- x: vector or object
- value is used to the set number of elements.
Example 1: Getting and Setting the Length of vectors
We are creating three vectors: x
, y
, and z
. The vector x
initially contains a single element (3
), y
contains five elements (1, 2, 3, 4, 5
), and z
also contains five elements (1, 2, 3, 4, 5
). Then, we modify the length of these vectors using the length()
function. For x
, we set the length to 2, which causes it to be extended with NA
values (since only one element was originally in x
). We do the same for y and z vectors.
R
x <- c(3)
y <- c(1, 2, 3, 4, 5)
z <- c(1, 2, 3, 4, 5)
length(x) <- 2
length(y) <- 7
length(z) <- 3
x
y
z
Output:

Setting Custom Length
Example 2: Getting and Setting the Length of a Matrix
We are creating a 2×3 matrix m
with values from 1 to 6. Then, we use the length()
function to change the length of the matrix to 4. Since matrices are stored in column-wise order, this operation reduces the matrix to only the first 4 elements. Finally, we print the modified matrix.
R
m <- matrix(1:6, nrow = 2, ncol = 3
head(m)
length(m) <- 4
print(m)
Output:

setting custom length of martix
Example 3: Getting and Setting the Length of a Data Frame
We are creating a data frame df
with two columns: A
containing numbers 1 to 5, and B
containing letters a to e. Then, we use the length()
function to modify the length of the data frame to 3, which truncates the data frame to only the first 3 rows. Finally, we print the modified data frame.
R
df <- data.frame(A = 1:5, B = letters[1:5])
head(df)
length(df) <- 3
head(df)
Output:

Setting custom length of a data frame
In this article, we explored how to get and set the length of vectors in R using the length()
function.
Similar Reads
Getting and Setting Length of the Vectors in R Programming - length() Function: Title change need
In R, the length() function is used to determine the number of elements in a vector. Vectors can be of various types, such as numeric, character, or logical, and the length() function provides a simple way to find out how many elements are contained in a vector. This function is versatile and can al
5 min read
Finding the length of string in R programming - nchar() method
nchar() method in R Programming Language is used to get the length of a character in a string object. Syntax: nchar(string) Where: String is object. Return: Returns the length of a string. R - length of string using nchar() ExampleExample 1: Find the length of the string in R In this example, we ar
2 min read
Generate Color Vectors of desired Length in R Programming - rainbow() Function
rainbow() function in R Language is a built in color palettes which can be used to quickly generate color vectors of desired length taken as the parameter and returns the hexadecimal code of the colours available. This hexadecimal code is of eight digits. This is because the last two digits specify
1 min read
Get the Last parts of a Data Set in R Programming - tail() Function
tail() function in R Language is used to get the last parts of a vector, matrix, table, data frame or function. Syntax: tail(x, n) Parameters: x: specified data types n: number of row need to be printed Example 1: # R program to illustrate # tail function # Calling the tail() function to # get the l
1 min read
Get the Minimum and Maximum element of a Vector in R Programming - range() Function
range() function in R Programming Language is used to get the minimum and maximum values of the vector passed to it as an argument. Syntax: range(x, na.rm, finite) Parameters: x: Numeric Vectorna.rm: Boolean value to remove NAfinite: Boolean value to exclude non-finite elementsR - range() Function E
1 min read
Replicating a Value to the Specified Length in R Programming - rep_len() Function
rep_len() function in R Language is used to replicate a given value in the specified length. Syntax: rep_len(x, length.out) Parameters: x: specified value length.out: specified number of items get printed Example 1: # R program to illustrate # rep_len function # Calling the rep_len() function rep_le
1 min read
Get or Set the Structure of a Vector in R Programming - structure() Function
structure() function in R Language is used to get or set the structure of a vector by changing its dimensions. Syntax: structure(vec, dim) Parameters: vec: Vector dim: New Dimensions Example 1: # R program to get # the structure of a Vector # Creating a Vector # of Numbers x1 <- c(1:10) structure
1 min read
Generating sequenced Vectors in R Programming - sequence() Function
sequence() function in R Language is used to create a vector of sequenced elements. It creates vectors with specified length, and specified differences between elements. It is similar to seq() function. Syntax: sequence(x) Parameters: x: Maximum element of vector Example 1: # R program to create seq
1 min read
Get the number of rows of an Object in R Programming - nrow() Function
nrow() function in R Language is used to return the number of rows of the specified matrix. Syntax: nrow(x)Parameters: x: matrix, vector, array or data frame Example 1: C/C++ Code # R program to illustrate # nrow function # Getting R Biochemical Oxygen Demand Dataset BOD # Calling nrow() function to
1 min read
Getting a Matrix of number of rows in R Programming - row() Function
In this article, we will discuss how to find the number of rows and columns in a matrix in R Programming Language. nrow() Function in Rnrow() function in R Language is used to get the row number of a matrix. Syntax: row(x, as.factor=FALSE) Parameters:x: matrixas.factor: a logical value indicating wh
2 min read