检查一个数据帧列中的值是否存在于第二个数据帧中 [英] Check whether values in one data frame column exist in a second data frame
本文介绍了检查一个数据帧列中的值是否存在于第二个数据帧中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个数据框(A和B),均带有 C列。我想检查数据帧B中的列'C'中的值是否存在于数据帧B中。
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))
推荐答案
按如下方式使用%in%
A$C %in% B$C
哪个会告诉您A的C列的哪些值在B中。
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
您可以将其用作<$ c $的行的索引c> A 或作为 A $ C
的索引以获取实际值:
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
我们也可以取反:
A$C[!A$C %in% B$C]
[1] 2 # returns all values of A$C that are NOT in B$C
如果您想知道特定值是否在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
这篇关于检查一个数据帧列中的值是否存在于第二个数据帧中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文