[R] Randomly shuffle an array 1000 times

Steve Lianoglou mailinglist.honeypot at gmail.com
Mon Oct 18 14:59:37 CEST 2010


Hi,

On Mon, Oct 18, 2010 at 7:37 AM, Peter Francis <peterfrancis at me.com> wrote:
> Dear List,
>
> I have a table i have read into R:
>
> Name    Yes/No
>
> John    0
> Frank   1
> Ann             0
> James   1
> Alex    1
>
> etc  - 800 different times.
>
> What i want to do is shuffle yes/no and randomly re-assign them to the name.

I guess you mean that you have a data.frame object. Let's say this is
called "response". You can get 800 permutations of response like so:

R> responses <- lapply(1:800, function(x) {
  x <- response
  x[,2] <- sample(x[,2])
  x
})

You can also look at "replicate" and use it in a similar fashion.

> I have used sample() and permute(), however there is no way to do this 1000 times. Furthermore, i want to copy the data into a excel spreadsheet in the same order as the data was input so i can build up a distribution of the statistic for each name.

It seems like you can do that in R, but if you want to use excel, you
can include a call to `write.table` before on x before you return it
from the lapply/replicate loop, then have excel import a tab/comma
delimited file.

There are some R packages that I believe write to excel format
directly, but I can't recall what they were ... this might be one:

http://www.omegahat.org/RExcelXML/

.. I'm pretty sure there is another, but it escapes me.

-steve

-- 
Steve Lianoglou
Graduate Student: Computational Systems Biology
 | Memorial Sloan-Kettering Cancer Center
 | Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact



More information about the R-help mailing list