1

我知道这个问题的标题很混乱,如果没有错的话。对此感到抱歉,让我解释一下我尝试做的事情:

# I have a population of individuals:
population <- c("Adam", "Bob", "Chris", "Doug", "Emily", "Frank", "George","Harry", "Isaac", "Jim", "Kyle", "Louis")
population_size <- length(population) # this is 12

# I then draw a sample from this population
mysample_size <- 5
mysample <- sample(population,mysample_size, replace=FALSE)

# I then simulate a network among the people in the sample
frn <- matrix(rbinom(mysample_size*mysample_size, 1, 0.4),nrow=n)
x[x<=0] <- 0
x[x>0] <- 1
rownames(frn) <- mysample 
colnames(frn) <- mysample

*我现在想将 frn 中的值转移到一个包含原始总体中所有成员的矩阵中,即 12 x 12 矩阵。该矩阵中的值仅来自 frn 5*5 矩阵。

我不知道如何从顶部的矩阵生成底部的矩阵。

我想过不同的方法(例如使用 iGraph 和通过边缘列表推进)或运行循环,但并没有真正找到一个单一的替代方案来运行。了解背景可能很重要:我的实际矩阵比这大得多,我需要多次运行此操作,因此一个有效的解决方案会很棒。非常感谢你的帮助。

4

3 回答 3

0
# create an empty matrix with NAs. You may have the full matrix already.
full_matrix <- matrix(rep(NA, population_size*population_size), nrow=population_size)
rownames(full_matrix) <- colnames(full_matrix) <- population
frn <- matrix(rbinom(mysample_size*mysample_size, 1, 0.4), nrow = mysample_size)
rownames(frn) <- colnames(frn) <- mysample
# Find the locations where they match
tmp <- match(rownames(frn), rownames(full_matrix))
tmp2 <- match(colnames(frn), colnames(full_matrix))

# do a merge
full_matrix[tmp,tmp2] <- frn
于 2012-06-08T05:50:25.247 回答
0

最简单的解决方案:ind = match(mysample,population)为您提供与样本对应的行和列的索引号,因此popn通过执行更新总体网络矩阵popn[ind,ind] = frn。完毕。

于 2012-06-08T06:48:46.460 回答
0

您可以使用...稀疏矩阵。

library(Matrix)
# Make sure the columns match
population <- c( mysample, setdiff(population, mysample) )
ij <- which( frn != 0, arr.ind=TRUE )
m <- sparseMatrix( 
  i = ij[,1], j=ij[,2], 
  x = 1,  # or frn[ij]
  dim = length(population)*c(1,1), 
  dimnames = list(population, population) 
)
m
于 2012-06-08T07:48:22.397 回答