Being the most important data structure in R, vectors have their own importance and ways of handling things under R Programming. However, when it comes to the creation as well as manipulation of vectors, there are certain functions we have under R programming which make a programmer's task easier at the end of the day doing vector manipulation.
Throughout this article, we will be discussing the functions that can be used on vectors, their relevance, and importance with hands-on Examples. Let us get going.
Vector functions under R the ones that allow us to either create or manipulate the data structure called vectors. These functions most of the time take a vector/s as an argument to generate an output. Roughly we can say there are eight functions associated with vectors and are listed below:
rep() function
seq() function
is.vector() function
as.vector() function
any() function
all() function
lapply() function
sapply() function
Let us discuss these vector functions one by one in detail.
The rep() function is specifically present there in R programming that allows you to repeat a set of objects or a vector to a given number of times. It has a syntax as shown below:
Syntax for rep()
Where,
x - is a vector or set of objects that we want to repeat a certain number of times.
… - is an argument that specifies how many times the vector should be repeated. It has different names as below-
each - specifies the number of times each element of a vector should be repeated.
times - it represents the number of times given vector gets repeated.
length.out - specifies the length of the resultant vector. The vector will repeat until the repetition reaches the given length.
We can specify any of these arguments under the function or simply put the number of times we want the repetition to happen directly. Examples for the rep() function are as shown in the image below:
Example for rep() function
The seq() function, on the other hand, generates a sequence of any data type in R from a starting point up to the ending point with some step value. It is a function that specifically falls under the vector creation category. Below is the syntax for the seq() function in R.
Syntax for seq() function
Where,
from - specifies the starting point of the sequence.
to - specifies the ending point for the sequence.
by - represents the step value which specifies the increment of the given sequence.
Let us see an example for a seq() function through the screenshot given below:
Example of seq() function
Here, in this example, we have created a sequence named my_seq (basically a vector) from 1 to 5 with a step 0.1.
The is.vector() function allows you to check if the object provided as an argument to it is a vector or not. This function takes an argument as an input and returns TRUE if the provided object is a vector. If the provided object is not a vector, this function returns FALSE.
The example for the is.vector() function is as shown below:
Example code and output for is.vector() function
In the example above, firstly, we have created a new object “p” which is a matrix. Well, if you want to explore more about the matrices, you should check the article Data Structures in R: Part 1 out. When we are using the is.vector() to check if “p” is a vector or not, the system is generating an output as FALSE. Clearly, because it is a matrix and not a vector.
Also, in the second example, we are trying to check if the sequence named my_seq, that we have created in the previous example (example of seq() function) is a vector or not. Since it is a vector, the system generates TRUE as an output.
The as.function() in R, allows you to convert a non-vector object into a vector. For example, if you have a matrix as an object and you wanted to convert it into a vector, then you can use the as.vector() function to get this done.
See an example below for a better understanding of as.vector() function.
Remember the matrix “p” that we have defined in our previous example? We re going to convert it into a vector using as.vector() function.
Example and output for as.vector() function
In this example, we can see that the class of “p” is a matrix. Meaning, “p” is a matrix object. However, when we use as.vector() function on the same matrix, it turns out to be a vector (see Mat_as_vect variable). That’s how the function work.
Suppose you come up with a situation, where you want to check if your vector contains elements that specify a certain criteria or not, you can check that using any() function under R. This function takes a vector and a logical condition applied on the vector as an argument and returns a boolean TRUE if any of the element from vector satisfies the criteria, else it returns FALSE.
The screenshot below explains the working of any() function with an example.
Example code for any() function
When we want to check if all of the elements from the given vector are following certain conditions or not, we are going to use all() function under R. The all() function under R takes a logical condition as an input and checks if all the elements from given vector are satisfying the given condition or not. If all the elements are following the logical condition, it will return as TRUE else it returns as FALSE.
See an example below for a better realization of all() function:
Example for all() function
Since every element of the vector x is greater than zero, the condition holds here and hence the final output is TRUE.
There comes a time when you have a vector of elements and you wanted to apply a certain function on each element of the given vector. We have the lapply() function under R, which does this task for you precisely. The lapply() function takes a vector as an argument and then applies a specific function on each element of that vector. Finally, this function returns a list as an output once the function is applied to each element.
See the example below:
Example for lapply() function
Point that we should note here is, even if you use lapply() on a vector, the final output will be a list (as you can see in the output of example above).
The lapply() function was specifically designed for working on with lists. However, it allows us to work on with other data structures as well such as vectors, and data frames. The “l” under the function name stands for the “lists”.
In lapply(), we were restricted to the final output (it was always a list for whatever input data type). To improve this function, the sapply() function came in the picture. The sapply() function under R is smart enough to make a judgemental call on what should be the data structure for the final output look. If it feels that the final output will also look good in vector format, it will convert it into a vector rather than generating it as a list.
Apart from this, there is no change in the function itself.
The function takes a vector, list, matrix, or data frame as an argument, applies a specific function over each element, and finally generates an output in a suitable format (vector, list, or any other) rather than lists only.
Example for the sapply() function under R is as shown below:
Example of sapply() function
In the example above, you can see that rather than generating the output as a list after applying tolower() function (this function converts entire text into lower cases), the system has generated it as a vector.
Vectors are the most important parts of the R Programming and so do the functions associated with them are. Throughout this article, we have discussed various functions associated with vectors in R. These functions either help us in creating a vector or manipulating those. In our next article, we will again come up with something new and interesting from the world of R Programming. Until then, read this one out, practice hard, and keep safe! 😊
5 Factors Influencing Consumer Behavior
READ MOREElasticity of Demand and its Types
READ MOREAn Overview of Descriptive Analysis
READ MOREWhat is PESTLE Analysis? Everything you need to know about it
READ MOREWhat is Managerial Economics? Definition, Types, Nature, Principles, and Scope
READ MORE5 Factors Affecting the Price Elasticity of Demand (PED)
READ MORE6 Major Branches of Artificial Intelligence (AI)
READ MOREScope of Managerial Economics
READ MOREDijkstra’s Algorithm: The Shortest Path Algorithm
READ MOREDifferent Types of Research Methods
READ MORE
Latest Comments