【问题标题】:populating a matrix with values that are a function of data from other matrices用来自其他矩阵的数据的函数值填充矩阵
【发布时间】:2017-05-19 10:17:09
【问题描述】:

我有一个关于在 R 中使用矩阵的问题 - 如果其中任何一个笨拙或不清楚,请原谅 - 我仍然是 R 初学者。

我有 2 个矩阵结构如下:

一个整数值的组织到组织矩阵,表示组织之间的值关系:

orgorg <- matrix(sample.int(50, 5*5, TRUE), 5, 5)
colnames(orgorg) <- colnames(orgorg, do.NULL = FALSE, prefix = "org")
rownames(orgorg) <- rownames(orgorg, do.NULL = FALSE, prefix = "org")

和组织的二进制人,指示哪些人属于哪些组织:

personorg <- matrix(sample(0:1,10*5, replace=TRUE),10,5)
colnames(personorg) <- colnames(personorg, do.NULL = FALSE, prefix = "org")
rownames(personorg) <- rownames(personorg, do.NULL = FALSE, prefix = "per")

我已经创建了如下的第三人称矩阵:

 npep=length(unique(rownames(personorg)))
 personperson <- matrix(0, npep, npep)

我想用以下方式填充这个矩阵的元素:

对于 personperson 矩阵 [person i, person j] 中的每个元素,我想查找每个人所属的组织(来自 personorg 矩阵),然后使用 orgorg 中的值填充该元素这些组织的矩阵。

因此,例如,如果 person1 是 org2 的成员,而 person2 是 org4 的成员,则 [per1, per2] 的 personperson 矩阵中的元素将是 [org2, org4] 的 orgorg 矩阵中的元素。

如果一个元素 [i,j] 由多个组织的成员组成,那么我希望该元素填充这些人所属组织之间的平均“距离”。 因此,例如,如果人员 8 是 org2 和 org4 的成员,而人员 9 是 org 1 的成员,那么

orgorg[org1, org2] = 12
orgorg[org1, org4] = 10

然后

personperson[per8,per9] = 11

我希望这很清楚!谢谢!

【问题讨论】:

  • 能否也包括personperson
  • 其实 npep=length(unique(rownames(personperson))) 失败是因为 personperson 没有定义。
  • 如果一个人属于多个组织(如您的示例所示)怎么办?
  • 好点谢谢。在那种情况下,我想要一个函数,它在一个二元组中的人所属的组织之间采取平均“距离”。我会将其添加到问题中。

标签: r matrix


【解决方案1】:

你的问题很有趣。我尝试了一个解决方案,创建两个新功能并使用 for 循环完成。请尝试此代码并告诉我它是否适合您。

# Preamble
orgorg <- matrix(sample.int(50, 5*5, TRUE), 5, 5)
colnames(orgorg) <- colnames(orgorg, do.NULL = FALSE, prefix = "org")
rownames(orgorg) <- rownames(orgorg, do.NULL = FALSE, prefix = "org")

personorg <- matrix(sample(0:1,10*5, replace=TRUE),10,5)
colnames(personorg) <- colnames(personorg, do.NULL = FALSE, prefix = "org")
rownames(personorg) <- rownames(personorg, do.NULL = FALSE, prefix = "per")

npep=length(unique(rownames(personorg)))
personperson <- matrix(0, npep, npep)

rownames(personperson) <- rownames(personorg)
colnames(personperson) <- rownames(personorg)

# combine_custom function
combine_custom <- function(per1, per2, mat){
  one <- mat[per1,]
  one <- names(one)[one!=0]

  two <- mat[per2,]
  two <- names(two)[two!=0]

  if( (length(one) != 0 && length(one) == 1) | (length(two) != 0 && length(two) == 1) ){
    combinations <- combn(c(one, two), 2)
  } else {
    combinations <- matrix(0, 2, 1)
    for(i in 1:length(one)){
      combinations <- cbind(combinations, combn(c(one[i], two), 2))
    }
    combinations <- combinations[,-1]
  }

  combinations <- unique(combinations, MARGIN=2)
}


# ext function
ext <- function(x, mat){
  y <- mat[x[1],x[2]]
  y
}

# For loop
for(i in rownames(personperson)){
  for(j in colnames(personperson)){
    personperson[i,j] <- mean(apply(combine_custom(i, j, personorg), 2, function(x) ext(x=x, mat=orgorg)))
  }
}

【讨论】:

  • 非常感谢您的回复,有时间我会查看代码!
猜你喜欢
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-06
  • 2014-03-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多