intersect

intersect returns a new SparkR DataFrame containing only rows that are in both DataFrames. This is equivalent to the INTERSECT query in SQL.

Syntax:

  • intersect(df1, df2)

Parameters:

  • df1: Any SparkR DataFrame
  • df2: Any SparkR DataFrame

Output:

  • SparkR DataFrame
newHires <- data.frame(name = c("Thomas", "George", "George", "John"),
                       surname = c("Smith", "Williams", "Brown", "Taylor"))
salesTeam <- data.frame(name = c("Lucas", "Bill", "George"),
                        surname = c("Martin", "Clark", "Williams"))

# Create SparkR DataFrame
newHiresDF <- createDataFrame(sqlContext, newHires)
salesTeamDF <- createDataFrame(sqlContext, salesTeam)

head(newHiresDF)
head(salesTeamDF)
# Use intersect() to find rows that occur in both DataFrames
newSalesHire <- intersect(newHiresDF, salesTeamDF)
head(newSalesHire)
# Returns a SparkR DataFrame
str(newSalesHire)