Open In App

Getting and Setting Length of the Vectors in R Programming – length() Function

Last Updated : 30 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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: 

  • x: vector or object

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

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

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:

emplist

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-length

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:

change_matrix

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:

change_dataframe

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.



Next Article

Similar Reads