简体   繁体   English

在 R 中使用 != 比较一个 Dataframe 中的列

[英]Using != in R to compare Columns in one Dataframe

I'm trying to find unequal values in 2 different columns from the same dataframe.我试图在来自同一数据帧的 2 个不同列中找到不相等的值。

I'm using我正在使用

df %>%
  filter(column1 != column2)

However it is returning some values that seem to be equal.然而,它正在返回一些似乎相等的值。 However it does not return the same set of values as its counterpart但是它不会返回与其对应的相同的值集

df %>%
  filter(column1 == column2)

Both columns are the double data type.两列都是双数据类型。

Am I supposed to find out a different method to compare them?我应该找出不同的方法来比较它们吗?

Example of equal values showing up in != query != 查询中显示相等值的示例

The == and != are elementwise comparison ie it compares the 1st value of column1 against the 1st of column2, 2nd against 2nd and so on. ==!=是元素比较,即将 column1 的第一个值与 column2 的第一个值进行比较,将第二个值与第二个值进行比较,依此类推。 If the intention is to returns match from any values in 'column2' with that of 'column1', use %in%如果目的是将“column2”中的任何值与“column1”中的任何值返回匹配,请使用%in%

library(dplyr)
df %>%
    filter(column1 %in% column2)

The reverse will be to negate ( ! )相反将否定( !

df %>%
   filter(!column1 %in% column2)

Regarding the OP's description However it is returning some values that seem to be equal.关于 OP 的描述However it is returning some values that seem to be equal. and Both columns are the double data type.Both columns are the double data type. . . It is a tricky situation with the columns are double ie it would also have to consider precision to make them equal if it is elementwise.列是双重的,这是一个棘手的情况,即如果是元素,它还必须考虑精度以使它们相等。 As there is no reproducible example, it is only based on assumption.由于没有可重复的例子,它仅基于假设。 One option is to round the column values and do the elementwise comparison一种选择是round列值并进行元素比较

df %>% 
    filter(round(column1, 1) == round(column2, 1))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM