Vector Functions in R


April 6, 2022, Learn eTutorial
1790

In this tutorial, we are going to learn about the available built-in functions in R programming that supports operations with the vector data structure. The functions that allow users to simplify the task of working with the vector data structure in R are known as vector functions. Under this tutorial, in detail with examples will understand each vector function concept and its usage in R programs.

vector functions img

What are vector functions in R?

The vector functions are the functions that allow creating or manipulating the commonly used R data structure known as vector. Vector functions operate over defined vectors to perform some particular operation. There are different functions available and each function has a specific unique characteristic.

The list of vector functions in R programming languages are

  • R rep() function
  • R seq() function
  • R any() function
  • R all() function
  • is.vector() function
  • as.vector() function

1. R rep() function

In R programming language the rep() function is the function to replicate the vector elements as well as to replicate the elements in a list. It is a generic function with the following syntax.

Syntax


rep(x, ...)
rep.int(x, times)
rep(x,each)
rep_len(x, length.out)

Where arguments

Arguments Description
x   is a vector.
x followed by dots represents the further arguments to be passed to or from other methods such as times, length.out.  
times is an argument following x to indicate the number of times the vector should repeat.
length.out is an argument to get desired length vector output.  
each argument allows each element to repeat continuously for the number of times mentioned.  

Vector1 = c(5,45,19)
print(Vector1)

Consider the vector named Vector1 (x in the syntax) created using the c() function to get the output as shown below.

Output


[1]  5 45 19

We created a vector Vector1 and now let us apply the rep() function over the vector to repeat the elements in the vector. The elements of Vector1 are 5 45 19 respectively.


#syntax rep(x,times)

rep(Vector1,times =3)
rep.int(Vector1,times =3)

The times argument with an assignment of any non-negative integer results in that number of times to repeat the whole vector. Here the times is assigned with a value of 3 which indicates the whole vector must repeat 3 times. Let us see the output after the execution of the above codes.


> rep(Vector1,times =3)
[1]  5 45 19  5 45 19  5 45 19
> rep.int(Vector1,times =3)
[1]  5 45 19  5 45 19  5 45 19
>

From the output you can notice both the syntax rep(Vector1,times =3)rep.int(Vector1,times =3) produces same result.

rep fn screenshot img

The screenshot shows a different representation of rep() using times which produces the resulting same outputs.

rep o/p screenshot img

Next, see each argument used in the rep() function.


rep(Vector1,each=3)

Each element in the vector gets repeated according to the specified number. Here each is set to 3. So each element gets repeated 3 times as shown


> rep(Vector1,each=3)
[1]  5  5  5 45 45 45 19 19 19

Next is how length.out argument functions in rep().The repetition of the given vector occurs till it reaches the specified length inside the rep function.

Also, it is mentioned using two different syntaxes to obtain the same result like


#length argument

rep(Vector1,len=)
rep_len(Vector1, length.out=)

Using the syntax for rep() with given different length arguments the following outputs can be generated.


> Vector1 = c(5,45,19)
> print(Vector1)
[1]  5 45 19
> rep(Vector1,len=1)
[1] 5
> rep(Vector1,len=2)
[1]  5 45
> rep_len(Vector1, length.out=3)
[1]  5 45 19
> rep_len(Vector1, length.out=4)
[1]  5 45 19  5
> rep_len(Vector1, length.out=5)
[1]  5 45 19  5 45
> rep_len(Vector1, length.out=6)
[1]  5 45 19  5 45 19
> rep_len(Vector1, length.out=10)
[1]  5 45 19  5 45 19  5 45 19  5
>

The screenshot given enables you to visualize how these codes resemble in RStudio.

code screenshot img

Resultant output

output screenshot img

The default behavior of the rep() function can be concluded as

Syntax


rep(x, times = 1, length.out = NA, each = 1)

Example


> rep(Vector1, times = 1, length.out = NA, each = 1)
[1]  5 45 19
> rep(Vector1, times = 4, length.out = 5, each = 2)
[1]  5  5 45 45 19
>

2. R seq() function

The seq() function in R's purpose is to generate regular sequences. The seq() is particularly used with a vector data structure. The seq() function in R creates sequences of values in the R programming language.

Let us see the syntax to know more about the functioning of seq(………..).

Syntax


seq(………….)
seq(from = 1, to = 1, by = ((to - from)/(length.out - 1)),
    length.out = NULL, along.with = NULL, ...)

seq.int(from, to, by, length.out, along.with, ...)

seq_along(along.with)
seq_len(length.out)

Where

Arguments Description
from The starting value of the sequence
to The ending value of the sequence
by The interval of sequence
length.out The desired length of the sequence. A non-negative number, which for seq and seq.int will be rounded up if fractional.
along.with taking the length from the length of this argument.

Typical usage is


seq(from, to)
seq(from, to, by= )
seq(from, to, length.out= )
seq(along.with= )
seq(from)
seq(length.out= )

Example with syntax seq() syntax

The seq(x) is one of the simplest functions many additional arguments can be used inside seq() which we are going to discuss in this section.


#use seq() function in R

seq(10)       #Apply seq(x) creates a sequence from 1 to 10

The output after execution of the seq() function over number 10 generates a sequence of numbers starting from 1 and ending up at number 10 as shown below


[1]  1  2  3  4  5  6  7  8  9 10

Example with syntax seq (from, to)


seq(5,10)     #seq function from to

The seq syntax contains arguments from and to. From indicates the beginning of the vector and to indicates the ending of the vector to which the sequence gets generated. In our example, a starting point 5 (from) and a finishing point 10 (to) are given, and the seq() generates another sequence of numbers as below. The sequence has an interval of 1.


[1]  5  6  7  8  9 10

Example with syntax seq(from, to, by= )

The sequence interval can be changed by the addition of an argument by to seq(). In an earlier example, the interval was 1. (6-5=1,7-6=1……..10-9=1 like to-from). The by set the interval between the elements while its sequence generation. In our given example by = 0.5 so the sequence of numbers generates with 0.5 difference from each other. we need to specify the starting point(from), and ending point(to) with by argument inside the seq().


seq(5,10,0.5)  #seq function from,to,by=0.5

The resultant output is


[1]  5.0  5.5  6.0  6.5  7.0  7.5  8.0  8.5  9.0  9.5 10.0

Example with syntax seq(from,to,length.out=)

The seq() function with length.out argument specifies how long the argument should be you need to specify both from, to as well to generate the sequence.


seq(3,10,length.out=5)   #sequence of length 5

When this code runs generates a sequence of length 5 because length.out = 5. The intervals of this sequence is depending upon the number of elements needed to create.


[1]  3.00  4.75  6.50  8.25 10.00

Example with syntax seq(from,to,along.with = )

Produces the same result as the above syntax does generate only a difference in arguments.


seq(3,10,along.with=1:5)   #using along.with argument

The output is


[1]  3.00  4.75  6.50  8.25 10.00

The screenshot showing to summarize the different syntax to generate the sequence.

seq screenshot img

4. R any() function

The any() function finds at least one of the values is true for the given set of logical vectors. The any() function is enclosed with an input vector and a condition that needs to be satisfied. In any() case any of the elements in the vector satisfies the condition returns a boolean TRUE. On the other hand, returns a FALSE.

Syntax


any(..., na.rm = FALSE)

Where

Arguments Description
... zero or more logical vectors. Other objects of zero length are ignored, and the rest are coerced to logical ignore any class.    
na.rm logical. If true NA values are removed before the result is computed.    
arg descr table

In the example below, a vector x is created and a condition is provided (x<4) any function or any() checks whether any of the values of x(vector) satisfies the given condition if satisfies returns a boolean value TRUE or FALSE.


# use any() function

x = c(2,5,6,-9,-12,-16)
print(x)

print(any(x<4))

The output will be boolean TRUE because 2,-9,-12, and-16 are less than 4. The function check for only any of the values satisfies the condition.


[1]   2   5   6  -9 -12 -16
[1] TRUE

Let us see another example using NA values


# use any() function

x = c(2,5,6,NA,-9,-12,-16)
print(x)

print(any(x<4,na.rm=TRUE))

The value of na.rm if true means NA values are removed before the result is computed.

It produces the same result as above since the condition in our example remains unchanged. Based on our input vector and condition the output for the any() function varies.

5. R all() function

The all() function finds are all the values true for the given set of logical vectors. The function all() takes an input vector and a condition to satisfy as its arguments are enclosed with all braces (). If all the elements present in the vector satisfy the condition return a TRUE. On the other hand, if none of the elements or any of the elements or a single element does not satisfy the condition will return a boolean FALSE. All elements should satisfy the condition to return TRUE

Syntax


any(..., na.rm = FALSE)

Where

Arguments Description
……… zero or more logical vectors. Other objects of zero length are ignored, and the rest are coerced to logical ignoring any class.    
na.rm logical. If true NA values are removed before the result is computed.    
all fn img

Consider the example


x1=c(1,2,3,4,5)
print(all(x1<6))

The code after execution gives TRUE because all the values of vector x1 are less than 6, so the condition is satisfied for all the vectors.


[1] TRUE

The reverse condition is all x1>6 results in returning a boolean FALSE output as shown below.


print(all(x1>6))
[1] FALSE

6. is.vector() function

In R the built-in function is.vector () determines whether a vector is existing in an R program. The function returns either TRUE if there exists a vector or FALSE in the case of a non-existing vector.

The is.vector is discussed already under the vector tutorial. Have a look at is.vector() function.

7. as.vector() function

In R, as.vector() is a built in function to convert any other data structure such as a matrix, array, etc to a vector data structure. It is a generic function that attempts to coerce its argument into a vector mode.

Syntax


as.vector(x, mode = "any")

Where arguments

x is an R object
mode=” any” It is a character string giving an atomic mode or “list“, or (except for ‘vector’) “any”.

 

Consider the example with a matrix. A matrix is created which needs to be converted into a vector data structure.


MATRIX <- matrix(1:15, nrow=3, ncol=5)
print(MATRIX)

The matrix created is


[,1] [,2] [,3] [,4] [,5]
[1,]    1    4    7   10   13
[2,]    2    5    8   11   14
[3,]    3    6    9   12   15

Use as.vector to convert matrix MATRIX of 3×5 to a 1D vector as shown below


as.vector(MATRIX)

The output is


[1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15

The screenshot for matrix conversion to vector using as.vector() is given below.

as.vector fn screenshot