[英]Finding values in a columns “a” which has different values in column “b” for two different data set
Data contains multiple columns and 3000 row数据包含多列和 3000 行
Same OrderNo
but different Ordertype
.相同的
OrderNo
但不同Ordertype
。
I want to get all the OrderNo
whose Ordertype
are different in the two data frame.我想获取两个数据框中
Ordertype
不同的所有OrderNo
。
I have isolated the two columns from the two data frame and set them in ascending order.我已经从两个数据框中分离出两列,并按升序设置它们。 Then I tried to use the function
cbind
to combine the two columns and find the missing values in one of the columns.然后我尝试使用 function
cbind
将两列合并并在其中一列中查找缺失值。
xxx <- data.frame( orderNo = c(1:10), Ordertype = c("a", "b", "c", "d", "a", "b", "c", "d", "e", "f"))
yyy <- data.frame( orderNo = c(1:10), Ordertype = c("a", "b", "c", "d", "a", "b", "e", "d", "e", "f"))
In the above example: OrderNo
"7" corresponds to "c" in one data frame and "e" in another data frame.在上面的例子中:
OrderNo
“7”对应一个数据帧中的“c”和另一个数据帧中的“e”。 I want a set of all such number with a different value in the column Ordertype
as my output.我想要一组在
Ordertype
列中具有不同值的所有此类数字作为我的 output。
It sounds like you want a data frame that contains differences between two data frames, matched by (and including) orderNo
.听起来您想要一个包含两个数据帧之间差异的数据帧,由(并包括)
orderNo
。 Is that correct?那是对的吗?
One possibility is:一种可能性是:
res <- merge(xxx, yyy, by = "orderNo")
res[res[,2] != res[,3], ]
orderNo Ordertype.x Ordertype.y
7 7 c e
Using dplyr
and anti_join
you can do the following to find differences:使用
dplyr
和anti_join
您可以执行以下操作来查找差异:
library(dplyr)
inner_join(anti_join(xxx, yyy), anti_join(yyy, xxx), by='orderNo')
orderNo Ordertype.x Ordertype.y
1 7 c e
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.