Many thanks Jim for your help. I am trying to apply the permutations with a
sequence of 20 but I obtain the error message:
Error in matrix(NA, nrow = nrows, ncol = lenx) :
invalid 'nrow' value (too large or NA)
In addition: Warning message:
In matrix(NA, nrow = nrows, ncol = lenx) :
NAs introduced by coercion to integer range
Here is the code:
r <- t(restrictedparts(10, 20))
r <- split(r, seq(nrow(r)))
rp <- crank::permute(r[[sample(1:length(r), 1)]])
In this case, Is it correct to permute the elements of a vector rather than to
permute a vector ?
Many thanks for your time.
Have a nice day
De : Jim Lemon <drjimle...@gmail.com>
Envoyé : mardi 10 juillet 2018 17:44:13
À : Nelly Reduan
Objet : Re: [R] Generate N random numbers with a given probability and condition
I may not have the right idea about this, but I think you need to do
this in two steps if it can be done. Let's say you want a sequence of
20 (N) numbers between 0 and 10 that sums to 10 (M). You can enumerate
the monotonically increasing sequences like this:
So if you select one of these sequences at random, there will be a
further set of sequences that are permutations of it. By randomly
selecting one of those permutations, I think you can solve your
problem. However, this is going to be computationally intensive, with
the set of permutations being very large for large N. Here is an
example using N = M = 5:
# enumerate the sequences = M
# generate the permutations for one sequence (120 in this case)
# select one of the permutations at random
 4 0 1 0 0
On Wed, Jul 11, 2018 at 10:11 AM, Nelly Reduan <nell.r...@hotmail.fr> wrote:
> Thank you very much for your reply.
> By omitting the probability, the expected results could be:
> c(2, 0, 0, 0, 0, 0, 1, 0, 5, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0)
> c(0, 0, 1, 0, 0, 1, 1, 0, 6, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0)
> If I omit the probability, I would like to generate N random positive
> integers that sum to M and the integers would be selected from a uniform
> Many thanks for your time
> De : Rolf Turner <r.tur...@auckland.ac.nz>
> Envoyé : mercredi 4 juillet 2018 16:11:11
> À : Nelly Reduan
> Cc : firstname.lastname@example.org
> Objet : Re: [R] Generate N random numbers with a given probability and
> On 05/07/18 10:21, Nelly Reduan wrote:
>> Dear all,
>> I would like to generate N random numbers with a given probability and
>> condition but I'm not sure how to do this.
>> For example, I have N = 20 and the vector from which to choose is seq(0, 10,
>> 1). I have tested:
>> x <- sample(seq(0, 10, 1), 20, replace=TRUE, prob=rep(0.28,
>> times=length(seq(0, 10, 1))))
>> But I don�t know how to put the condition sum(x) <= max(seq(0, 10, 1)).
>> Many thanks for your time.
> Your thinking requires considerable clarification.
> (1) Note that seq(0,10,1) is just 0, 1, 2, ..., 10.
> (2) Hence length(seq(0,10,1)) is 11.
> (3) Likewise max(seq(0,10,1)) is 10.
> (4) Your prob vector is *constant* --- so specifying "prob" makes
> no difference --- the result is the same as if you omitted "prob".
> (5) You need to think carefully about what you really mean by "random".
> In what way do you want the final result to be "random"?
> I expect that the lecturer who assigned this problem to you needs to
> clarify his/her thinking as well.
> Rolf Turner
> Technical Editor ANZJS
> Department of Statistics
> University of Auckland
> Phone: +64-9-373-7599 ext. 88276
> [[alternative HTML version deleted]]
> Remail@example.com mailing list -- To UNSUBSCRIBE and more, see
> 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]]
Rfirstname.lastname@example.org mailing list -- To UNSUBSCRIBE and more, see
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.