[R] Randomly shuffle an array multiple times

Peter Langfelder peter.langfelder at gmail.com
Mon Oct 18 18:31:13 CEST 2010


On Mon, Oct 18, 2010 at 4:38 AM, John Haart <another83 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 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. When i use shuffle the date gets returned like this -
>
> [1] 1 0 0 1 0 1 0 0 0 1 1 1 1 1 1 1 1 1 0 1 0 0 0 0 0 0 1 0 0 0 1 0 1
>  [34] 0 1 0 0 0 0 0 0 0 1 0 1 1 0 1 0 0 0 0 0 0 1 1 0 0 1 0 1 1 1 0 0 0
>  [67] 0 0 0 0 0 0 0 1 1 1 0 1 0 0 1 1 0 1 0 1 0 1 1 1 0 0 1 0 0 1 1 1 1
> [100] 1 1 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 1 0 0 1 1 0 0 0 1 0 1 0 0
> [133] 0 0 0 0 0 0 1 0 1 1 0 1 1 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 0 0 1 0
> [166] 0 0 0 1 1 1 0 1 0 1 0 1 0 0 0 1 0 0 0 0 0 0 1 0 1 1 1 0 0 1 1 0 1
> [199] 0 0 0 0 1 0 1 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 1 0 1 1 0 0 0 1 0 0 1
> [232] 0 0 0 1 1 0 1 0 0 1 0 0 0 0 1 0 1 0 1 0 1 0 1 0 0 1 0 0 0 0 0 0 1
> [265] 0 1 0 0 0 1 0 0 0 1 0 1 1 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 0 1
> [298] 0 1 1 0 0 0 0 1 0 1 0 1 0 0 0 0 0 0 0 1 0 0 0 1 1 0 0 0 1 0 0 1 0
> [331] 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 1 0 0 1 0 0 1 0 1 0 0 0 0 1 0 1 1
>
> etc
>
> Rather than like this, is there a way to change the output?
>
> John    0
> Frank   1
> Ann             0
> James   1
> Alex    1
>
> Can anyone suggest a script that would achieve this?

I'm sure there is a more elegant way, but here's one. Assume your
original table is contained in the variable tab1 that has 2 columns,
one with name and one with the 1/0.

Do this:

nPermutations = 1000;
mat1000base = matrix(tab1[, 2], nrow(tab1), nPermutations);
set.seed(10) # For reproducibility
mat1000 = apply(mat1000base, 2, sample);
tab1000 = data.frame(name = tab1[, 1], mat1000);

tab1000 is the result you want, you can save it as a csv:

write.csv(tab1000, file = "tablePermuted1000Times.csv")

Peter



More information about the R-help mailing list