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

07 Dec, 2021
length() function in R Programming Language is used to get or set the length of a vector (list) or other objects.
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
R
# R program to illustrate# length function # Specifying some vectorsx <- c(6)y <- c(1, 2, 3, 4, 5) # Calling length() function# to get length of the vectorslength(x)length(y)
Output : 
[1] 1
[1] 5
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 Alength(A)
Output:
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9

9
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:
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 hereempId = 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 vectorempName = 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 commandempList = 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
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 Stringstring <- "Geeks For Geeks" # Basic application of length()length(string)   # unlist the string and then count the lengthlength(unlist(strsplit(string, "")))
Output:
1
15
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 vectorsx <- c(3)y <- c(1, 2, 3, 4, 5)z <- c(1, 2, 3, 4, 5) # Setting length of the vectorlength(x) <- 2length(y) <- 7length(z) <- 3 # Getting elements of the# new vectorsxyz
Output: 
[1]  3 NA
[1]  1  2  3  4  5 NA NA
[1] 1 2 3
arorakashish0911
kumar_satyam
simmytarika5
R Object-Function
R Vector-Function
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Replace specific values in column in R DataFrame ?
How to change Row Names of DataFrame in R ?
Filter data by multiple conditions in R using Dplyr
Loops in R (for, while, repeat)
Change Color of Bars in Barchart using ggplot2 in R
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
K-Means Clustering in R Programming
Remove rows with NA in one column of R DataFrame