Vector Data Structure in R & How to Create Vectors

The R language is widely used in various fields like data science, scientific research, & statistical data analysis.

All of these fields deal with a large number of data sets, big data, or massive databases. Moreover, the R programming language is quite popular in data analytics and data engineering, where data is handled and molded according to underlying use cases.

R provides various data structures to store and work with massive datasets. One of these frequently used data structures is vectors. Let us begin by elaborating on the vector data structure in R.

What is a vector?

Vectors are the most fundamental objects in R. Simply these are R lists of similar items. In addition, vectors are similar to arrays in C or Java languages.

They act as a container that can hold different entities of the same data type. One significant difference between vectors in R and similar data structures (arrays, lists) in other languages is that indexing of vectors starts from 1 rather than 0.

Types of vectors in R

Vectors in R come in six different types. These types differ based on the data types of objects which they store. These types are as follows.

  1. Logical Vectors: stores logical data types like [TRUE, TRUE, FALSE, TRUE]
  2. Integer Vector: stores integers like [23, 10, 26, 5]
  3. Double Vectors: store doubles or floating point numbers like [1.2, 2.6, 2.5, 7.5]
  4. Complex Vectors: store complex numbers like [1 + 1i, 2 - 1i, 5 + 1i, 3 - 2i]
  5. Character Vectors: store variables of character data type like "ALGOIDEAS"
  6. Raw Vectors store fixed-length byte sequences like [0x6A, 0x7E, 0x12, 0x00]

These are some types of vectors available in R. Now, Let’s look at how to create a vector in R to store the data points. The data points will be stored in contiguous memory locations.

Vector Data Structure in R
Types of vectors in R

How to create vectors in R?

R offers multiple methods or ways to create vectors. Some of these are explained below.

Colon Operator

The colon operator (:) is used to create vectors of any type by defining a sequence whose starting point is on the left side of the colon operator, and the final point is on the right side. It will create a vector of the defined sequence, and the difference between every consecutive series term will be 1.

myVector <- 10 : 20

It will create a vector named myVector of sequence starting from 10 and ending at 20 (including boundary values). The vector created is [10 11 12 13 14 15 16 17 18 19 20].

myVector <- 10.5 : 20.4

It creates a vector of sequence starting from 10.5 and ending at 20.4. The vector created is [10.5 11.5 12.5 13.5 14.5 15.5 16.5 17.5 18.5 19.5].

Seq() Function

The sequence function can also be used to create a vector by defining a sequence. The difference between this operator and the colon operator is that we can determine the incrementing jump between every consecutive series term.

myVector <- seq(10, 15, by = 0.5)

It will create a vector myVector starting from 10 and ending at 15. Each consecutive value will differ by 0.5. The vector created is [10.0 10.5 11.0 11.5 12.0 12.5 13.0 13.5 14.0 14.5 15.0].

c() Function

In R, programmers can utilize the c() function to provide the specific values they want to store in the vector. These values still need to be of the same data type. If at least one value in c() is a character value, then all other values are converted to character data type and stored in the vector.

v <- c(1, 6, 9, -2, 55, 74, 21)

c() will create a vector v containing argument values as [1, 6, 9, -2, 55, 74, 21].

Stay in the Loop

Get the daily email from Algoideas that makes reading the news actually enjoyable. Join our mailing list to stay in the loop to stay informed, for free.

Latest stories

- Advertisement -

You might also like...