# [R] Margins to fill matrix

David Winsemius dwinsemius at comcast.net
Thu Sep 11 22:33:30 CEST 2014

```On Sep 11, 2014, at 5:13 AM, Stefan Petersson wrote:

> I have :
>
> rs <- c(3, 2, 3, 4)
> cs <- c(4, 5, 3)
>
> And want:
>
>> matrix
>    [,1] [,2] [,3]
> [1,] 1    2    0
> [2,] 1    0    1
> [3,] 1    1    1
> [4,] 1    2    1
>
> The rowSums in the above matrix is equal to sum(rs) and colSums is
> equal to sum(cs). It's sort of a matrix expansion where the margins
> are known beforehand...
>

Looks like an integer programming question. 7 integer equations with 12 unknowns, so probably not specifying a unique solution. Have you looked at the appropriate Task Views?

Best;
David.

> I hope I make sense.
>
>
> 2014-09-11 14:09 GMT+02:00 Charles Determan Jr <deter088 at umn.edu>:
>> Do you have an example of what you would like your output to look like?  It
>> is a little difficult to fully understand what you are looking for.  You
>> only have 18 values but are looking to fill at 10x8 matrix (i.e. 80 values).
>> If you can clarify better we may be better able to help you.
>>
>> Charles
>>
>>
>> On Thu, Sep 11, 2014 at 3:47 AM, Stefan Petersson <stefan at inizio.se> wrote:
>>>
>>> Hi,
>>>
>>> I have two vector of margins. Now I want to create "fill" matrix that
>>> reflects the margins.
>>>
>>> seats <- c(17,24,28,30,34,36,40,44,46,50)
>>> mandates <- c(107,23,24,19,112,19,25,20)
>>>
>>> Both vectors adds up to 349. So I want a 10x8 matrix with row sums
>>> corresponding to "seats" and column sums corresponding to "mandates".
>>>
>>> ______________________________________________

>

David Winsemius
Alameda, CA, USA

```