首页 文章

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

提问于
浏览
0

我有一个输出,例如如下:

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 回答

  • 1

    在下面的代码中, IDs 是所有不同 ID 对的数据表 . 然后,您需要为每一行检查 x <- df[c(ID1, ID2), -1] ,即对应于给定 ID 对的 df 的非ID列 . 该代码创建一个逻辑向量,对于非零列( x[1] != 0 )是 TRUE ,具有相等元素的列( 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
    ')
    

相关问题