Development

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • How to merge two strings

    5 answers - 354 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    Hi r-help,
    A very simple question for which I have not been able to find an answer
    in the docs:
    How can I merge two character strings?
    I am searching for the equivalent of the (non-existing) stringmerge
    function illustrated below:
    s1 <- "R-"
    s2 <- "project"
    stringmerge(s1,s2)
    [1] "R-project"
    Claus
  • No.1 | | 1067 bytes | |

    look at ?paste(), e.g.,

    s1 <- "R-"
    s2 <- "project"
    paste(s1, s2, sep = "")

    Best,
    Dimitris

    Dimitris Rizopoulos
    Ph.D. Student
    Biostatistical Centre
    School of Public Health
    Catholic University of Leuven

    Address: Kapucijnenvoer 35, Leuven, Belgium
    Tel: +32/16/336899
    Fax: +32/16/337015
    Web:
    ~m0390867/dimitris.htm

    Message
    From: "Claus Hindsgaul" <claush (AT) mek (DOT) dtu.dk>
    To: <r-help (AT) stat (DOT) math.ethz.ch>
    Sent: Tuesday, August 16, 2005 1:01 PM
    Subject: [R] How to merge two strings

    Hi r-help,

    A very simple question for which I have not been able to find an
    answer
    in the docs:

    How can I merge two character strings?

    I am searching for the equivalent of the (non-existing) stringmerge
    function illustrated below:
    >
    >s1 <- "R-"
    >s2 <- "project"
    >stringmerge(s1,s2)

    [1] "R-project"
    >>

    >

    Claus
  • No.2 | | 806 bytes | |

    Claus Hindsgaul wrote:

    >Hi r-help,
    >
    >A very simple question for which I have not been able to find an answer
    >in the docs:
    >
    >How can I merge two character strings?
    >
    >I am searching for the equivalent of the (non-existing) stringmerge
    >function illustrated below:
    >


    >
    >>s1 <- "R-"
    >>s2 <- "project"
    >>stringmerge(s1,s2)

    >
    >>

    >[1] "R-project"


    >
    >
    >Claus
    >


    paste(s1, s2, sep = "")

    HTH,
    Tobias

    R-help (AT) stat (DOT) math.ethz.ch mailing list

    PLEASE do read the posting guide!
  • No.3 | | 1214 bytes | |

    Thank you all!
    Paste() was just the function I needed to know!

    Claus

    tir, 16 08 2005 kl. 13:06 +0200, skrev Dimitris Rizopoulos:
    look at ?paste(), e.g.,

    s1 <- "R-"
    s2 <- "project"
    paste(s1, s2, sep = "")

    Best,
    Dimitris

    Dimitris Rizopoulos
    Ph.D. Student
    Biostatistical Centre
    School of Public Health
    Catholic University of Leuven

    Address: Kapucijnenvoer 35, Leuven, Belgium
    Tel: +32/16/336899
    Fax: +32/16/337015
    Web:
    ~m0390867/dimitris.htm

    Message
    From: "Claus Hindsgaul" <claush (AT) mek (DOT) dtu.dk>
    To: <r-help (AT) stat (DOT) math.ethz.ch>
    Sent: Tuesday, August 16, 2005 1:01 PM
    Subject: [R] How to merge two strings

    Hi r-help,

    A very simple question for which I have not been able to find an
    answer
    in the docs:

    How can I merge two character strings?

    I am searching for the equivalent of the (non-existing) stringmerge
    function illustrated below:
    >
    >s1 <- "R-"
    >s2 <- "project"
    >stringmerge(s1,s2)

    [1] "R-project"
    >>

    >

    Claus
  • No.4 | | 1004 bytes | |

    Claus Hindsgaul wrote:
    Thank you all!
    Paste() was just the function I needed to know!

    sprintf:

    s1 <- "R-"
    s2 <- "project"
    sprintf("%s%s",s1,s2)
    [1] "R-project"

    It seems to be much faster:

    unix.time(for(i in 1:100000){junk=sprintf("%s%s",s1,s2)})
    [1] 1.12 0.00 1.12 0.00 0.00
    unix.time(for(i in 1:100000){junk=paste(s1,s2,sep='')})
    [1] 5.90 0.01 5.92 0.00 0.00

    Not that I imagine string concatenation will ever be a bottleneck
    worth optimising but there it is. A well-constructed sprintf() call may
    be more readable than a pastey mess though, with all its fiddly commas
    and quotes - contrived example:

    sprintf("%s://%s%s/%s",scheme,host,dir,file)
    [1] ""

    paste(scheme,'://',host,dir,'/',file,sep='')
    [1] ""

    which do you prefer?

    Barry

    R-help (AT) stat (DOT) math.ethz.ch mailing list

    PLEASE do read the posting guide!
  • No.5 | | 1130 bytes | |

    Tue, 16 Aug 2005, Barry Rowlingson wrote:

    Claus Hindsgaul wrote:
    >Thank you all!
    >Paste() was just the function I needed to know!
    >

    sprintf:

    s1 <- "R-"
    s2 <- "project"
    sprintf("%s%s",s1,s2)
    [1] "R-project"

    It seems to be much faster:

    unix.time(for(i in 1:100000){junk=sprintf("%s%s",s1,s2)})
    [1] 1.12 0.00 1.12 0.00 0.00
    unix.time(for(i in 1:100000){junk=paste(s1,s2,sep='')})
    [1] 5.90 0.01 5.92 0.00 0.00

    Not that I imagine string concatenation will ever be a bottleneck
    worth optimising but there it is. A well-constructed sprintf() call may
    be more readable than a pastey mess though, with all its fiddly commas
    and quotes - contrived example:

    sprintf("%s://%s%s/%s",scheme,host,dir,file)
    [1] ""

    paste(scheme,'://',host,dir,'/',file,sep='')
    [1] ""

    which do you prefer?

    That's actually the reason we have the enhanced sprintf that we do
    nowadays: to enable readable (and translatable) error messages to be
    written via gettextf.

Re: How to merge two strings


max 4000 letters.
Your nickname that display:
In order to stop the spam: 5 + 4 =
QUESTION ON "Development"

EMSDN.COM