Vector Functions in R – Master R seq, sapply, rep functions
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!
Keeping you updated with latest technology trends, Join TechVidvan on Telegram
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:
These functions either take a vector as input or return a vector as output.
1. The rep() Function
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
repeated_vector <- rep(c(1,2,3),4) repeated_vector
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.
repeated_vector2 <- rep(c(4,5,6),length.out=15) repeated_vector2
We can also use the
each argument to repeat each element of the repeating vector. For example:
repeated_vector3 <- rep(c(4,5,6),each=3) repeated_vector3
2. The seq() Function
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.
sequence1 <- seq(from=2,to=15,by=0.5) sequence1
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.
sequence2 <- seq(from=5,length.out=12) sequence2
sequence3 <- seq(from=5,to=15,length.out=20) sequence3
3. The is.vector() Function
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.
4. The as.vector() Function
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.
mat_to_vec <- matrix(c(1:9),c(3,3)) mat_to_vec
[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.
Now, let’s use the
as.vector() function on our matrix.
mat_to_vec <- as.vector(mat_to_vec) mat_to_vec
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.
5. The any() Function
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 return
TRUE, if any one of the elements in the logical vector is
vec <- as.integer(c(34,23,53,42,16,42,64,32,76)) any(vec,vec>50)
6. The all() Function
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
FALSE if all elements are not
7. The lapply() Function
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:
names <- c("JOHN","RICK","RAHUL","ABDUL") lapply(names,tolower)
8. The sapply() Function
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:
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
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.