Re: [R] permutation/randomization

From: Tobias Verbeke <tobias.verbeke_at_telenet.be>
Date: Wed, 09 Apr 2008 22:11:45 +0200

Grant Gillis wrote:
> Hello,
>
> I have what I suspect might be an easy problem but I am new to R and
> stumped. I have a data set that looks something like this
>
> b<-c(2,3,4,5,6,7,8,9)
> x<-c(2,3,4,5,6,7,8,9)
> y<-c(9,8,7,6,5,4,3,2)
> z<-c(9,8,7,6,1,2,3,4)
> data<-cbind(x,y,z)
> row.names(data)<-c('a','b','c','d','e','f','g','h')
>
> which gives:
>
> x y z
> a 2 9 9
> b 3 8 8
> c 4 7 7
> d 5 6 6
> e 6 5 1
> f 7 4 2
> g 8 3 3
> h 9 2 4
>
> I would like to randomize data within columns. The closest I have been able

> to come permutes data within the columns but keeps rows together along with
> row names(example below). For some context, eventually I would like use
> this to generate many data sets and perform calculations on these random
> data sets (I think I know how to do this but we'll see). So ideally I would
> like row names to remain the same (in order a through h) and column data to
> randomize within columns but independently of the other columns. Just
> shuffle the data deck I guess
>
>

>> data[permute(1:length(data[,1])),]

> x y z
> b 3 8 8
> c 4 7 7
> h 9 2 4
> e 6 5 1
> f 7 4 2
> a 2 9 9
> g 8 3 3
> d 5 6 6

I changed 'data' to 'mymat' as 'data' is a function in R (see ?data) and added the row names after permuting:

someMatrix <- cbind(x, y, z)
permutedMat <- apply(someMatrix, 2, sample) row.names(permutedMat)<-c('a','b','c','d','e','f','g','h') permutedMat

HTH,
Tobias



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed 09 Apr 2008 - 20:13:40 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 09 Apr 2008 - 20:30:40 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive