Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether values in one data frame column exist in a second data frame

Tags:

dataframe

r

I have two data frames (A and B), both with a column 'C'. I want to check if values in column 'C' in data frame A exists in data frame B.

A = data.frame(C = c(1,2,3,4)) B = data.frame(C = c(1,3,4,7)) 
like image 533
user1631306 Avatar asked Dec 08 '12 05:12

user1631306


People also ask

How do you check if a value exists in a Dataframe R?

To check if the element belongs to a vector or dataframe in R, use the %in% operator. %in% in R can be used in the data frame in the following circumstances. To create a new variable of a column using the %in% operator.

How do you check if a variable is a data frame or not?

frame() function in R Language is used to return TRUE if the specified data type is a data frame else return FALSE.


1 Answers

Use %in% as follows

A$C %in% B$C 

Which will tell you which values of column C of A are in B.

What is returned is a logical vector. In the specific case of your example, you get:

A$C %in% B$C # [1]  TRUE FALSE  TRUE  TRUE 

Which you can use as an index to the rows of A or as an index to A$C to get the actual values:

# as a row index A[A$C %in% B$C,  ]  # note the comma to indicate we are indexing rows  # as an index to A$C A$C[A$C %in% B$C] [1] 1 3 4  # returns all values of A$C that are in B$C 

We can negate it too:

A$C[!A$C %in% B$C] [1] 2   # returns all values of A$C that are NOT in B$C 



If you want to know if a specific value is in B$C, use the same function:
  2 %in% B$C   # "is the value 2 in B$C ?"     # FALSE    A$C[2] %in% B$C  # "is the 2nd element of A$C in B$C ?"     # FALSE 
like image 155
Ricardo Saporta Avatar answered Sep 24 '22 15:09

Ricardo Saporta