Types of Vectors in R Programming

28 Jul, 2020
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. 
Atomic VectorIntegerDoubleLogicalCharacterComplexRawRecursive VectorList
Atomic VectorIntegerDoubleLogicalCharacterComplexRaw
Integer
Double
Logical
Character
Complex
Raw
Recursive VectorList
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:
Type, typeof(), what it is.Length, length(), how many elements it contains.Attributes, attributes(), additional arbitrary metadata.
Type, typeof(), what it is.
Length, length(), how many elements it contains.
Attributes, attributes(), additional arbitrary metadata.
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 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 vectorprint(v1)  # display type of vector print(typeof(v1))
Output:
[1] 1 4 2 5
[1] "integer"

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 vectorprint(v1)  # display type of vector print(typeof(v1))
Output:
[1] 4 5 6 7
[1] "double"

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 vectorprint(v1)  # display type of vector print(typeof(v1))
Output:
[1]  TRUE FALSE  TRUE    NA
[1] "logical"

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 vectorprint(v1)  # display type of vector print(typeof(v1))
Output:
[1] "geeks" "2"     "hello" "57"   
[1] "character"

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 vectorv1 <- c(1+2i, 3i, 4-5i, -12+6i)    # print vectorprint(v1)  # display type of vector print(typeof(v1))
Output:
[1]   1+2i   0+3i   4-5i -12+6i
[1] "complex"

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 vectorprint(typeof(raw(3)))
Output:
[1] 00 00 00
[1] "raw"
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 Vectorsl1 <- list(1, 2, 3)    # print vectorprint(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.
Picked
R-Vectors
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Filter data by multiple conditions in R using Dplyr
How to Replace specific values in column in R DataFrame ?
Change Color of Bars in Barchart using ggplot2 in R
How to Split Column Into Multiple Columns in R DataFrame?
Loops in R (for, while, repeat)
Printing Output of an R Program
Group by function in R using Dplyr
How to change Row Names of DataFrame in R ?
How to Change Axis Scales in R Plots?
How to filter R DataFrame by values in a column?