Open In App

Types of Vectors in R Programming

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Vectors in R programming are the same as the arrays in C language which are used to hold multiple data values of the same type. One major key point is that in R the indexing of the vector will start from ‘1’ and not from ‘0’. Vectors are the most basic data types in R. Even a single object created is also stored in the form of a vector. Vectors are nothing but arrays as defined in other languages. Vectors contain a sequence of homogeneous types of data. There are mainly two types of vectors in R. The complete classification vectors in R are given below. 

  1. Atomic Vector
    • Integer
    • Double
    • Logical
    • Character
    • Complex
    • Raw
  2. Recursive Vector
    • List

The main difference between atomic vectors and recursive vector(list) is that atomic vectors are homogeneous, whereas the recursive vector(list) can be heterogeneous. Vectors have three common properties:

  1. Type, typeof(), what it is.
  2. Length, length(), how many elements it contains.
  3. Attributes, attributes(), additional arbitrary metadata.

Atomic Vectors

Atomic vectors are probably the most fundamental data structure in the R programming language. An atomic vector is different from a one-dimensional array: an array has a dim attribute of length one while a vector has no such attribute. An atomic vector is also different from a list. The elements of a vector are all of the same types while a list can contain any arbitrary type. Atomic vectors are constructed with the c() function or the vector function.

Integer Vector

Integer vectors are also known as numeric vectors in R. This includes negative and positive whole values. In R, numbers are double by default so to make an integer, place L after the number.

Example:

R




# R program to create integer Vectors 
  
# creation of integer vectors
# using c() function. 
v1 <- c(1L, 4L, 2L, 5L) 
  
# print vector
print(v1)
  
# display type of vector 
print(typeof(v1))


Output:

[1] 1 4 2 5
[1] "integer"

Double Vector

Double vectors are also known as numeric vectors in R. This includes decimal values with precision.  In R, numbers are double by default. 

Example:

R




# R program to create double Vectors 
  
# creation of double vectors
# using c() function. 
v1 <- c(4, 5, 6, 7) 
  
# print vector
print(v1)
  
# display type of vector 
print(typeof(v1))


Output:

[1] 4 5 6 7
[1] "double"

Logical Vector

Logical vectors are the simplest type of atomic vector as they take only three possible values: FALSE, TRUE, and NA. Logical vectors can be constructed with comparison operators.we can also create these using c().

Example:

R




# R program to create Logical Vectors 
  
# Creating logical vector 
# using c() function 
v1 <- c(TRUE, FALSE, TRUE, NA
  
# print vector
print(v1)
  
# display type of vector 
print(typeof(v1))


Output:

[1]  TRUE FALSE  TRUE    NA
[1] "logical"

Character Vector

Character vectors are the most complex type of atomic vector because each element of a character vector is a string and a string can contain an arbitrary amount of data.strings in R can contain the alphabets, numbers, and symbols. The easiest way to denote that a value is of character type in R is to wrap the value inside single or double inverted commas. We can even use the as.character() function to store a value as a character or to convert a value to the character data type.

Example:

R




# R program to create Character Vectors 
  
# by default numeric values  
# are converted into characters 
v1 <- c('geeks', '2', 'hello', 57)  
  
# print vector
print(v1)
  
# display type of vector 
print(typeof(v1))


Output:

[1] "geeks" "2"     "hello" "57"   
[1] "character"

Complex Vector

The complex data type is to store numbers with an imaginary component. Examples of complex values are 1+2i, 3i, 4-5i, -12+6i, etc.

Example:

R




# R program to create complex Vectors 
  
# create complex vector
v1 <- c(1+2i, 3i, 4-5i, -12+6i)  
  
# print vector
print(v1)
  
# display type of vector 
print(typeof(v1))


Output:

[1]   1+2i   0+3i   4-5i -12+6i
[1] "complex"

Raw Vector

Raw vectors store raw bytes of data. Making raw vectors gets complicated, but raw vectors can be created using the raw() function.

Example:

R




# R program to illustrate raw vector
  
# Creating raw vector using raw()
print(raw(3))
  
# Print the type of vector
print(typeof(raw(3)))


Output:

[1] 00 00 00
[1] "raw"

Recursive Vector

Lists are stepped up in complexity from atomic vectors, because the list can contain other lists. This makes them suitable for representing hierarchical or tree-like structures. We can create a list with list().

Example:

R




# R program to create recursive Vectors 
  
# create recursive Vectors
l1 <- list(1, 2, 3)  
  
# print vector
print(l1)
  
# display type of vector 
print(typeof(l1))


Output:

[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

[1] "list"

The list elements can be given names, and they can be accessed using these names. Lists can be extremely useful inside functions. Because the functions in R are able to return only a single object, you can “staple” together lots of different kinds of results into a single object that a function can return. A list does not print to the console like a vector. Instead, each element of the list starts on a new line. Elements are indexed by double brackets. If the elements of a list are named, they can be referenced by the $ notation.



Last Updated : 28 Jul, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads