[R] choosing selective data with permutations

Dennis Murphy djmuser at gmail.com
Wed Aug 10 22:51:43 CEST 2011


Hi:

Your R terminology needs a little work, but I think this is what you're after:

# read your example data from e-mail into an R session
dd <- read.table(textConnection("
Ind1   11  00   12  15  28
Ind2  21 33 22 67 52
Ind3  22 45 21 22 56
Ind4  11  25   74  77  42
Ind5  41 32 67 45 22"), header = FALSE)
closeAllConnections()

# generate a matrix whose rows yield three unique indices:
rows2sample <- t(replicate(100, sample(seq_len(nrow(dd)), 3, replace = FALSE)))

# Generate a list, each component of which contains a sub-data frame.
# lapply() actually replaces a for loop in this case; the function simply
# samples the rows of dd that correspond to the indices in the i-th row
# of rows2sample.
# seq_len(nrow(rows2sample)) is a more long-winded, but safer, way
# to say 1:100.
subdataList <- lapply(seq_len(nrow(rows2sample)),
                                    function(i) dd[rows2sample[i, ], ])

My results yielded the following:
> head(rows2sample, 2)  # first two rows of the index matrix
     [,1] [,2] [,3]
[1,]    2    5    1
[2,]    4    1    3
> length(subdataList)
[1] 100
> subdataList[[1]]
    V1 V2 V3 V4 V5 V6
2 Ind2 21 33 22 67 52
5 Ind5 41 32 67 45 22
1 Ind1 11  0 12 15 28
> subdataList[[2]]
    V1 V2 V3 V4 V5 V6
4 Ind4 11 25 74 77 42
1 Ind1 11  0 12 15 28
3 Ind3 22 45 21 22 56

...so it looks like it's doing the right thing. Now you need to learn
how to process lists, and for that the Introduction to R manual is a
good place to get started.

HTH,
Dennis


On Wed, Aug 10, 2011 at 11:37 AM, Vikram Chhatre
<crypticlineage at gmail.com> wrote:
> Hello,
>
> I am a R beginner and hoping to obtain some hints or suggestions about
> using permutations to sort a data set I have.
>
> Here is an example dataset:
>
>
> Ind1   11  00   12  15  28
> Ind2  21 33 22 67 52
> Ind3  22 45 21 22 56
> Ind4  11  25   74  77  42
> Ind5  41 32 67 45 22
>
> This will be read into a variable using read.table.  What I want to do
> is permute these individuals and every time pick 3 individuals and
> write them to a new variable.  I want to do this 100 times so that in
> the end I will have 100 tables containing data for 3 individuals each.
>  The data (for individuals) itself is not to be permuted, rather the
> selection of individuals.
>
> I am guessing this is probably trivial to do.  But I would appreciate
> any advice on this matter.
>
> Thank you.
> Vikram
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list