[R] A problem about all possible sequences
(Ted Harding)
ted.harding at nessie.mcc.ac.uk
Tue Apr 17 17:40:45 CEST 2007
On 17-Apr-07 14:26:15, Paul Smith wrote:
> Dear All
>
> Suppose a sequence of length 10 generated by the following rule:
> the first element is 0 with 50% of probability or 1 with the
> same probability; the second element likewise; and so on.
You don't say whether the elements of the sequence are independent,
but plausibility suggests that this may be what you intend.
In which case:
> Is there some R command to obtain all possible different sequences
> formed by the above rule?
while(TRUE){print(sample(c(0,1),10,replace=TRUE))}
and just wait!
(Expected time to wait: about 7700 iterations, I think).
> I am aware that one could write a small
> program to do that, but I am speculating about whether a command
> is already existent.
Taking my tongue out of my cheek, however, it's not clear what you
are really asking for.
If really you want to generate those sequences randomly according
to your probabilistic description, retaining as you go only those
which have not been sampled before, until you have all 2^10
possible sequences, then my "suggestion" above is not the way
to do it! And as far as I know there is not an R function which
does this by proceeding in exactly that way.
Better to recognise that your random scheme means that each
possible sequence is equally likely with all the others, and
so you can do the equivalent by sampling 1024 from (1:1024)
without replacement, i.e. putting (1:1024) in random order.
Then the binary representation of each element is such a
sequence.
So
S<-sample((1:1024),1024)
is an existing R function which does the heart of the job.
(It remains to convert each integer K in S to binary form,
but as far as I know there is not an R function to convert
an integer K directly into a vector of binary 0/1 with a
given number of digits, i.e. not the equivalent of
to.binary(13,10) --> c(0,0,0,0,0,0,1,1,0,1)
except maybe in some special package, so I think you'll end
up writing your own for this bit anyway).
It gets more interesting if your example is just an illustraton,
and what you really want is more general.
E.g. if the different 0/1 outcomes in the 10 positions do
not have the same probabilities, but are still independent,
then you have to do more spadework (and again I'm pretty
sure there is no simple function in R to do it).
In that case it's definitely a programming job.
Even more so if the successive 0/1 outcomes are not independent,
whether P[0] = P[1] = 0.5 in each position or not. So again
a prgramming job.
Since you seem to be quite willling to do the programming
if necessary, I won't try to spoil your fun on that front!
Best wishes,
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <ted.harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 17-Apr-07 Time: 16:40:42
------------------------------ XFMail ------------------------------
More information about the R-help
mailing list