如何在不删除NA或0的情况下计算匹配对象

时间:2018-12-09 17:40:57

标签: r

例如,我的输出如下:

ID C1 C2 C3 C4 C5 C6
1  0  1  2  2  1  1
2  0  1  1  2  1  1
3  1  0  1  1  1  1
4  2  0  2  2  1  2
5  2  1  1  0  2  2
6  1  2  1  0  1  2
7  2  2  2  2  0  2
8  1  1  1  1  0  1
9  1  1  2  2  2  0
10 1  2  1  2  1  0

并且我通过faster way to compare rows in a data frame

中的示例确定对象的同时出现
for ( i in 1:(nr-1)) {
# all combinations of i with i+1 to nr
samplematch <- cbind(dt[i],dt[(i+1):nr])

# renaming the comparison sample columns
setnames(samplematch,append(colnames(dt),paste0(colnames(dt),"2")))

#calculating number of matches
samplematch[,noofmatches := 0]

for (j in 1:nc){
samplematch[,noofmatches := noofmatches+1*(get(paste0("CC",j)) == get(paste0("CC",j,"2")))]
  }

# removing individual value columns and matches < 5
samplematch <- samplematch[noofmatches >= 5,list(ID,ID2,noofmatches)]

# adding to the list
totalmatches[[i]] <- samplematch
}

通过以上功能获得的结果有助于我识别每个ID之间的总匹配。但是,仅当CC(1:6)仅包含值1和2时,我才识别匹配ID。这意味着每行的总值假定为5,而不是6。

我需要的输出应包含诸如以下信息

ID1 ID2 Match
1    2   4/5
1    3   2/5
1    4   3/5
:    :    :
:    :    :
2    3   3/5
2    4   2/5

如何编写函数而不删除任何行,因为每行的值为0。

1 个答案:

答案 0 :(得分:1)

在下面的代码中,IDs是所有成对的ID对的数据表。然后,您需要为每行检查x <- df[c(ID1, ID2), -1],即与给定的df对相对应的ID的非ID列。该代码为非零列(TRUE)和元素相等的列(x[1] != 0)创建了一个x[2] == x[1]逻辑向量。这个向量的总和就是匹配的数量。

library(data.table)
setDT(df)
setkey(df, ID)

IDs <- CJ(ID1 = df$ID, ID2 = df$ID)[ID1 != ID2]
IDs[, Match := {x <- df[c(ID1, ID2), -1] 
                sum(x[1] != 0 & x[2] == x[1])} 
    , by = .(ID1, ID2)]

head(IDs)
#    ID1 ID2 Match
# 1:   1   2     4
# 2:   1   3     2
# 3:   1   4     3
# 4:   1   5     1
# 5:   1   6     1
# 6:   1   7     2

使用的数据:

df <- fread('
ID C1 C2 C3 C4 C5 C6
1  0  1  2  2  1  1
2  0  1  1  2  1  1
3  1  0  1  1  1  1
4  2  0  2  2  1  2
5  2  1  1  0  2  2
6  1  2  1  0  1  2
7  2  2  2  2  0  2
8  1  1  1  1  0  1
9  1  1  2  2  2  0
10 1  2  1  2  1  0
')