Skip to content

rearrange set of items randomly

8 messages · flokke, (Ted Harding), David Winsemius +1 more

#
Dear all,
I hope that this question is not too weird, I will try to explain it as good
as I can.

I have to write a function for a school project and one limitation is that I
may not use the in built function sample()

At one point in the function I would like to resample/rearrange the items of
my sample (so I would want to use sample, but I am not allowed to do so), so
I have to come up with sth else that does the same as the in built function
sample()

The only thing that sample() does is rearranging the items of a sample, so I
searched the internet for a function that does that to be able to use it,
but I cannot find anything that could help me. 

Can maybe someone help me with this?
I would be very grateful,

Cheers,
Maria

--
View this message in context: http://r.789695.n4.nabble.com/rearrange-set-of-items-randomly-tp4013723p4013723.html
Sent from the R help mailing list archive at Nabble.com.
#
On Nov 7, 2011, at 4:09 PM, flokke wrote:

            
May I suggests thinking about the ordering of random variables?
#
Sorry, but I dont think that I get what you mean (I am a quite new user
though)
I hae a data file of a sample, so I cannot use random numbers, the only
thing I want to do is to randomly reassign the order of the items. 



--
View this message in context: http://r.789695.n4.nabble.com/rearrange-set-of-items-randomly-tp4013723p4015161.html
Sent from the R help mailing list archive at Nabble.com.
#
On 08-Nov-11 07:46:15, flokke wrote:
The simplest method of randomly re-ordering is to use
sample() to re-arrange (1:N) randomly, where N is the
number of items in the data. Then use the result to
access the items. Example:

  D <- data.frame(X1=c(1.1,2.1,3.1,4.1),
                  X2=c(1.2,2.2,3.2,4.2))
  N <- nrow(D)
  ix <- sample((1:N))
  D
  #    X1  X2
  # 1 1.1 1.2
  # 2 2.1 2.2
  # 3 3.1 3.2
  # 4 4.1 4.2
  N
  # [1] 4
  ix
  # [1] 3 2 4 1
  D[ix,]
  #    X1  X2
  # 3 3.1 3.2
  # 2 2.1 2.2
  # 4 4.1 4.2
  # 1 1.1 1.2

Note that the defaults for sample() are (see ?sample):

  For 'sample' the default for 'size' is the number
  of items inferred from the first argument, so that
  'sample(x)' generates a random permutation of the
  elements of 'x' (or '1:x').

and the default for the 'replace' option is "FALSE",
so sample((1:N)) samples N from (1:N) without replacement,
i.e. a random permutation.

Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <ted.harding at wlandres.net>
Fax-to-email: +44 (0)870 094 0861
Date: 08-Nov-11                                       Time: 08:59:35
------------------------------ XFMail ------------------------------
#
On 08-Nov-11 08:59:38, Ted Harding wrote:
While I am at it, an alternative to this use of sample()
is to use order() to find the permutation which re-arranges
a set of random numbers into increasing order. This in effect
returns a random permutation of (1:N). Hence, instead of
"ix <- sample(1:N))" in the above, you could use:

  ix <- order(runif(N))

Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <ted.harding at wlandres.net>
Fax-to-email: +44 (0)870 094 0861
Date: 08-Nov-11                                       Time: 09:30:33
------------------------------ XFMail ------------------------------
#
Thanks for the replies!

Indeed, I could use order() instead of sample()  but that it wouldnt be
random anymore, as it sorts data points in increasing(!) order. Second, I
have to use 1000 different samples. 

I got the hint that I have to do somethin with indeces, but still cant
figure out what this should be. 
Maybe someone of you knows?

--
View this message in context: http://r.789695.n4.nabble.com/rearrange-set-of-items-randomly-tp4013723p4016613.html
Sent from the R help mailing list archive at Nabble.com.
#
On Nov 8, 2011, at 11:31 AM, flokke wrote:

            
Please re-read the help page for `order` _and_ the one for `sort`,  
examine their differences,  re-read the replies you have gotten so  
far, work through _all_ of the examples, and if illumination still  
does not arrive, then repeat the above process until it does.
Many (if not most)  of us do. We try to avoid having r-help becoming a  
homework tutoring site, however. You should be using your academic  
resources for this effort.
6 days later
#
If you don't want to go with the simple method mentioned by David and Ted, or you just want some more theory, you can check out: http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle and implement that.