簡體   English   中英

在 R 中使用 != 比較一個 Dataframe 中的列

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

我試圖在來自同一數據幀的 2 個不同列中找到不相等的值。

我正在使用

df %>%
  filter(column1 != column2)

然而,它正在返回一些似乎相等的值。 但是它不會返回與其對應的相同的值集

df %>%
  filter(column1 == column2)

兩列都是雙數據類型。

我應該找出不同的方法來比較它們嗎?

!= 查詢中顯示相等值的示例

==!=是元素比較,即將 column1 的第一個值與 column2 的第一個值進行比較,將第二個值與第二個值進行比較,依此類推。 如果目的是將“column2”中的任何值與“column1”中的任何值返回匹配,請使用%in%

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

相反將否定( !

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

關於 OP 的描述However it is returning some values that seem to be equal. Both columns are the double data type. . 列是雙重的,這是一個棘手的情況,即如果是元素,它還必須考慮精度以使它們相等。 由於沒有可重復的例子,它僅基於假設。 一種選擇是round列值並進行元素比較

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM