How to Create a Vector of Zero Length in R

Last Updated : 23 Aug, 2021

In this article, we are going to discuss how to create a vector of zero length in R programming language.

We can create a vector of zero length by passing the datatype as a value to the vector.

Syntax:

vector=datatype()

There are six data types and each can be used to create a vector of the required datatype. Let’s see how to create a vector of 0 length of each data type.

character()

It is used to create a Numeric vector of 0 length.

Syntax:

vector = character()

Example: R program to create a numeric vector of 0 length and display

R

 `# Numeric vector of 0 length ` `vector < - ``character``() ` ` `  `# display ` `print``(vector) `

Output:

character(0)

factor()

It is used to create a Factor vector of 0 length

Syntax:

vector = factor()

Example: R program to create a factor vector of length 0 and display

R

 `# Factor vector of 0 length ` `vector = ``factor``()        ` ` `  `# display ` `print``(vector) `

Output:

factor(0)

Levels:

logical()

It is used to create a logical vector of 0 length.

Syntax:

vector = logical()

Example: R program to create a logical vector  of length 0 and display

R

 `# logical vector of 0 length ` `vector = ``logical``()        ` ` `  `# display ` `print``(vector) `

Output:

logical(0)

integer()

It is used to create an Integer vector of 0 length.

Syntax:

vector = integer ()

Example: R program to create an integer vector of length 0 and display

R

 `# Integer vector of 0 length ` `vector = ``integer``()        ` ` `  `# display ` `print``(vector)        `

Output:

integer(0)

double()

It is used to create a double vector of 0 length.

Syntax:

vector = double()

Example: R program to create a double vector of 0 length and display

R

 `# double vector of 0 length ` `vector = ``double``()        ` ` `  `# display ` `print``(vector)         `

Output:

numeric(0)

raw()

It is used to create a raw vector of 0 length.

Syntax:

vector = raw()

Example: R program to create a Raw vector of length 0 and display

R

 `# raw vector of 0 length ` `vector = ``raw``()        ` ` `  `# display ` `print``(vector) `

Output:

raw(0)

complex()

It is used to create a complex vector of 0 length.

Syntax:

vector = complex()

Example: R program to create a complex vector of 0 length and display

R

 `# complex vector of 0 length ` `vector = ``complex``()        ` ` `  `# display ` `print``(vector) `

Output:

complex(0)