The Wayback Machine - https://web.archive.org/web/20240930154508/https://www.geeksforgeeks.org/get-exclusive-elements-between-two-objects-in-r-programming-setdiff-function/
Open In App

Get Exclusive Elements between Two Objects in R Programming – setdiff() Function

Last Updated : 27 Oct, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

setdiff() function in R Programming Language is used to find the elements which are in the first Object but not in the second Object.

Syntax: setdiff(x, y)

Parameters: 

  • x and y: Objects with sequence of items

R – setdiff() Function Example

Example 1: Apply setdiff to Numeric Vectors in R Language

R




# R program to illustrate
# the use of setdiff() function
   
# Vector 1
x1 <- c(1, 2, 3, 4, 5, 6, 5, 5)   
   
# Vector 2 
x2 <- c(2:4)    
   
# Calling setdiff() Function
x3 <- setdiff(x1, x2)      
   
print(x3)                


Output: 

[1] 1 5 6

Example 2: Apply setdiff to Character Vectors in R Language

R




# R program to illustrate
# the use of setdiff() function
   
# Vector 1
x <- c("GFG", "GEEKS")   
   
# Vector 2 
y <- c("GFG", "Welcome", "HOME")    
   
# Calling setdiff() Function
x3 <- setdiff(x, y)      
   
print(x3)                


Output:

[1] "GEEKS"

Example 3: setdiff Between R Dataframes

R




# R program to illustrate 
# the use of setdiff() function
   
# Data frame 1
data_x <- data.frame(x1 = c(5, 6, 7),    
                     x2 = c(2, 2, 2))
   
# Data frame 2
data_y <- data.frame(y1 = c(2, 3, 4),       
                     y2 = c(2, 2, 2))
   
# Calling setdiff() Function
data_z <- setdiff(data_x, data_y)  
   
print(data_z)


Output: 

  x1
1  5
2  6
3  7


Similar Reads

Check for Presence of Common Elements between Objects in R Programming - is.element() Function
is.element() function in R Language is used to check if elements of first Objects are present in second Object or not. It returns TRUE for each equal value. Syntax: is.element(x, y) Parameters: x and y: Objects with sequence of items Example 1: # R program to illustrate # the use of is.element() function # Vector 1 x1 <- c(1, 2, 3) # Vector 2 x2
1 min read
Check if Two Objects are Equal in R Programming - setequal() Function
setequal() function in R Language is used to check if two objects are equal. This function takes two objects like Vectors, dataframes, etc. as arguments and results in TRUE or FALSE, if the Objects are equal or not. Syntax: setequal(x, y) Parameters: x and y: Objects with sequence of items Example 1: # R program to illustrate # the use of setequal(
1 min read
Union of two Objects in R Programming - union() Function
union() function in R Language is used to combine the data of two objects. This function takes two objects like Vectors, dataframes, etc. as arguments and results in a third object with the combination of the data of both the objects. Syntax: union(x, y) Parameters: x and y: Objects with sequence of items Example 1: Union of two Vectors # R program
2 min read
Compare two Objects for Equality in R Programming - identical() Function
identical() function in R Language is used to return TRUE when two objects are equal else return FALSE. Syntax: identical(a, b)Parameters: a, b: specified two objects Example 1: C/C++ Code # R program to illustrate # identical function # Calling the identical() function identical(factorial(3), gamma(4)) identical(lfactorial(5), log(factorial(5))) i
2 min read
Intersection of Two Objects in R Programming - intersect() Function
In this article, we will discuss how we do the Intersection of Two Objects in R Programming Language using the intersect() function. What is the intersect() Function?intersect() function in R Programming Language is used to find the intersection of two Objects. This function takes two objects like Vectors, Dataframes, etc. as arguments and results
2 min read
How to Prevent ifelse() from Turning Date Objects into Numeric Objects in R?
The ifelse() function in R is widely used for vectorized conditional operations. However, a common issue arises when you apply ifelse() to Date objects: it converts them into numeric objects, representing the number of days since January 1, 1970. This behavior can be problematic when you want to maintain the date format. Here's how you can prevent
2 min read
Concatenating Objects in R Programming - combine() Function
In R programming, coercion function c() and combine() function are similar to each other but are different in a way. combine() functions acts like c() and unlist() functions but uses consistent dplyr coercion rules. Moreover, combine() function is used to combine factors in R programming. In this article, we'll see the implementation of combine() a
2 min read
List all the Objects present in the Current Working Directory in R Programming - ls() Function
ls() function in R Language is used to list the names of all the objects that are present in the working directory. Syntax: ls() Parameters: This function needs no argument Example 1: # R program to list all the object names # Creating a vector vec <- c(1, 2, 3) # Creating a matrix mat <- matrix(c(1:4), 2) # Creating an array arr <- array(
1 min read
Remove Objects from Memory in R Programming - rm() Function
rm() function in R Language is used to delete objects from the memory. It can be used with ls() function to delete all objects. remove() function is also similar to rm() function. Syntax: rm(x) Parameters: x: Object name Example 1: # R Program to remove # objects from Memory # Creating a vector vec <- c(1, 2, 3, 4) vec # Creating a list list1 =
2 min read
Get or Set names of Elements of an Object in R Programming - names() Function
names() function in R Language is used to get or set the name of an Object. This function takes object i.e. vector, matrix or data frame as argument along with the value that is to be assigned as name to the object. The length of the value vector passed must be exactly equal to the length of the object to be named. Syntax: names(x) <- value Para
2 min read
Get the Sign of Elements of a Numeric Vector in R Programming - sign() Function
sign() function in R Language is used to find the sign of the elements of the numeric vector provided as argument. It does not work on complex vectors. Syntax: sign(x) Parameter: x represents numeric vector Returns: 1 for Positive number and -1 for Negative Number Example 1: # Create a variable x <- 1 y <- -100 # Check sign cat("Sign of
1 min read
Get the List of Arguments of a Function in R Programming - args() Function
args() function in R Language is used to get the required arguments by a function. It takes function name as arguments and returns the arguments that are required by that function. Syntax: args(name) Parameters: name: Function name Returns: For a closure: Formal Argument list but with NULL body For a Primitive Function: A closure with usage and a N
1 min read
Looping over Objects in R Programming
One of the biggest issues with the “for” loop is its memory consumption and its slowness in executing a repetitive task. When it comes to dealing with a large data set and iterating over it, a for loop is not advised.  In this article we will discuss How to loop over a list in R Programming Language provides many alternatives to be applied to vecto
5 min read
Compute the Covariance between Two Vectors in R Programming - cov() Function
cov() function in R Language is used to measure the covariance between two vectors. Syntax: cov(x, y, method) Parameters: x, y: Data vectors method: Type of method to be used Example 1: # Data vectors x <- c(1, 3, 5, 10) y <- c(2, 4, 6, 20) # Print covariance using Pearson method print(cov(x, y, method = "pearson")) Output: [1] 30.6
1 min read
Compute the Correlation Coefficient Value between Two Vectors in R Programming - cor() Function
cor() function in R Language is used to measure the correlation coefficient value between two vectors. Syntax: cor(x, y, method) Parameters: x, y: Data vectors method: Type of method to be used Example 1: # Data vectors x <- c(1, 3, 5, 10) y <- c(2, 4, 6, 20) # Print covariance using Pearson method print(cor(x, y, method = "pearson"
1 min read
Add Color Between Two Points of Kernel Density Plot in R Programming - Using with() Function
plot() and with() functions are used to draw and add color between a particular area of two points of Kernel Density Plot in R Language. Draw a Kernel Density plot plot() function in R Language is used to draw a Kerner Density Plot Syntax: plot(dens_x) Parameters: dens_x: Default plot of density. Returns: Kernel density plot # Create example data s
1 min read
Find the Difference Between Two Dates in R Programming - julian() Function
In R programming, difference between two dates can be determined by using basic function julian() and passing date object as the parameter in the function. It ignores the leap-seconds. Syntax: julian(x, origin) Parameters: x: represents date object origin: represents another date object from where difference has to be computed Example 1: # Define d
1 min read
Apply a Function over a List of elements in R Programming - lapply() Function
lapply() function in R Programming Language is used to apply a function over a list of elements. lapply() function is used with a list and performs the following operations: lapply(List, length): Returns the length of objects present in the list, List.lapply(List, sum): Returns the sum of elements held by objects in the list, List.lapply(List, mean
2 min read
Calculate the difference between Consecutive pair of Elements of a Vector in R Programming - diff() Function
diff() function in R Language is used to find the difference between each consecutive pair of elements of a vector. Syntax: diff(x, lag, differences) Parameters: x: vector or matrix lag: period between elements differences: Order of difference Example 1: # R program to find the difference # between each pair of elements of a vector # Creating a vec
2 min read
What is the Relation Between R Programming and Hadoop Programming?
The relation between R programming and Hadoop revolves around integrating R with the Hadoop ecosystem to analyze large datasets that are stored in a Hadoop environment. R, primarily used for statistical analysis and data visualization, is not inherently built for handling big data. However, when combined with Hadoop, it can leverage Hadoop's distri
3 min read
Get or Set Levels of a Factor in R Programming - levels() Function
levels() function in R Language is used to get or set the levels of a factor. Syntax: levels(x) Parameters: x: Factor Object Example 1: # R program to get levels of a factor # Creating a factor gender <- factor(c("female", "male", "male", "female")); gender # Calling levels() function # to get the levels l
1 min read
Get Indices of Specified Values of an Array in R Programming - arrayInd() Function
arrayInd() function in R Language is used to get the indices of the values passed to the function as argument. This function takes values and the array in which the values are to be searched and returns the indices for each match found. Syntax: arrayInd(values, dim(x)) Parameters: values: value or vector of values to be searched dim(x): array to be
2 min read
Get the number of columns of an Object in R Programming - ncol() Function
ncol() function in R Language is used to return the number of columns of the specified matrix. Syntax: ncol(x)Parameters: x: matrix, vector, array or data frame Example 1: C/C++ Code # R program to illustrate # ncol function # Getting R Biochemical Oxygen Demand Dataset BOD # Calling ncol() function to # get the number of columns ncol(BOD) Output:
1 min read
Get the number of rows of an Object in R Programming - nrow() Function
nrow() function in R Language is used to return the number of rows of the specified matrix. Syntax: nrow(x)Parameters: x: matrix, vector, array or data frame Example 1: C/C++ Code # R program to illustrate # nrow function # Getting R Biochemical Oxygen Demand Dataset BOD # Calling nrow() function to # get the number of rows nrow(BOD) Output: Time d
1 min read
Get Transpose of a Matrix or Data Frame in R Programming - t() Function
t() function in R Language is used to calculate transpose of a matrix or Data Frame. Syntax: t(x) Parameters: x: matrix or data frame Example 1: # R program to illustrate # t function # Getting R Biochemical Oxygen Demand Dataset BOD # Calling t() function t(BOD) Output: Time demand 1 1 8.3 2 2 10.3 3 3 19.0 4 4 16.0 5 5 15.6 6 7 19.8 [, 1] [, 2] [
1 min read
Get the Maximum element of an Object in R Programming - max() Function
max() function in R Language is used to find the maximum element present in an object. This object can be a Vector, a list, a matrix, a data frame, etc.. Syntax: max(object, na.rm) Parameters: object: Vector, matrix, list, data frame, etc. na.rm: Boolean value to remove NA element. Example 1: # R program to illustrate # the use of max() function #
1 min read
Get the Minimum element of an Object in R Programming - min() Function
min() function in R Language is used to find the minimum element present in an object. This object can be a Vector, a list, a matrix, a data frame, etc.. Syntax: min(object, na.rm) Parameters: object: Vector, matrix, list, data frame, etc. na.rm: Boolean value to remove NA element. Example 1: # R program to illustrate # the use of min() function #
1 min read
Search and Return an Object with the specified name in R Programming - get() Function
get() function in R Language is used to return an object with the name specified as argument to the function. This is another method of printing values of the objects just like print function. This function can also be used to copy one object to another. Syntax: get(object) Parameters: object: Vector, array, list, etc. Example 1: # R program to ill
1 min read
Get the Minimum and Maximum element of a Vector in R Programming - range() Function
range() function in R Programming Language is used to get the minimum and maximum values of the vector passed to it as an argument. Syntax: range(x, na.rm, finite) Parameters: x: Numeric Vectorna.rm: Boolean value to remove NAfinite: Boolean value to exclude non-finite elementsR - range() Function ExamplesExample 1: Get the Minimum and Maximum elem
1 min read
Get a List of all the Attached Packages in R Programming - search() Function
search() function in R Language is used to get the list of all the attached packages in the R search path. Syntax: search() Parameters: This function takes no parameters. Example 1: # R program to list the packages # attached to R search path # Calling search() function search() Output: [1] ".GlobalEnv" "package:stats" "package:graphics" [4] "packa
1 min read