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 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:
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:
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:
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:
Similar Reads
Rust - Tuple A tuple in rust is a finite heterogeneous compound data type, meaning it can store more than one value at once. In tuples there is no inbuilt method to add elements into a tuple. We can use the index to get the value of a tuple, and we also can not iterate over a tuple using for loop. Tuples in Rust
2 min read
Create a List of Tuples in Python The task of creating a list of tuples in Python involves combining or transforming multiple data elements into a sequence of tuples within a list. Tuples are immutable, making them useful when storing fixed pairs or groups of values, while lists offer flexibility for dynamic collections. For example
3 min read
Create Sequence of Repeated Values in R A repeated sequence of numbers is an incremental list of numbers arranged in such a way that the difference in the successive numbers is 1, and each of these numbers is repeated for the specified number of times. R provides us with a variety of methods to generate repeated sequences both using vecto
4 min read
How to iterate through list of tuples in Python In Python, a list of tuples is a common data structure used to store paired or grouped data. Iterating through this type of list involves accessing each tuple one by one and sometimes the elements within the tuple. Python provides several efficient and versatile ways to do this. Letâs explore these
2 min read
List of Vectors in R Vectors are a sequence of elements belonging to the same data type. A list in R, however, comprises of elements, vectors, variables or lists which may belong to different data types. In this article, we will study how to create a list consisting of vectors as elements and how to access, append and d
6 min read
Operations on Lists in R Programming Lists in R language, are the objects which comprise elements of diverse types like numbers, strings, logical values, vectors, list within a list and also matrix and function as its element. A list is generated using list() function. It is basically a generic vector that contains different objects. R
4 min read