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:**

`# 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*

*filter_none*

**Output :**

[1] 1 [1] 5

**Example 2:**

`# 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*

*filter_none*

**Output:**

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