Updated March 23, 2023
Introduction to Vectors in R
Vectors in R language is a standard data structure for storing and retrieving similar types of data. This is the simplest form of variable storage in R language which is one dimensional. R language supports several built-in syntaxes to create the vector variable and assign the elements to it. These languages are supported all the primitive data types like integer, character and double and complex data types. It provides efficient indexing to access the element from it. R language users mostly use c() function in R to create the vectors while developing R scripting.
In a vector, the indexing always starts from 1, and we use [] for indexing and it is by using indexing we access the components of a vector.
There are various types of indexing:
- Indexing using position: Indexing starts from 1, we can use these index to print out/access that particular element in the vector.
- Logical indexing: Indexing is done by assigning true/false to the elements. A false index says that drop the element from the result.
- Negative value indexing: The indexes are assigned with negative values starting from -1. The elements mentioned with the negative index to get the output those elements will be dropped from the result.
- 0/1 indexing: This is similar to that of logical indexing where 0 corresponds to FALSE and 1 corresponds to TRUE.
Now that we have learned the basics let us dig deep into vectors and learn their types and different manipulation techniques that we can perform in R.
Types of Vectors in R
Vectors are broadly classified into two:
1. Atomic Vectors
Now let us try to understand the atomic vectors in R. Atomic vectors are homogeneous in nature, there are 4 important types of atomic vector they are:
Logical
They are the simplest form of a vector as they take only 3 values namely TRUE, FALSE and NA. These vectors are constructed using the combine function or with the help of comparison operators.
Example:
Here are some of the examples of logical vectors given below:
X<- c(TRUE, FALSE, NA)
X
Output:
X<- c(TRUE, FALSE, NA)
x<- 1:6 %% 3 == 0
x
Output:
Numeric
Numeric attributes are integers and doubles. Floating point numbers are represented by doubles, in R all the numbers are considered as double by default to define an integer we must append the keyword L after the number.
Example:
Here some of the examples of numeric vector which are explained below:
typeof(2)
Output:
typeof(2L)
Output:
Doubles usually have 4 special values such as (-Inf, NA, NaN, Inf) whereas integers have only one special value i.e. NA.
c(-1, 0, 1)/0
Output:
Character
This is the most complex type of atomic vector as a character is nothing but a string and they can contain any amount of data. In R each of the unique strings will be stored only once as it uses as a global string pool and whenever the string is being used it points to this representation, in this way we reduce the amount of memory for any duplicated strings in R.
Example:
Here are some of the examples of characters vectors given below:
x<- 'attribute'
typeof(x)
Output:
2. Recursive Vectors (Lists)
Lists are heterogeneous in nature; a list can contain another list as its element. A list is created using the Keyword list() in R. It can also contain different types of objects, this an advantage to that of atomic vectors.
Example:
x<-c(list(1, 'a' , 2.5))
x
Output:
Now, we will see two important properties of a vector:
Type
The type of a vector can be determined by using typeof() function in R,
typeof(2);
Output:
Length
The length of a vector is determined using the function length(),
x<-c(1,2,3);
length(x)
Output:
Vector Functions in R
The function is defined as a piece of code used to perform a task. Functions are treated as other types of objects.
1. R rep() function
As we can refer from the name, this function is used to repeat the values given as an input in a function.
Syntax: rep()
Example
rep(c(1, 2, 3), times = 4)
Output:
[1] 1 2 3 1 2 3 1 2 3 1 2 3There are different ways of implementing the rep function.
We can also input the number of times a particular value needs to be repeated.
Example
rep(c(1, 2), times = c(4,3))
Output:
[1] 1 1 1 1 2 2 2The third way of implementing a rep function is by specifying the length.
Example
rep(1:2,length.out=9)
Output:
[1] 1 2 1 2 1 2 1 2 12. R Seq function
The sequence function is used to create a set of sequential values.
Let’s suppose we want to create a set of sequential integers. We can use the sequence function to create them.
Syntax: seq()
Example:
seq(from = 3.5, to = 1, by = -0.3)
Output:
[1] 3.5 3.2 2.9 2.6 2.3 2.0 1.7 1.4 1.1Additionally, We can also add another attribute to the input of the function (i.e length)
Example
seq(from = 3.5, to = 1, length.out = 6)
Output;
[1] 3.5 3.0 2.5 2.0 1.5 1.03. R any() function
Any() function takes input values and returns logical vectors which either True or False.
Syntax : any()
Let’s assume we have assigned a set of values to x.
x = 1,2,3,4,5
Syntax:
x <- 1:5
Now, we use any() function to see if there is any value above 5.
any(x > 5)
Output:
FALSE From a similar example, We can also output TRUE by changing the value from 5 to 3.
any(x > 3)
Output:
TRUE
4. R all() function
The all() function is similar to any() function.The difference here is it checks for every value and then prints the output. Even if a single value doesn’t meet the condition specified, it will print as FALSE.
Syntax: all()
Example
1. When all values meet the condition
x <- 1:5all(x > 0)
Output: TRUE
2.When values don’t meet the condition
x <- 1:5all(x > 3)
Output: FALSE
In the above example, only two values are above 3. The functions in R is defined by Rf_ or R_
5. ‘:’ Operator: The ‘:’ operator is used to create a vector of consecutive numbers.
Example:
x<- 1:7;
x
Output:
Vector Operations
There are various vector operations we can perform in R, a few of which are explained below:
1. Arithmetic operations
Similar to any data structure all the arithmetic operations like addition, subtraction, multiplication, and division can be performed in R with the vectors of the same length.
There are a few examples of arithmetic operations which are explained below.
Addition
A<- c(2, 4, 6, 8)
B<- c(4, 16, 36, 64)
print(A+B)
Output:
Subtraction
A<- c(2, 4, 6, 8)
B<- c(4, 16, 36, 64)
print(A-B)
Output:
Multiplication
A<- c(2, 4, 6, 8)
B<- c(4, 16, 36, 64)
print(A *B)
Output:
Division
A<- c(2, 4, 6, 8)
B<- c(4, 16, 36, 64)
print(A/B)
output:
2. Element Recycling
When two different vectors of different lengths are used in any of the above-mentioned arithmetic operations then the shorter size vector gets recycled to complete the operations,
Example:
X<- c(1, 2, 3)
Y<- c(5)
print(X+Y)
Output:
3. Sorting
By using the sort() function that R package offers we can sort our elements. By default if we use the sort function it will sort the vector in ascending order, to get the descending order we need to use the keyword revsort() and mention that decreasing = TRUE.
Example:
Z<- c(-1, -8, 3, 2, 9)
sort(Z);
Output:
Z<- c(-1, -8, 3, 2, 9)
sort(Z, decreasing=TRUE);
Output:
4. Modify
We can also modify our vector elements with the help of the assignment operator by using the indexing technique and also we can truncate the elements of a vector with the help of reassignments.
Example:
W<-c(-5, -2, 0, 3, 6)
W[2] <- 1;
W
Output:
W<-c(-5, -2, 0, 3, 6)
W[W<0]<- 2;
Output:
W<-c(-5, -2, 0, 3, 6)
W<- W[1:4];
Output:
4. Deletion
Anything which can be created can be deleted as well. In R we just assign NULL to any vector to delete that particular vector.
Example:
V<- c(1, 2, 3, 4, 5)
V<- NULL;
V
Output:
Conclusion
Applications of R vectors are of abundance in nature. It is majorly used in PCA which is one of the most important feature reduction techniques. It is also widely used in SVM and Neural networks. With the above content, we have learned what it is and how do we define a vector and we also discussed various vector manipulation techniques as well.
Recommended Articles
This is a guide to Vectors in R. Here we discuss the types of vectors in R and vector Operations along with various examples and their functions. You may also look at the following articles to learn more –