choosing selective data with permutations

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

choosing selective data with permutations

Vikram Chhatre
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: choosing selective data with permutations

Michael Weylandt
To pick random elements to sample, you can just use the sample function

sample(1:5,3,replace=T/F) # pick true or false as needed for your data.

If you replicate this, you should have no problem.

replicate(100,function() return(sample(1:5,3,replace=T/F)))

This will be plenty fast, but if you get into very large scale boot
strapping, you might want to vectorize the whole thing. That's easily done
with replace = T (just take 3*100 samples and then convert the output vector
to a matrix) but I'm not sure its quite as easy with replace = F.

Hope this helps,

Michael

On Wed, Aug 10, 2011 at 2:37 PM, Vikram Chhatre <[hidden email]>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
>
> ______________________________________________
> [hidden email] 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: choosing selective data with permutations

Michael Weylandt
Sorry, that second line of code won't work: do it in 2.

f <- function() {return(sample(1:5,3,replace=T/F))}
replicate(100,f())

Michael


On Wed, Aug 10, 2011 at 3:06 PM, R. Michael Weylandt <
[hidden email]> wrote:

> To pick random elements to sample, you can just use the sample function
>
> sample(1:5,3,replace=T/F) # pick true or false as needed for your data.
>
> If you replicate this, you should have no problem.
>
> replicate(100,function() return(sample(1:5,3,replace=T/F)))
>
> This will be plenty fast, but if you get into very large scale boot
> strapping, you might want to vectorize the whole thing. That's easily done
> with replace = T (just take 3*100 samples and then convert the output vector
> to a matrix) but I'm not sure its quite as easy with replace = F.
>
> Hope this helps,
>
> Michael
>
>
> On Wed, Aug 10, 2011 at 2:37 PM, Vikram Chhatre <[hidden email]>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
>>
>> ______________________________________________
>> [hidden email] 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.
>>
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: choosing selective data with permutations

djmuseR
In reply to this post by Vikram Chhatre
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
<[hidden email]> 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
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.