Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过循环比较R中所有可能的对象组合_R_Loops_Asbio - Fatal编程技术网

如何通过循环比较R中所有可能的对象组合

如何通过循环比较R中所有可能的对象组合,r,loops,asbio,R,Loops,Asbio,假设我有二进制向量,需要将它们与Kappa函数进行类似的比较 library(asbio) A <- c(0,1,1,1,1,1,0) B <- c(0,0,1,0,1,0,1) C <- c(1,0,0,1,1,0,0) D <- c(1,1,0,0,0,1,1) E <- c(1,0,0,1,1,0,1) 如何循环Kappa函数以获得所有可能比较的表 我想得到这样的东西: A B C D E A 100 42 - - -

假设我有二进制向量,需要将它们与
Kappa
函数进行类似的比较

library(asbio)
A <- c(0,1,1,1,1,1,0)
B <- c(0,0,1,0,1,0,1)
C <- c(1,0,0,1,1,0,0)
D <- c(1,1,0,0,0,1,1)
E <- c(1,0,0,1,1,0,1)
如何循环
Kappa
函数以获得所有可能比较的表

我想得到这样的东西:

    A   B   C   D   E
A 100  42   -   -   -
B  42 100   -   -   -
C   -   - 100   -   -
D   -   -   - 100   -
E   -   -   -   - 100
你可以使用这个函数


你也可以不使用任何软件包:
sapply(seq_len(nrow(mat)),function(i)(colSums(mat[i,]==t(mat))/ncol(mat))*100)
where
mat=rbind(A,B,C,D,E)
.Hi@digEmAll!我在问题中使用了您的代码(请查看是否感兴趣):
    A   B   C   D   E
A 100  42   -   -   -
B  42 100   -   -   -
C   -   - 100   -   -
D   -   -   - 100   -
E   -   -   -   - 100
library(asbio)

A <- c(0,1,1,1,1,1,0)
B <- c(0,0,1,0,1,0,1)
C <- c(1,0,0,1,1,0,0)
D <- c(1,1,0,0,0,1,1)
E <- c(1,0,0,1,1,0,1)


M <- rbind(A,B,C,D,E)

res <- outer(1:nrow(M),
             1:nrow(M),
             FUN=function(i,j){
               # i and j are 2 vectors of same length containing 
               # the combinations of the row indexes. 
               # e.g. (i[1] = 1, j[1] = 1) (i[2] = 1, j[2] = 2)) etc...
               sapply(1:length(i),
                      FUN=function(x) Kappa(M[i[x],],M[j[x],])$ttl_agreement )
             })
row.names(res) <- c('A','B','C','D','E')
colnames(res) <- c('A','B','C','D','E')

#> res
          A         B         C         D         E
# A 100.00000  42.85714  42.85714  28.57143  28.57143
# B  42.85714 100.00000  42.85714  28.57143  57.14286
# C  42.85714  42.85714 100.00000  28.57143  85.71429
# D  28.57143  28.57143  28.57143 100.00000  42.85714
# E  28.57143  57.14286  85.71429  42.85714 100.00000
M <- rbind(A,B,C,D,E)

res <- matrix(NA,nrow=5,ncol=5) # pre-allocate the matrix
combs <- expand.grid(1:nrow(M),1:nrow(M))
for(i in 1:nrow(combs)){
  r <- combs[i,1]
  c <- combs[i,2]
  res[r,c] <- Kappa(M[r,],M[c,])$ttl_agreement
}
row.names(res) <- c('A','B','C','D','E')
colnames(res) <- c('A','B','C','D','E')