s-news
[Top] [All Lists]

Re: Permuting a data frame

To: "Walter R. Paczkowski" <dataanalytics@earthlink.net>
Subject: Re: Permuting a data frame
From: Ernst Linder <elinder@cisunix.unh.edu>
Date: Tue, 21 Jun 2005 16:40:02 -0400
Cc: Dimitris Rizopoulos <dimitrios.rizopoulos@student.kuleuven.ac.be>, s-news@lists.biostat.wustl.edu
References: <E1DkiQC-0002Hp-7r@smtpauth07.mail.atl.earthlink.net> <007601c57663$c8dddbf0$0540210a@www.domain>
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.0.1) Gecko/20020823 Netscape/7.0
Hi Walt:

In case you ever want to apply such permutations to large matrices:
Here is a more elegant solution that avoids a for loop:

nr <- 30  # number rows
nc <- 5    # number columns
outer(0:(nr-1),0:(nc-1),"+")%% nr + 1


Ernst Linder






Dimitris Rizopoulos wrote:
maybe you can use something like this:

mat <- matrix(1:10, 10, 5)
#########3
n <- nrow(mat)
for(i in 2:ncol(mat)) mat[, i] <- mat[c(seq(n - i + 2, n), seq(1, n - i + 1)), i]
mat


I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.be/biostat/
    http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm



----- Original Message ----- From: "Walter R. Paczkowski" <dataanalytics@earthlink.net>
To: "S-News" <s-news@lists.biostat.wustl.edu>
Sent: Tuesday, June 21, 2005 3:04 PM
Subject: [S] Permuting a data frame


Good morning,

I have a trivial problem. I want to permute the rows of a data frame with, say, five columns, each column having the same sequence of numbers, 1:n. For example, I want to permute

x1  x2  x3  x4  x5
1    1     1    1    1
2    2     2    2    2
3    3     3    3    3
4    4     4    4    4
5    5     5    5    5

But, after the permutation, there can be no duplications in each row and, of course, each number in each column can appear only once in that column of the resulting data frame . So, I would like something like

x1  x2  x3  x4  x5
1    5    4    3    2
2    1     5   4    3
3    2     1   5    4
4    3     2   1    5
5    4     3   2    1

except the problem I'm working on has 30 rows with five columns.

Is there an easy way to do this? I tried the repeated application of sample with any and duplicated to check for the duplications, but this is very inefficient.

Thanks,

Walt Paczkowski
________________________

Walter R. Paczkowski, Ph.D.
Data Analytics Corp.
44 Hamilton Lane
Plainsboro, NJ  08536
(V) 609-936-8999
(F) 609-936-3733

--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu.  To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message:  unsubscribe s-news

--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu.  To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message:  unsubscribe s-news



--
****************************************************************
Ernst Linder                        elinder@math.unh.edu
Department of Mathematics and Statistics     603 - 862- 2687
University of New Hampshire            Fax:  603 - 862 - 4096
Durham, NH 03824                www.math.unh.edu/~elinder
****************************************************************


<Prev in Thread] Current Thread [Next in Thread>