简体   繁体   中英

Using != in R to compare Columns in one Dataframe

I'm trying to find unequal values in 2 different columns from the same dataframe.

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. If the intention is to returns match from any values in 'column2' with that of 'column1', use %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. and 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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