combinations between two vectors

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

combinations between two vectors

R help mailing list-2
Hi all,I am looking for a function that would give me all the combinations between two vectors.Lets take as example the 

test<-seq(1,30000,by=5000)
Browse[2]> test
[1]     1  5001 10001 15001 20001 25001
I want all the combinations between two times the test... I think this is  called permutation so a function that could do permutation(test,test)and produce the following
1,11,50011,100011,15001....
3,13,5001...25001,20001,25001,25001
is there such a function ?
RegardsAlex


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: combinations between two vectors

Sarah Goslee
I can't quite tell what you want: your example output is either
unclear to me or mangled by posting in HTML (please don't).

Is
expand.grid(test, test)
what you want, or partway to what you want?


Sarah

On Thu, Dec 18, 2014 at 9:56 AM, Alaios via R-help <[hidden email]> wrote:

> Hi all,I am looking for a function that would give me all the combinations between two vectors.Lets take as example the
>
> test<-seq(1,30000,by=5000)
> Browse[2]> test
> [1]     1  5001 10001 15001 20001 25001
> I want all the combinations between two times the test... I think this is  called permutation so a function that could do permutation(test,test)and produce the following
> 1,11,50011,100011,15001....
> 3,13,5001...25001,20001,25001,25001
> is there such a function ?
> RegardsAlex
>
>
>         [[alternative HTML version deleted]]
>

--
Sarah Goslee
http://www.functionaldiversity.org

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: combinations between two vectors

David Carlson
Depending on what you want, you probably want to start with expand.grid():

# All combinations of test with test
> pairs1 <- expand.grid(test, test)
> nrow(pairs1)
[1] 36
# Exclude cases that differ only in the order of the values
# E.g. (1, 5001), but not (5001, 1), also (1, 1), etc are included
> pairs2 <- pairs1[pairs1[,1] <= pairs1[,2],]
> nrow(pairs2)
[1] 21
# Same as pairs2 but (1, 1), etc are not included
> pairs3 <- pairs1[pairs1[,1] < pairs1[,2],]
> nrow(pairs3)
[1] 15

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-----Original Message-----
From: R-help [mailto:[hidden email]] On Behalf Of Sarah Goslee
Sent: Thursday, December 18, 2014 9:06 AM
To: Alaios
Cc: [hidden email]
Subject: Re: [R] combinations between two vectors

I can't quite tell what you want: your example output is either
unclear to me or mangled by posting in HTML (please don't).

Is
expand.grid(test, test)
what you want, or partway to what you want?


Sarah

On Thu, Dec 18, 2014 at 9:56 AM, Alaios via R-help <[hidden email]> wrote:

> Hi all,I am looking for a function that would give me all the combinations between two vectors.Lets take as example the
>
> test<-seq(1,30000,by=5000)
> Browse[2]> test
> [1]     1  5001 10001 15001 20001 25001
> I want all the combinations between two times the test... I think this is  called permutation so a function that could do permutation(test,test)and produce the following
> 1,11,50011,100011,15001....
> 3,13,5001...25001,20001,25001,25001
> is there such a function ?
> RegardsAlex
>
>
>         [[alternative HTML version deleted]]
>

--
Sarah Goslee
http://www.functionaldiversity.org

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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 -- To UNSUBSCRIBE and more, see
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: combinations between two vectors

Michael Dewey
In reply to this post by R help mailing list-2


On 18/12/2014 14:56, Alaios via R-help wrote:
> Hi all,I am looking for a function that would give me all the combinations between two vectors.Lets take as example the
>
> test<-seq(1,30000,by=5000)
> Browse[2]> test
> [1]     1  5001 10001 15001 20001 25001
> I want all the combinations between two times the test... I think this is  called permutation

I think is more likely it is a combination problem.
??combination
would have directed you to the solution others have offered
(expand.grid) and other things too.

  so a function that could do permutation(test,test)and produce the
following

> 1,11,50011,100011,15001....
> 3,13,5001...25001,20001,25001,25001
> is there such a function ?
> RegardsAlex
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>
>
> -----
> No virus found in this message.
> Checked by AVG - www.avg.com
> Version: 2015.0.5577 / Virus Database: 4253/8759 - Release Date: 12/18/14
>

--
Michael
http://www.dewey.myzen.co.uk

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: combinations between two vectors

Hemant Rupani
This post has NOT been accepted by the mailing list yet.
In reply to this post by R help mailing list-2
Hi I'm new, sorry that I'm questioning on your question.
What is the use of Browse[2]> test for this
It shows me ,"Error: object  'Browse' not found"
Regards,
HR
Reply | Threaded
Open this post in threaded view
|

Re: combinations between two vectors

Chel Hee Lee
In reply to this post by David Carlson
I like the example provided by David L. Carlson using the function
'expand.grid()' as shown in the previous email.

You may try 'outer()' and 'upper.tri()' instead 'expand.grid()'.  Please
see the below:

 > x <- outer(test, test, paste, sep=",")
 > x
      [,1]      [,2]         [,3]          [,4]          [,5]
[1,] "1,1"     "1,5001"     "1,10001"     "1,15001"     "1,20001"
[2,] "5001,1"  "5001,5001"  "5001,10001"  "5001,15001"  "5001,20001"
[3,] "10001,1" "10001,5001" "10001,10001" "10001,15001" "10001,20001"
[4,] "15001,1" "15001,5001" "15001,10001" "15001,15001" "15001,20001"
[5,] "20001,1" "20001,5001" "20001,10001" "20001,15001" "20001,20001"
[6,] "25001,1" "25001,5001" "25001,10001" "25001,15001" "25001,20001"
      [,6]
[1,] "1,25001"
[2,] "5001,25001"
[3,] "10001,25001"
[4,] "15001,25001"
[5,] "20001,25001"
[6,] "25001,25001"


 > x.idx <- upper.tri(x, diag=FALSE)
 > x[x.idx]
  [1] "1,5001"      "1,10001"     "5001,10001"  "1,15001"     "5001,15001"
  [6] "10001,15001" "1,20001"     "5001,20001"  "10001,20001" "15001,20001"
[11] "1,25001"     "5001,25001"  "10001,25001" "15001,25001" "20001,25001"

 > x.idx2 <- upper.tri(x, diag=TRUE)
 > x[x.idx2]
  [1] "1,1"         "1,5001"      "5001,5001"   "1,10001"     "5001,10001"
  [6] "10001,10001" "1,15001"     "5001,15001"  "10001,15001" "15001,15001"
[11] "1,20001"     "5001,20001"  "10001,20001" "15001,20001" "20001,20001"
[16] "1,25001"     "5001,25001"  "10001,25001" "15001,25001" "20001,25001"
[21] "25001,25001"
 >

I hope this helps.  Thank you, David, for your good lesson.

Chel Hee Lee, PhD.
Biostatistian and Manager
Clinical Research Support Unit
College of Medicine
University of Saskatchewan

On 12/18/2014 9:16 AM, David L Carlson wrote:

> Depending on what you want, you probably want to start with expand.grid():
>
> # All combinations of test with test
>> pairs1 <- expand.grid(test, test)
>> nrow(pairs1)
> [1] 36
> # Exclude cases that differ only in the order of the values
> # E.g. (1, 5001), but not (5001, 1), also (1, 1), etc are included
>> pairs2 <- pairs1[pairs1[,1] <= pairs1[,2],]
>> nrow(pairs2)
> [1] 21
> # Same as pairs2 but (1, 1), etc are not included
>> pairs3 <- pairs1[pairs1[,1] < pairs1[,2],]
>> nrow(pairs3)
> [1] 15
>
> -------------------------------------
> David L Carlson
> Department of Anthropology
> Texas A&M University
> College Station, TX 77840-4352
>
> -----Original Message-----
> From: R-help [mailto:[hidden email]] On Behalf Of Sarah Goslee
> Sent: Thursday, December 18, 2014 9:06 AM
> To: Alaios
> Cc: [hidden email]
> Subject: Re: [R] combinations between two vectors
>
> I can't quite tell what you want: your example output is either
> unclear to me or mangled by posting in HTML (please don't).
>
> Is
> expand.grid(test, test)
> what you want, or partway to what you want?
>
>
> Sarah
>
> On Thu, Dec 18, 2014 at 9:56 AM, Alaios via R-help <[hidden email]> wrote:
>> Hi all,I am looking for a function that would give me all the combinations between two vectors.Lets take as example the
>>
>> test<-seq(1,30000,by=5000)
>> Browse[2]> test
>> [1]     1  5001 10001 15001 20001 25001
>> I want all the combinations between two times the test... I think this is  called permutation so a function that could do permutation(test,test)and produce the following
>> 1,11,50011,100011,15001....
>> 3,13,5001...25001,20001,25001,25001
>> is there such a function ?
>> RegardsAlex
>>
>>
>>          [[alternative HTML version deleted]]
>>
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Chel Hee Lee
Reply | Threaded
Open this post in threaded view
|

Re: combinations between two vectors

Hemant Rupani
This post has NOT been accepted by the mailing list yet.
In reply to this post by R help mailing list-2
Hi I'm new, sorry that I'm questioning on your question.
What is the use of Browse[2]> test for this
It shows me ,"Error: object  'Browse' not found
Regards,
HR