Are you finding difficulty working with R vectors? Don’t worry, look at some vector functions in R that will make your life easier with R programming.
Functions are pieces of code that are created to perform specific tasks. We make functions to avoid repetition of code and errors. They also help us in making the code organized and readable.
In this R tutorial, we will take a look at a few commonly used vector functions in R. We will see how they are used and what they do. We will also look at some examples of their usage. So, let’s get going!
What are R vector functions?
There are functions in R that either help with creating a vector or take vectors as input arguments. We call such functions as vector functions in R programming.
Today, we are going to study a few vector functions in R, that you will find very useful in R programming. These functions are:
- rep()
- seq()
- is.vector()
- as.vector()
- any()
- all()
- lapply()
- sapply()
These functions either take a vector as input or return a vector as output.
1. The rep() Function
The rep()
function repeats a vector, or value, a given number of times. It then returns a vector with the repeated values. For example:
Wait! Do you know what is R vector? – vectors in R
Code:
repeated_vector <- rep(c(1,2,3),4) repeated_vector
Output:
We can also specify the length of the vector instead of the number of times using the length.out
argument. The function keeps the repetition going until the mentioned length is reached.
Code:
repeated_vector2 <- rep(c(4,5,6),length.out=15) repeated_vector2
Output:
We can also use the each
argument to repeat each element of the repeating vector. For example:
Code:
repeated_vector3 <- rep(c(4,5,6),each=3) repeated_vector3
Output:
2. The seq() Function
The seq()
function creates a sequence of numbers starting from the given from
argument and ending at the to
argument. We can also pass the by
argument to specify the increase/decrease step. By default, the increase step is 1.
Example 1:
sequence1 <- seq(from=2,to=15,by=0.5) sequence1
Output:
We can also use the length.out
argument to specify the length of the output vector. This way R keeps the sequence going until the desired length is reached.
Example 2:
sequence2 <- seq(from=5,length.out=12) sequence2
Example 3:
sequence3 <- seq(from=5,to=15,length.out=20) sequence3
Output:
3. The is.vector() Function
The is.vector()
function takes an object as an input and returns TRUE
if the input object is a vector. It returns FALSE
if the object is not a vector.
Code:
is.vector(sequence3)
Output:
4. The as.vector() Function
The as.vector()
function takes an object as an argument and converts it into a vector. Let’s take a look at the workings of this function with an example.
First, let’s create a matrix. We will name this matrix mat_to_vec
, as we will be converting it into a vector soon.
Code:
mat_to_vec <- matrix(c(1:9),c(3,3)) mat_to_vec
Output:
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
By using the class()
function we can confirm that mat_to_vec
is in fact a matrix.
Code:
class(mat_to_vec)
Output:
Now, let’s use the as.vector()
function on our matrix.
Code:
mat_to_vec <- as.vector(mat_to_vec) mat_to_vec
Output:
As you can see, mat_to_vec
is now arranged in a single row instead of 3×3 rows and columns. Instead of an integer matrix, it should now be an integer vector and should have a class of integer.
Code:
class(mat_to_vec)
Output:
5. The any() Function
The any()
function takes a vector and a logical condition as input arguments. It checks the vector against the condition and creates a logical vector. It then returnTRUE
, if any one of the elements in the logical vector is TRUE
.
Code:
vec <- as.integer(c(34,23,53,42,16,42,64,32,76)) any(vec,vec>50)
Output:
6. The all() Function
The all()
function takes a vector and a logical condition as input arguments. It checks the vector against the condition and creates a logical vector. It then returns TRUE
if all the elements in the logical vector are TRUE
, and FALSE
if all elements are not TRUE
.
Code:
all(vec,vec>50)
7. The lapply() Function
The lapply()
function takes a vector, list or a data frame and a function. The lapply()
function applies a function to all elements of a vector, list or data frames. The function then returns the result in the form of a list. For example:
Code:
names <- c("JOHN","RICK","RAHUL","ABDUL") lapply(names,tolower)
Output:
8. The sapply() Function
The sapply()
is very similar to the lappy()
function. The only difference is that the sapply()
function returns the result in the form of a vector. For example:
Code:
sapply(names,tolower)
Output:
Summary
Vector functions are functions that perform operations on vectors or give output as vectors. In this article, we studied some important vector functions in R. We looked at their uses and also saw examples of their usage.
The above functions like is.vector()
, as.vector()
, lapply()
, sapply()
, any()
, and seq()
are very important and commonly used functions in the R programming language.
Still, finding difficulty working with vector functions in R?
Feel free to ask any queries related to R tutorial and our experts at TechVidvan will be happy to help you.