如何从不匹配的数据框中选择行? [英] How I can select rows from a dataframe that do not match?
本文介绍了如何从不匹配的数据框中选择行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试识别数据框中不匹配的值,但不知道如何执行此操作.
I'm trying to identify the values in a data frame that do not match, but can't figure out how to do this.
# make data frame
a <- data.frame( x = c(1,2,3,4))
b <- data.frame( y = c(1,2,3,4,5,6))
# select only values from b that are not in 'a'
# attempt 1:
results1 <- b$y[ !a$x ]
# attempt 2:
results2 <- b[b$y != a$x,]
如果 a = c(1,2,3)
这有效,因为 a
是 b
的倍数.但是,我试图从数据框 y
中选择所有不在 x
中的值,并且不知道要使用什么函数.
If a = c(1,2,3)
this works, as a
is a multiple of b
. However, I'm trying to just select all the values from data frame y
, that are not in x
, and don't understand what function to use.
推荐答案
如果我理解正确,您需要对 %in%
运算符取反.这样的事情应该可以工作:
If I understand correctly, you need the negation of the %in%
operator. Something like this should work:
subset(b, !(y %in% a$x))
> subset(b, !(y %in% a$x))
y
5 5
6 6
这篇关于如何从不匹配的数据框中选择行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文