Code Comments

Code Comments

Tips and short tutorials on various programming technologies

Code Comments RSS Feed
 
 
 
 

How to Do a String Join in R

Let’s say you have a list of values in a vector. And you want to be able to convert that into a delimited string. In some languages (such as Python), you can do this easily with the join method. But how would you do this in R?

This can be done with the paste function. The following code example shows how you would convert a simple vector to a comma-separated string.

x = c(1,2,3)
paste(x, collapse=",")

3 Responses to “How to Do a String Join in R”

  1. 1
    Franc Brglez:

    paste(x, collapse=”,”) is very useful. Would you have a solution for the following

    > binNum = “1001110″
    > foo = noquote(strsplit(binNum, NULL[[1]]))
    > foo
    [[1]]
    [1] 1 0 0 1 1 1 0
    > foo[4]
    [[1]]
    NULL

    How would you create, from binNum, a “vector” binSeq = c(1,0,0,1,1,1,0) so that
    > binSeq[4]
    [1] 1
    > binSeq[7]
    [1] 0
    >
    MANY THANKS — franc

  2. 2
    admin:

    Hi Franc,

    Sorry for the delay. Would the following work for you?

    > bar = foo[[1]]
    > bar[4]
    [1] “1″
    > bar[7]
    [1] “0″

  3. 3
    Franc Brglez:

    Thanks Admin,

    Your reply is close but one more step is needed — recognizing whether or not the returned value is an integer, a hint I got from Prof. Brian Ripley as per
    http://markmail.org/search/list:r-project?q=split+string

    In the meantime, I also created and documented two functions,
    string2vector and vector2string
    that take my R-programming to a level where I am more comfortable. Here they are:

    string2vector = function(string=”ch-2 \t sec-7\tex-5″, SS=”\t”, type=”char”)
    # This procedure splits a string and assigns substrings to an R-vector.
    # The split is controlled by the string separator SS (default value: SS=”\t”).
    # Here we convert a binary string into a binary vector:
    # let binS = “10011″
    # then binV = string2vector(binS, SS=”", type=”int”)
    # Here we convert a string into a vector of substrings:
    # let strS = “I am done”
    # then vecS = string2vector(strS, SS=” “, type=”char”)
    #
    # LIMITATION: The function interprets all substrings either as of type
    # “int” or “char”. A function that interprets the type of each
    # substring dynamically may one day be written by an R-guru.
    #
    # Franc Brglez, Wed Dec 9 14:19:16 EST 2009
    {
    qlist = strsplit(string, SS) ; qvector = qlist[[1]]
    n = length(qvector) ; xvector = NULL
    for (i in 1:n) {
    if (type == “int”) {
    tmp = as.integer(qvector[i])
    } else {
    tmp = qvector[i]
    }
    xvector = c(xvector, tmp)
    }
    return(xvector)
    } # string2vector

    vector2string = function(vector=c(“ch-2″, “sec-7″, “ex-5″), SS=”_”, type=”char”)
    # This procedure converts values from a vector to a concatenation of substrings
    # separated by user-specified string separator SS (default value: SS=”_”).
    # Each substring represents a vector component value, either as a numerical
    # value or as an alphanumeric string.
    # Here we convert a binary vector to a binary string representing an integer:
    # let binV = c(1,0,0,1)
    # then strS = vector2string(binV, type=”int”)
    # Here we convert a binary vector to string representing a binary sequence:
    # let binV = c(1,0,0,1)
    # then seqS = vector2string(binV, SS=” “, type=”char”)
    # Here we convert a vector of substrings to colon-separated string:
    # let subsV = c(“I”, “am”, “done”)
    # then strS = vector2string(subsV, SS=”:”, type=”char”)
    #
    # LIMITATION: The function interprets all substrings in the vector either as of
    # type “int” or “char”. A function that interprets the type of each
    # substring dynamically may one day be written by an R-guru.
    #
    # Franc Brglez, Wed Dec 9 15:43:59 EST 2009
    {
    if (type == “int”) {
    string = paste(strsplit(paste(vector), ” “), collapse=”")
    } else {
    n = length(vector) ; nm1 = n-1 ; string = “”
    for (i in 1:nm1) {
    tmp = noquote(vector[i])
    string = paste(string, tmp, SS, sep=”")
    }
    tmp = noquote(vector[n])
    string = paste(string, tmp, sep=”")
    }
    return(string)
    } # vector2string

Leave a Reply