Open In App

R – Tuples

Last Updated : 09 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

R tuple is basically an entity containing items belonging to different data types. The integral values can be clubbed with the Boolean or the string variables under one parameter in R programming language it can be implemented using the list method. Primarily, the list method allows the creation and addition of various elements belonging to different types in R. 

Creation of a Tuple 

The tuple is created using the list method. The various parameters that are supplied to the list become the values at different indexes beginning with zero inside a tuple. The tuple can be printed by simply using the print statement in the following code snippet R list that is a tuple in R contains four elements, and each of them is printed when we apply the print operation.

R




# creating a tuple
t <- list(1,2,3, TRUE)
print("Tuple")
print(t)


Output:

Tuple in R Programming

 

Tuple Library

It can also be installed and loaded into the R workspace in order to deal with the creation and modification of the tuples and apply basic operations to them. It can be installed using the following command: 

install.packages("tuple")

There are various inbuilt methods available in the tuple package which are used to perform operations. Some of these methods are 

Duplicate Method

The Duplicate Method is found out the elements which are being repeated in the tuple. 

Syntax : duplicate(tuple)

In the following code snippet, we have applied the duplicate operation which prints the elements which are being repeated.

R




library("tuple")
  
# creating a tuple
t <- list(1,2,3, TRUE,1,3)
  
# finding duplicayes in tuple
dup <- duplicate(t)
print("Duplicate Values in Tuple")
print(dup)


Output:

Tuple in R Programming

 

In-operator

The in-operator is used to check for the occurrence of the elements in another tuple. For example, in the code below we are checking whether each of the elements is present in another tuple name to be vec or not, it returns a true value of the element encountered otherwise it returns a false therefore it returns a Boolean vector with the length equivalent to the tuple which is being searched for. 

R




# installing the required libraries
library("tuple")
  
# creating a tuple
t <- list(1,2,3, TRUE,1,3)
  
# creating another tuple 
vec <- list(1,3,5,4,2,TRUE,FALSE,6)
   
# finding duplicayes in tuple
print("Finding values of one tuple\
which are present in another tuple")
print(vec %in% t)


Output:

Tuple in R Programming

 

Aggregate Methods

The aggregate methods are also available in the tuple package which can be applied over the tuple in order to calculate the various mathematical values that are required in the operations. For example, the sum of Min and Max can the tuple can be computed using these methods. The sum operation is used in case all the values in the tuple are integral in nature. 

Syntax : 

  • For sum – sum(tuple)
  • For min – min(tuple) 
  • For max – max(tuple)

R




# installing the required libraries
library("tuple")
  
# creating a tuple
tup <- c(4,2,6,7,3,1)
  
# using aggreagate methods over tuples
print("Minimum value of tuple")
print(min(tup))
  
print("Maximum value of tuple")
print(max(tup))
  
print("Sum of values of tuple")
print(sum(tup))


Output:

Tuple in R Programming

 

Repeating Values of Tuple up till a Specific Time 

The tuple package in R also has the rep method which is used to repeat the data values until the specified number is met. 

Syntax : rep(tuple,n))

Arguments: tuple-the tuple in which the values have been to be repeated and the number of times the values have to be repeated.

R




# installing the required libraries
library("tuple")
  
# creating a tuple
tup <- c(4,2)
  
# using aggreagate methods over tuples
print("Repeating values of tuple")
print(rep(tup,3))


Output:

Tuple in R Programming

 



Like Article
Suggest improvement
Share your thoughts in the comments