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 Vector
- Integer
- Double
- Logical
- Character
- Complex
- Raw
- Recursive Vector
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.
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
v1 <- c (1L, 4L, 2L, 5L)
print (v1)
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
v1 <- c (4, 5, 6, 7)
print (v1)
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
v1 <- c ( TRUE , FALSE , TRUE , NA )
print (v1)
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
v1 <- c ( 'geeks' , '2' , 'hello' , 57)
print (v1)
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
v1 <- c (1+2i, 3i, 4-5i, -12+6i)
print (v1)
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
print ( raw (3))
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
l1 <- list (1, 2, 3)
print (l1)
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.