Rainer M Krug
2011-Mar-11 10:53 UTC
[R] How to get all combinations between two character vectors?
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi I know there is a function - I have used it before - but I always forget what it is called... I need the combination of two character vectors, i.e: x <- c("a", "b") y <- c("x", "y") z <- THEFUNCTION(x, y) z == c("ax", "ay", "bx", "by") I promise I will write the name of the function on my desk! Thanks, Rainer - -- Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, UCT), Dipl. Phys. (Germany) Centre of Excellence for Invasion Biology Natural Sciences Building Office Suite 2039 Stellenbosch University Main Campus, Merriman Avenue Stellenbosch South Africa Tel: +33 - (0)9 53 10 27 44 Cell: +27 - (0)8 39 47 90 42 Fax (SA): +27 - (0)8 65 16 27 82 Fax (D) : +49 - (0)3 21 21 25 22 44 Fax (FR): +33 - (0)9 58 10 27 44 email: Rainer at krugs.de Skype: RMkrug -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAk15/yMACgkQoYgNqgF2egq82wCghEyb/0Hk+4zsC4Z60ftIyHDp FTIAniqqbmpkvqnglOM/FlCUMwbGfgbe =qHes -----END PGP SIGNATURE-----
Dennis Murphy
2011-Mar-11 11:22 UTC
[R] How to get all combinations between two character vectors?
Hi: This is probably not quite what you had in mind, but both work for your example - I think the second is probably easier. x <- c("a", "b") y <- c("x", "y") as.vector(outer(x, y, function(x, y) paste(x, y, sep = ''))) [1] "ax" "bx" "ay" "by" apply(expand.grid(x, y), 1, paste, collapse = '') [1] "ax" "bx" "ay" "by" HTH, Dennis On Fri, Mar 11, 2011 at 2:53 AM, Rainer M Krug <r.m.krug@gmail.com> wrote:> -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Hi > > I know there is a function - I have used it before - but I always forget > what it is called... > > I need the combination of two character vectors, i.e: > > x <- c("a", "b") > y <- c("x", "y") > > z <- THEFUNCTION(x, y) > > z == c("ax", "ay", "bx", "by") > > I promise I will write the name of the function on my desk! > > Thanks, > > Rainer > > - -- > Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation > Biology, UCT), Dipl. Phys. (Germany) > > Centre of Excellence for Invasion Biology > Natural Sciences Building > Office Suite 2039 > Stellenbosch University > Main Campus, Merriman Avenue > Stellenbosch > South Africa > > Tel: +33 - (0)9 53 10 27 44 > Cell: +27 - (0)8 39 47 90 42 > Fax (SA): +27 - (0)8 65 16 27 82 > Fax (D) : +49 - (0)3 21 21 25 22 44 > Fax (FR): +33 - (0)9 58 10 27 44 > email: Rainer@krugs.de > > Skype: RMkrug > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.10 (GNU/Linux) > Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ > > iEYEARECAAYFAk15/yMACgkQoYgNqgF2egq82wCghEyb/0Hk+4zsC4Z60ftIyHDp > FTIAniqqbmpkvqnglOM/FlCUMwbGfgbe > =qHes > -----END PGP SIGNATURE----- > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
Eik Vettorazzi
2011-Mar-11 11:44 UTC
[R] How to get all combinations between two character vectors?
Hi Rainer, I don't know a function for literally substituting "THEFUNCTION", but x <- c("a", "b") y <- c("x", "y") sort(levels(interaction(x,y,sep=""))) or as.vector(t(outer(x,y,paste,sep=""))) will work. "sort" and "t" respectively here are used to produce the desired order. hth. Am 11.03.2011 11:53, schrieb Rainer M Krug:> Hi > > I know there is a function - I have used it before - but I always forget > what it is called... > > I need the combination of two character vectors, i.e: > > x <- c("a", "b") > y <- c("x", "y") > > z <- THEFUNCTION(x, y) > > z == c("ax", "ay", "bx", "by") > > I promise I will write the name of the function on my desk! > > Thanks, > > Rainer >______________________________________________ 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. -- Eik Vettorazzi Institut f?r Medizinische Biometrie und Epidemiologie Universit?tsklinikum Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/7410-58243 F ++49/40/7410-57790