[R] Cartesian Product Of Character Vectors

Rory.WINSTON at rbs.com Rory.WINSTON at rbs.com
Fri Dec 5 16:08:57 CET 2008


Got it...I completely overlooked the collapse argument to paste():

apply(expand.grid(x,y),1,function(x) paste(x,collapse=""))



Rory Winston
RBS Global Banking & Markets
Office: +44 20 7085 4476

-----Original Message-----
From: WINSTON, Rory, GBM
Sent: 05 December 2008 14:30
To: WINSTON, Rory, GBM; dwinsemius at comcast.net
Cc: r-help at r-project.org
Subject: RE: [R] Cartesian Product Of Character Vectors

Sorry, I spoke too soon...

interaction() only works for sequences of equal length. Anyone know a method that works with unequal-length vectors?


Rory Winston
RBS Global Banking & Markets
Office: +44 20 7085 4476

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of WINSTON, Rory, GBM
Sent: 05 December 2008 14:20
To: dwinsemius at comcast.net
Cc: r-help at r-project.org
Subject: Re: [R] Cartesian Product Of Character Vectors

Hi David

Perfect - if I specify sep="", it gives me exactly what I need.

Cheers



Rory Winston
RBS Global Banking & Markets
Office: +44 20 7085 4476

-----Original Message-----
From: David Winsemius [mailto:dwinsemius at comcast.net]
Sent: 05 December 2008 14:16
To: WINSTON, Rory, GBM
Cc: r-help at r-project.org
Subject: Re: [R] Cartesian Product Of Character Vectors

Does this satisfy?

levels(interaction(x,y))
[1] "aaa.1" "bbb.1" "ccc.1" "aaa.2" "bbb.2" "ccc.2" "aaa.3" "bbb.3"
"ccc.3"

--
David Winsemius


On Dec 5, 2008, at 8:12 AM, <Rory.WINSTON at rbs.com> <Rory.WINSTON at rbs.com> wrote:

> Hi all
>
> (I'm sure this question has been asked before, but I cant find it).
>
> If I have two character vectors:
>
>> x <- c("aaa","bbb","ccc")
>> y <- c("1","2","3")
>
> How can I get the cartesian product of the string values?
>
>> expand.grid(x,y)
>
> Gives me a data frame with separate columns...however, I cant seem to
> get *apply to paste the column values together.
>
> Thanks
> Rory
>
> Rory Winston
> RBS Global Banking & Markets
> 280 Bishopsgate, London, EC2M 4RB
> Office: +44 20 7085 4476
>
>
>
> **********************************************************************
> ************* The Royal Bank of Scotland plc. Registered in Scotland
> No 90312.
> Registered Office: 36 St Andrew Square, Edinburgh EH2 2YB.
> Authorised and regulated by the Financial Services Authority
>
> This e-mail message is confidential and for use by the=2...{{dropped:
> 25}}
>
> ______________________________________________
> 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.

______________________________________________
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