Open In App
Related Articles

Getting and Setting Length of the Vectors in R Programming – length() Function: Title change need

Improve Article
Improve
Save Article
Save
Like Article
Like

length() function in R Programming Language is used to get or set the length of a vector (list) or other objects.

Getting the length of object in R Programming

Here we are going to get the length of the vector in R Programming, for this we will use length() function.

Syntax: length(x)

Parameters: 

  • x: vector or object

Example 1: Getting the length of the vector

R




# R program to illustrate
# length function
 
# Specifying some vectors
x <- c(6)
y <- c(1, 2, 3, 4, 5)
 
# Calling length() function
# to get length of the vectors
length(x)
length(y)

Output : 

[1] 1
[1] 5

Example 2: Getting the length of the matrix

R




A = matrix(
      
  # Taking sequence of elements
  c(1, 2, 3, 4, 5, 6, 7, 8, 9),
    
  # No of rows
  nrow = 3, 
    
  # No of columns
  ncol = 3,       
    
  # By default matrices are in column-wise order
  # So this parameter decides how to arrange the matrix
  byrow = TRUE        
)
print(A)
 
# length of A
length(A)

Output:

     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9

9

Example 3: Getting the length of the Dataframe

Here BOD is dataframe has 6 rows and 2 columns giving the biochemical oxygen demand versus time in an evaluation of water quality. and we are going to get the length of this dataframe.

R




print(BOD)
 
length(BOD)

Output:

  Time demand
1    1    8.3
2    2   10.3
3    3   19.0
4    4   16.0
5    5   15.6
6    7   19.8

2

Note: If the parameter is a matrix or dataframe, it returns the number of variables:

Example 4: Getting the length of the list

R




# R program to create a List and get the len
 
# The first attributes is a numeric vector
# containing the employee IDs which is created
# using the command here
empId = c(1, 2, 3, 4)
 
# The second attribute is the employee name
# which is created using this line of code here
# which is the character vector
empName = c("Debi", "Sandeep", "Subham", "Shiba")
 
# The third attribute is the number of employees
# which is a single numeric variable.
numberOfEmp = 4
 
# We can combine all these three different
# data types into a list
# containing the details of employees
# which can be done using a list command
empList = list(empId, empName, numberOfEmp)
 
print(empList)
 
print("Length of the list:")
length(empList)

Output:

[[1]]
[1] 1 2 3 4

[[2]]
[1] "Debi"    "Sandeep" "Subham"  "Shiba"  

[[3]]
[1] 4

[1] "Length of the list:"

3

Example 5: Getting the length of the string

In R Language, we can not easily get the length of the string, first, we have to get the character of the string using split and then unlist each character to count the length.

R




# R program to split a string
 
# Given String
string <- "Geeks For Geeks"
 
# Basic application of length()
length(string)  
 
# unlist the string and then count the length
length(unlist(strsplit(string, "")))

Output:

1
15

Setting length of the object in R Programming

Here we are going to set the length of the vector in R Programming, for this we will use length() function.

Syntax: length(x) <- value

Parameters: 

  • x: vector or object

R




# R program to illustrate
# length function
 
# Specifying some vectors
x <- c(3)
y <- c(1, 2, 3, 4, 5)
z <- c(1, 2, 3, 4, 5)
 
# Setting length of the vector
length(x) <- 2
length(y) <- 7
length(z) <- 3
 
# Getting elements of the
# new vectors
x
y
z

Output: 

[1]  3 NA
[1]  1  2  3  4  5 NA NA
[1] 1 2 3

Last Updated : 07 Dec, 2021
Like Article
Save Article
Similar Reads