2017-07-17 204 views
1

我有兩個向量,我想知道向量中的哪些索引不相同。我不知道如何做到這一點,因爲NA == NA產生NANA == 5也產生NA。有人可以提供指導嗎?當我們在兩列NAR:確定兩個向量中的不同元素

# Create data with NA vs. 3 
dat1 <- data.frame(foo = c(NA, 5, 9), 
        bar = c(3, 5, 9)) 

# Create data with NA vs. NA 
dat2 <- data.frame(foo = c(NA, 5, 9), 
        bar = c(NA, 5, 9)) 

# Produces same result 
dat1$foo == dat1$bar 
dat2$foo == dat2$bar 

identical((dat1$foo == dat1$bar), (dat2$foo == dat2$bar)) 
+1

'ind = dat1 $ foo!= dat1 $ bar;哪個(is.na(ind)| ind)' –

+0

簡潔明快。添加爲答案。 – user3614648

回答

3

編輯

下面的解決方案是行不通的。爲了處理這一點,我們可以定義一個函數:

dissimilar_index <- function(dat) { 
    ind = with(dat, (foo == bar) | (is.na(foo) & is.na(bar))) 
    which(is.na(ind) | !ind) 
} 

dissimilar_index(dat1) 
#[1] 1 

dissimilar_index(dat2) 
#integer(0) 

要檢查函數創建一個新的數據幀dat3

dat3 = rbind(dat1, c(2, 3)) 
dat3 
# foo bar 
#1 NA 3 
#2 5 5 
#3 9 9 
#4 2 3 

dissimilar_index(dat3) 
#[1] 1 4 

我們也可以使用,

ind = !with(dat1, is.na(foo) == is.na(bar) & foo == bar) 
which(!is.na(ind) & ind) 
#[1] 1 

ind = !with(dat2, is.na(foo) == is.na(bar) & foo == bar) 
which(!is.na(ind) & ind) 
#integer(0) 

在這裏,我們檢查,如果兩欄均爲NA,兩者相等。

原來的答案

我們可以得到哪些不相似的列的索引,並添加額外的檢查,對NA S使用which拿到指標。

ind = dat1$foo != dat1$bar 
which(is.na(ind) | ind) 

#[1] 1 
+0

其實剛剛意識到它不適用於區分NA == NA和NA = 5。我希望NA == NA生成TRUE /不包含在索引中。 – user3614648

+0

@ user3614648更新了答案。請檢查。 –

+1

我無法想出更好的東西。我唯一可能的合理化是'compNA < - function(x,y)union(which(is.na(x)!= is.na(y)),which(x!= y))'then'compNA(dat3 $ foo,dat3 $ bar)' – thelatemail

0

的一種方法使用sapplyidentical

non_ident_ind <- function(df) { 
    which(!sapply(1:nrow(df), function(i) identical(df$foo[i], df$bar[i]))) 
} 

結果:

non_ident_ind(dat1) 
# [1] 1 
non_ident_ind(dat2) 
# integer(0) 

使用apply的另一種方法:

which(apply(dat1, 1, function(r) length(unique(r)) > 1)) 
# [1] 1 
which(apply(dat2, 1, function(r) length(unique(r)) > 1)) 
# integer(0) 
相關問題