It is not right. My data is the presence-absence data. And I want to get
thousands of presence-absence random data which length of rows and columns
is the same with the former data. Meantime, the new data needs to have the
fixed sums for each row and column with the former data.
For example:
The data "sites":
site1 site2 site3 site4 site5 site6 site7 site8
1 0 0 0 1 1 0 0
0 1 1 1 0 1 0 1
1 0 0 0 1 0 1 0
0 0 0 1 0 1 0 1
1 0 1 0 0 0 0 0
0 1 0 1 1 1 1 1
1 0 0 0 0 0 0 0
0 0 0 1 0 1 0 1
> apply(sites,2,sum)
site1 site2 site3 site4 site5 site6 site7 site8
    4     2     2     4     3     5     2     4
> apply(sites,1,sum)
[1] 3 5 3 3 2 6 1 3

If I get the new data "sites.random":
site1 site2 site3 site4 site5 site6 site7 site8
1 0 0 0 1 1 0 0
1 1 1 1 0 1 0 0
1 0 0 0 1 0 1 0
0 0 0 1 0 1 0 1
0 0 1 0 0 0 0 1
0 1 0 1 1 1 1 1
1 0 0 0 0 0 0 0
0 0 0 1 0 1 0 1
> apply(sites.random,2,sum) # the same with the former data
site1 site2 site3 site4 site5 site6 site7 site8
    4     2     2     4     3     5     2     4
> apply(sites.random,1,sum) # the same with the former data
[1] 3 5 3 3 2 6 1 3

How can I get the new random data? Thanks.



On 7/8/07, Alan Zaslavsky <[EMAIL PROTECTED]> wrote:
>
> If I understand your problem, this might be a solution.  Assign
> independent random numbers for row and column and use the corresponding
> ordering to assign the row and column indices.  Thus row and column
> assignments are independent and the row and column totals are fixed.  If
> cc and rr are respectively the desired row and column totals, with
> sum(cc)==sum(rr), then
>
> n = sum(cc)
> row.assign = rep(1:length(rr),rr)[order(runif(n))]
> col.assign = rep(1:length(cc),cc)[order(runif(n))]
>
> If you want many such sets of random assignments to be generated at once
> you can use a few more rep() calls in the expressions to generate multiple
> sets in the same way.  (Do you actually want the assignments or just the
> tables?) Of course there are many other possible solutions since you have
> not fully specified the distribution you want.
>
>        Alan Zaslavsky
>        Harvard U
>
> > From: "Zhang Jian" <[EMAIL PROTECTED]>
> > Subject: [R] random sampling with some limitive conditions?
> > To: r-help <[email protected]>
> >
> > I want to gain thousands of random sampling data by randomizing the
> > presence-absence data. Meantime, one important limition is that the row
> and
> > column sums must be fixed. For example, the data "tst" is following:
> >    site1 site2 site3 site4 site5 site6 site7 site8 1 0 0 0 1 1 0 0 0 1 1
> 1 0
> > 1 0 1 1 0 0 0 1 0 1 0 0 0 0 1 0 1 0 1 1 0 1 0 0 0 0 0 0 1 0 1 1 1 1 1 1
> 0 0
> > 0 0 0 0 0 0 0 0 1 0 1 0 1
> >
> > sum(tst[1,]) = 3, sum(tst[,1])=4, and so on. When I randomize the data,
> the
> > first row sums must equal to 3, and the first column sums must equal to
> 4.
> > The rules need to be applied to each row and column.
> > How to get the new random sampling data? I have no idea.
> > Thanks.
>
> ______________________________________________
> [email protected] 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]]

______________________________________________
[email protected] 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.

Reply via email to