[R] Margins to fill matrix

Stefan Petersson stefan at inizio.se
Thu Sep 11 22:52:40 CEST 2014


Yes! That's excactly what I need. Thank You so much!
 Den 11 sep 2014 21:20 skrev "David L Carlson" <dcarlson at tamu.edu>:

> You want r2dtable():
>
> > ?r2dtable
> > set.seed(42)
> > a <- r2dtable(1, seats, mandates)
> addmargins(a[[1]])
>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
>  [1,]    2    3    1    0    6    2    1    2   17
>  [2,]    8    0    1    1   11    0    2    1   24
>  [3,]    8    0    5    2    7    1    1    4   28
>  [4,]   10    5    3    1    6    3    0    2   30
>  [5,]   13    4    1    4    9    0    2    1   34
>  [6,]    8    2    2    0   17    3    4    0   36
>  [7,]   13    0    2    6    9    2    3    5   40
>  [8,]   12    4    4    3   12    3    3    3   44
>  [9,]   14    3    3    2   18    0    4    2   46
> [10,]   19    2    2    0   17    5    5    0   50
> [11,]  107   23   24   19  112   19   25   20  349
>
>
> -------------------------------------
> David L Carlson
> Department of Anthropology
> Texas A&M University
> College Station, TX 77840-4352
>
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> On Behalf Of Stefan Petersson
> Sent: Thursday, September 11, 2014 7:13 AM
> To: Charles Determan Jr
> Cc: r-help at r-project.org
> Subject: Re: [R] Margins to fill matrix
>
> 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...
>
> 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".
> >>
> >> ______________________________________________
> >> R-help at r-project.org mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guide
> >> http://www.R-project.org/posting-guide.html
> >> and provide commented, minimal, self-contained, reproducible code.
> >
> >
> >
> >
> > --
> > Dr. Charles Determan, PhD
> > Integrated Biosciences
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list