[R] Randomly shuffle an array 1000 times

Peter Francis peterfrancis at me.com
Mon Oct 18 13:37:08 CEST 2010


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

John	0
Frank	1
Ann		0
James	1
Alex	1

Can anyone suggest a script that would achieve this?

Thanks

Peter



More information about the R-help mailing list