Types of Vectors in R Programming
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
- 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 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.
 1 4 2 5  "integer"
Double vectors are also known as numeric vectors in R. This includes decimal values with precision. In R, numbers are double by default.
 4 5 6 7  "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().
 TRUE FALSE TRUE NA  "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.
 "geeks" "2" "hello" "57"  "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.
 1+2i 0+3i 4-5i -12+6i  "complex"
Raw vectors store raw bytes of data. Making raw vectors gets complicated, but raw vectors can be created using the raw() function.
 00 00 00  "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().
[]  1 []  2 []  3  "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.
Please Login to comment...