Loading, please wait...

A to Z Full Forms and Acronyms

Explain Vector Operations and function in the R programming language | R Tutorial

Jan 26, 2022 #RLanguage #Programming, 2190 Views
In this article, we will discuss the vector operations and functions of the R programming language.

Explain Vector Operations and function in the R programming language | R Tutorial

In this article, we will discuss the vector operations and functions of the R programming language.

Vector Operations

There are various vector operations in the R programming language:

  • Arithmetic operations: The user can perform all the arithmetic operations such as addition, subtraction, multiplication, and division in vectors in the R programming language. The condition we have is that the length of the vector should be the same. 
  • Element Recycling: The two different vectors of different length, if used in any of the above arithmetic operations then the shorter size vector get recycled to complete the operations. 
  • Sorting: The R package has the sort() function to sort the elements. By default, if you use the sort function to sort the elements of the vectors, it will sort in ascending order. To sort the elements in descending order, function revsort() along with a mention that decreasing = TRUE. 
  • Modify: In the R programming language, it allows you to modify the vector elements. Using the indexing techniques with the help of the assignment operators, we can modify the element. The user can also truncate the element of the vector through the reassignment operator. 
  • Deletion: To free the storage and delete the elements of the vector, assign the NULL to the vector to delete the particular element.

Vector Function

The function is used to perform a specific task. Functions are considered as other types of objects. 

1. R rep() function

rep() function is used to repeat the value given in the rep function as input. 

Syntax:

rep()

Example:

rep(c(1, 2, 3, 4, 5), times=2)

In this, we repeat the (1, 2, 3, 4, 5) two times. 

OUTPUT:

[1] 1 2 3 4 5 1 2 3 4 5

 

2. R seq() function

seq() function is used to create a set of sequential values. 

Syntax:

seq()

Example:

seq(from = 5.0, to = 2.0, by= -0.5)

In this, it is generating the set of sequential values from 5.0 to 2.0 by decreasing it to 0.3. 

OUTPUT:

[1] 5.0 4.5 4.0 3.5 3.0 2.5

 

3. R any() function

It takes input values and returns the logical values such as TRUE and FALSE.

Syntax:

any()

Example:

y <- 1:4

any(y>5)

It will check whether any element in any particular range is greater than 5. 

OUTPUT:

FALSE 

 

4. R all() function

all() function works similar to any() function. The only difference between both the functions is it checks each value individually and if any value is unable to meet the condition. It returns FALSE. 

Syntax:

all()

 

A to Z Full Forms and Acronyms