Combining characters

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

Combining characters

jeremy
Hi all,

I'm trying to combine exhaustively several character arrays in R like:
x=c("one","two","three")
y=c("yellow","blue","green")
z=c("apple","cheese")

in order to get concatenation of

x[1] y[1] z[1]  ("one yellow apple")
x[1] y[1] z[2] ("one yellow cheese")
x[1] y[2] z[1]("one blue apple")
...
x[length(x)] y[length(y)] z[length(z)]  ("three green cheese")

Anyone has a solution ?
Thank in advance
Reply | Threaded
Open this post in threaded view
|

Re: Combining characters

Rui Barradas
Hello,

If you want to apply the same procedure to all elements of an object, check out the  '*apply' functions.

In this case,

x=c("one","two","three")
y=c("yellow","blue","green")
z=c("apple","cheese")

lapply(x, function(x) paste(x, y))

gives a good picture of what you want to do, just transform it into a function and use the function:

f <- function(x, y) unlist(lapply(x, function(x) paste(x,y)))

f(x, f(y, z))

With more than 3 vectors, you could try a recursive version.
I hope this helps.

Rui Barradas
Reply | Threaded
Open this post in threaded view
|

Re: Combining characters

Michael Weylandt
In reply to this post by jeremy
? expand.grid

Michael

On Wed, Jan 4, 2012 at 10:32 AM, jeremy <[hidden email]> wrote:

> Hi all,
>
> I'm trying to combine exhaustively several character arrays in R like:
> x=c("one","two","three")
> y=c("yellow","blue","green")
> z=c("apple","cheese")
>
> in order to get concatenation of
>
> x[1] y[1] z[1]  ("one yellow apple")
> x[1] y[1] z[2] ("one yellow cheese")
> x[1] y[2] z[1]("one blue apple")
> ...
> x[length(x)] y[length(y)] z[length(z)]  ("three green cheese")
>
> Anyone has a solution ?
> Thank in advance
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Combining-characters-tp4261888p4261888.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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.
Reply | Threaded
Open this post in threaded view
|

Re: Combining characters

andrija djurovic
In reply to this post by jeremy
Hi. You can use expand.grid here

expand.grid(x,y,z)

Andrija

On Wed, Jan 4, 2012 at 5:32 PM, jeremy <[hidden email]> wrote:

> Hi all,
>
> I'm trying to combine exhaustively several character arrays in R like:
> x=c("one","two","three")
> y=c("yellow","blue","green")
> z=c("apple","cheese")
>
> in order to get concatenation of
>
> x[1] y[1] z[1]  ("one yellow apple")
> x[1] y[1] z[2] ("one yellow cheese")
> x[1] y[2] z[1]("one blue apple")
> ...
> x[length(x)] y[length(y)] z[length(z)]  ("three green cheese")
>
> Anyone has a solution ?
> Thank in advance
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Combining-characters-tp4261888p4261888.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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.
Reply | Threaded
Open this post in threaded view
|

Re: Combining characters

Marc Schwartz-3
In reply to this post by jeremy

On Jan 4, 2012, at 10:32 AM, jeremy wrote:

> Hi all,
>
> I'm trying to combine exhaustively several character arrays in R like:
> x=c("one","two","three")
> y=c("yellow","blue","green")
> z=c("apple","cheese")
>
> in order to get concatenation of
>
> x[1] y[1] z[1]  ("one yellow apple")
> x[1] y[1] z[2] ("one yellow cheese")
> x[1] y[2] z[1]("one blue apple")
> ...
> x[length(x)] y[length(y)] z[length(z)]  ("three green cheese")
>
> Anyone has a solution ?
> Thank in advance


See ?expand.grid and ?paste

DF <- expand.grid(x, y, z)

> DF
    Var1   Var2   Var3
1    one yellow  apple
2    two yellow  apple
3  three yellow  apple
4    one   blue  apple
5    two   blue  apple
6  three   blue  apple
7    one  green  apple
8    two  green  apple
9  three  green  apple
10   one yellow cheese
11   two yellow cheese
12 three yellow cheese
13   one   blue cheese
14   two   blue cheese
15 three   blue cheese
16   one  green cheese
17   two  green cheese
18 three  green cheese


> with(DF, paste(Var1, Var2, Var3))
 [1] "one yellow apple"    "two yellow apple"    "three yellow apple"
 [4] "one blue apple"      "two blue apple"      "three blue apple"  
 [7] "one green apple"     "two green apple"     "three green apple"  
[10] "one yellow cheese"   "two yellow cheese"   "three yellow cheese"
[13] "one blue cheese"     "two blue cheese"     "three blue cheese"  
[16] "one green cheese"    "two green cheese"    "three green cheese"

HTH,

Marc Schwartz

______________________________________________
[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: Combining characters

Justin Haynes
In reply to this post by jeremy
apply(expand.grid(x, y, z, stringsAsFactors=F), 1, paste, collapse=' ')



On Wed, Jan 4, 2012 at 8:32 AM, jeremy <[hidden email]> wrote:

> Hi all,
>
> I'm trying to combine exhaustively several character arrays in R like:
> x=c("one","two","three")
> y=c("yellow","blue","green")
> z=c("apple","cheese")
>
> in order to get concatenation of
>
> x[1] y[1] z[1]  ("one yellow apple")
> x[1] y[1] z[2] ("one yellow cheese")
> x[1] y[2] z[1]("one blue apple")
> ...
> x[length(x)] y[length(y)] z[length(z)]  ("three green cheese")
>
> Anyone has a solution ?
> Thank in advance
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Combining-characters-tp4261888p4261888.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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: Combining characters

Joshua Wiley-2
In reply to this post by jeremy
Try expand.grid() to create all the combinations.  Then just collapse
them with paste():

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

Cheers,

Josh

On Wed, Jan 4, 2012 at 8:32 AM, jeremy <[hidden email]> wrote:

> Hi all,
>
> I'm trying to combine exhaustively several character arrays in R like:
> x=c("one","two","three")
> y=c("yellow","blue","green")
> z=c("apple","cheese")
>
> in order to get concatenation of
>
> x[1] y[1] z[1]  ("one yellow apple")
> x[1] y[1] z[2] ("one yellow cheese")
> x[1] y[2] z[1]("one blue apple")
> ...
> x[length(x)] y[length(y)] z[length(z)]  ("three green cheese")
>
> Anyone has a solution ?
> Thank in advance
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Combining-characters-tp4261888p4261888.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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.



--
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/

______________________________________________
[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.