[R] String split and concatenation

Gabor Grothendieck ggrothendieck at gmail.com
Thu Sep 30 16:53:17 CEST 2010


On Wed, Sep 29, 2010 at 4:15 AM, Steven Kang <stochastickang at gmail.com> wrote:
> x <- rep(letters[1:3], 2)
>
> Are there any ways to transform & assign the above as the one shown below
> to an object? (in exact format; i.e length of 1 & class of character),
> i.e
>>x
> "('a', 'b', 'c', 'a', 'b', 'c')"
>
> Highly appreciate for any advice.
>

Here are a few variations.  They all use paste (or the paste0 wrapper)
and toString.  The last one uses sQuote to do the quoting, turning off
fancy quotes so that ordinary single quotes are used.

# 1
paste("(", toString(paste("'", x, "'", sep = "")), ")", sep = "")

#2
library(gsubfn) # paste0
paste0("(", toString(paste0("'", x, "'")), ")")

# 3
P <- function(x, pre = "'", post = pre) paste(pre, x, post, sep = "")
P(toString(P(x)), "(", ")")

# 4
old <- options(useFancyQuotes = FALSE)
paste("(", toString(sQuote(x)), ")", sep = "")
options(old)

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list