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))
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.
frame() function in R Language is used to return TRUE if the specified data type is a data frame else return FALSE.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With