Search code examples
rinputoutputeconomics

How to extract values from a vector and create a diagonal matrix?


I have a vector of n numbers, for simplicity assume that

test <- c(1:100)

It is simple to construct a diagonal matrix for a vector with diag().

However, I want to extract every value of the vector and create a 4x4 matrix with the extracted value being in i = 1 and j = 1 (upper left hand corner) and all other values being zero.

Personally, I have no clue whatsoever how to accomplish that.

Why do I want to do that? I'm performing Input/Output analysis and want to calculate the inoperability of a sector. For that I need the sector recovery time which is in a vector of 1000 randomly generated recovery times from a pert distribution.

To be more precise:

If I have this vector from 1:100 I want to extract every value from 1:100 and create a separate matrix that looks like this (for 1 to 100):

1 0 0 0 
0 0 0 0 
0 0 0 0

Solution

  • A slightly shorter version would be

    lapply(1:100, function(x) matrix(c(x, rep(0, 15)), 4))