1

I am aware that the title of this question is confusing, if not wrong. Sorry for this, let me explain what I try to do:

# 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

*I would now like to transfer the values from frn into a matrix that includes all members from the original population, i.e. a 12 by 12 matrix. The values in that matrix would only come from the frn 5*5 matrix.

I do not know how to generate the matrix at the bottom from the matrix at the top.

I have thought of different ways (e.g. using iGraph and advancing via edgelists) or running loops, but did not really get a single alternative to run. Maybe important to know as background: My actual matrices are much larger than this and I need to run this operation many times, thus an efficient solution would be great. Thanks a lot for your help.

Maiasaura
  • 32,226
  • 27
  • 104
  • 108
Henning Piezunka
  • 255
  • 1
  • 3
  • 9
  • 1
    What is `x` and how does that fit in here? and `n` in `nrow=n` should be `nrow=mysample_size`, right? – Maiasaura Jun 08 '12 at 05:41
  • Thanks for the editing. You are correct. It should have been "nrow=mysample_size" One would need to replace x by frn. My fault. Sorry. Thanks for paying so much attention – Henning Piezunka Jun 08 '12 at 22:29

3 Answers3

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
Maiasaura
  • 32,226
  • 27
  • 104
  • 108
0

Neatest solution: ind = match(mysample,population) gives you the index numbers of the rows and columns corresponding to the sample, so update the population network matrix popn by doing popn[ind,ind] = frn. Done.

Tim P
  • 1,383
  • 9
  • 19
0

You could use... a sparse matrix.

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
Vincent Zoonekynd
  • 31,893
  • 5
  • 69
  • 78
  • You *could*... but what would the advantage be over doing it directly? – Tim P Jun 08 '12 at 08:29
  • If the matrices are that small, there is no advantage, but if they are much larger, and sparse, it is more memory-efficient. – Vincent Zoonekynd Jun 08 '12 at 08:52
  • very cool. Thanks a lot. This also works (I had started looking at the solution by Tim). For sum reason it suppresses the colnames when displaying m. Not sure why. – Henning Piezunka Jun 08 '12 at 22:28
  • Really not a fan of relying on external packages unnecessarily for exactly this reason - there's always oddities and inconsistencies of various kinds. In my book, the smart first step is always to see if there's a slick way to solve the problem at hand without loading anything else up. – Tim P Jun 08 '12 at 23:50