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

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

Syntax: length(x)

Parameters:
x: vector or object

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to illustrate
# length function
  
# Specifing 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)

chevron_right


Output :

[1] 1
[1] 5

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to illustrate
# length function
  
# Specifing 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

chevron_right


Output:

[1]  3 NA
[1]  1  2  3  4  5 NA NA
[1] 1 2 3
My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.