2017-07-17 43 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)