Introduction to R Vectors
Vectors are basic R data type objects. They are divided into six atomic vectors.
Single Element Vectors
The data created with one value is of the vector of length 1.
Multiple Element Vector
We can also create a vector with multiple values using the colon with numerical data.
v <- 5:10
output: 5 6 7 8 9 10
The sequence of decimal values
v <- 6.6:9.6
output: 6.6 7.6 8.6 9.6
In the below example,11.4 will not be in the sequence and it will be discarded as it doesn’t belong in the sequence.
v <- 3.8:8.4
output: 3.8 4.8 5.8 6.8 7.8
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. There different types of functions in R programming.
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.
4.5 (2,181 ratings)
rep(c(1, 2, 3), times = 4)
 1 2 3 1 2 3 1 2 3 1 2 3
There are different ways of implementing the rep function.
We can also input the number of times a particular value needs to be repeated.
rep(c(1, 2), times = c(4,3))
Output: 1 1 1 1 2 2 2
The third way of implementing a rep function is by specifying the length.
Output: 1 2 1 2 1 2 1 2 1
2. 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.
seq(from = 3.5, to = 1, by = -0.3)
Output: 3.5 3.2 2.9 2.6 2.3 2.0 1.7 1.4 1.1
Additionally, We can also add another attribute to the input of the function (i.e length)
seq(from = 3.5, to = 1, length.out = 6)
 3.5 3.0 2.5 2.0 1.5 1.0
3. R any() function
Any() function takes input values and returns logical vectors which either True or False.
Let’s assume we have assigned a set of values to x.
x = 1,2,3,4,5
x <- 1:5
Now, we use any() function to see if there is any value above 5.
any(x > 5)
FALSE From a similar example, We can also output TRUE by changing the value from 5 to 3.
any(x > 3)
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.
1. When all values meet the condition
x <- 1:5all(x > 0)
2.When values don’t meet the condition
x <- 1:5all(x > 3)
In the above example, only two values are above 3. The functions in R is defined by Rf_ or R_
This is a guide to R Vectors. Here we discuss the different types of Vector functions in R programming with Syntax and Examples. You may also have a look at the following articles to learn more –