如何计算非对角元素?

时间:2015-10-09 02:52:02

标签: r matrix

我有一个矩阵,我需要从该矩阵的非对角线中提取值。但是,在下面的R代码中,我编写了一个只读取矩阵行的代码。我怎么能纠正我的R代码?假设矩阵中的每一行都对应一个个体,则值是两个个体之间的相关性。例如,我想知道矩阵的第1行" var",有多少元素在上面0.80等等。

var<-matrix(c(1,0.9,0.8,0.7,0.6,0.9,1,0.7,0.8,0.5,0.8,0.7,1,0.5,0.4,0.7,
              0.8,0.5,1,0.3,0.6,0.5,0.4,0.3,1),ncol=5)


rowmatrix=1:nrow(var)
OUT=NULL        

for (x in rowmatrix) {                       
  row=c(var[x,])
  count<-sum(row>=0.8)
  count1<-count-1
  if(count1 < 0) {
    count1=0 
  }

  output=cbind(x,count,count1)                        
  OUT <<- rbind(OUT,output)

}

colnames(OUT) <- c("index.ind","countrow","countrow_withoutdiag")
OUT

4 个答案:

答案 0 :(得分:1)

> sum(var[lower.tri(var)] > 0.8)
[1] 1
> (sum(var > 0.8) - sum(diag(var) > .8))/2
[1] 1

答案 1 :(得分:1)

我认为这会给你你想要的东西:

lowcount <- rowSums(replace(var, upper.tri(var,diag=TRUE), NA) >= 0.8, na.rm=TRUE)
uppcount <- rowSums(replace(var, lower.tri(var,diag=TRUE), NA) >= 0.8, na.rm=TRUE)
cbind(OUT,lowcount,uppcount)

#     index.ind countrow countrow_withoutdiag lowcount uppcount
#[1,]         1        3                    2        0        2
#[2,]         2        3                    2        1        1
#[3,]         3        2                    1        1        0
#[4,]         4        2                    1        1        0
#[5,]         5        1                    0        0        0

正如您所看到的,添加lowcount + uppcount与您现有的countrow_withoutdiag匹配 - 因此数字似乎有效。

答案 2 :(得分:0)

要计算多少元素&gt; = 0.8 do:

sum(var>=.8)

这将包括符合标准的对角线上的任何元素 如果您不想计算它们,请减去sum(diag(var)>=.8)

sum(var>=.8) - sum(diag(var)>=.8)

计算每行的阈值以上的元素数量,但仅限于上对角线
我将编辑您的程序以获得上述计数。

var<-matrix(c(1,0.9,0.8,0.7,0.6,0.9,1,0.7,0.8,0.5,0.8,0.7,1,0.5,0.4,0.7,
              0.8,0.5,1,0.3,0.6,0.5,0.4,0.3,1),ncol=5)

# don't include the last row (it has no element above the diagonal)
rowmatrix = 1:(nrow(var)-1)

OUT = NULL

for (x in rowmatrix) {                       
  # The row will subset the current row with columns from x+1 to end
  row = var[x,(x+1):nrow(var)]
  count <- sum(row >= 0.8)
  # since the diagonal element is not in the subset these lines are no longer needed.
  # "count" is now the number of elements >= 0.8 without the diagonal

  # count1<-count-1
  #if(count1 < 0) {
  #  count1=0 
  #}

  # if we assume diagonal elements equal to 1 then the next line changes to:
  output=cbind(x,count + 1, count)

  OUT <<- rbind(OUT,output)

}

colnames(OUT) <- c("index.ind","countrow","countrow_withoutdiag")
OUT

结果:

> OUT
     index.ind countrow countrow_withoutdiag
[1,]         1        3                    2
[2,]         2        2                    1
[3,]         3        1                    0
[4,]         4        1                    0

答案 3 :(得分:0)

我得到了你想要的东西:

lower<-var*lower.tri(var,diag=FALSE)
for (i in 1:(nrow(var))){
  print(paste("Row ", i," has ",length(which(lower[i,]>=0.8))," values superior or equal to 0.8"))
}

upper<-var*upper.tri(var,diag=FALSE)
for (i in 1:(nrow(var))){
  print(paste("Row ", i," has ",length(which(upper[i,]>=0.8))," values superior or equal to 0.8"))
}

修改

如果你想为每个列计算上三角矩阵,那么:

upper<-var*upper.tri(var,diag=FALSE)
    for (i in 1:(ncol(var))){
      print(paste("Column ", i," has ",length(which(upper[,i]>=0.8))," values superior or equal to 0.8"))
    }