Types of Vectors in R Programming
Last Updated :
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 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
# 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.
Similar Reads
Assigning Vectors in R Programming
Vectors are one of the most basic data structure in R. They contain data of same type. Vectors in R is equivalent to arrays in other programming languages. In R, array is a vector of one or more dimensions and every single object created is stored in the form of a vector. The members of a vector are
5 min read
Dot Product of Vectors in R Programming
In mathematics, dot product or commonly referred as the scalar product is an algebraic operation involving the two same-length sequences of numbers and a resultant single number. Let us assume two vectors A and B and we have to calculate the dot product of two vectors.Formula for Dot ProductDotProdu
2 min read
Cross Product of Vectors in R Programming
In mathematics, the cross product or also known as the vector product is a binary operation on two vectors in three-dimensional space and is denoted by the symbol 'X'. Given two linearly independent vectors a and b, the cross product, a à b is a vector that is perpendicular to both a and b and thus
4 min read
8 Coding Style Tips for R Programming
R is an open-source programming language that is widely used as a statistical software and data analysis tool. R generally comes with the Command-line interface. R is available across widely used platforms like Windows, Linux, and macOS. Also, the R programming language is the latest cutting-edge to
5 min read
Sorting of a Vector in R Programming - sort() Function
In R Programming Language we can sort a vector in ascending or descending order using the sort() function. The sort() function returns a sorted version of the input vector. sort() function in is used to sort a vector by its values. It takes the Boolean value as an argument to sort in ascending or de
2 min read
Data Types in Programming
In Programming, data type is an attribute associated with a piece of data that tells a computer system how to interpret its value. Understanding data types ensures that data is collected in the preferred format and that the value of each property is as expected. Data Types in Programming Table of Co
11 min read
Operations on Vectors in R
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. If mixed values are given then it auto converts the data according t
4 min read
Data Structures in R Programming
A data structure is a particular way of organizing data in a computer so that it can be used effectively. The idea is to reduce the space and time complexities of different tasks. Data structures in R programming are tools for holding multiple values. Râs base data structures are often organized by
6 min read
Append Operation on Vectors in R Programming
In this article, let us discuss different methods to concatenate/append values to a vector in R Programming Language. Append method in RVectors in the R Programming Language is a basic objects consisting of sequences of homogeneous elements. vector can be integer, logical, double, character, comple
2 min read
Types of Vectors
Vector is a physical quantity that has both direction and magnitude, it is a quantity that not only tells us about its strength but also gives us details about the direction in which it is acting upon. There are various types of vectors based on different parameters like their direction, magnitude,
9 min read