0

嗨,我想将一个字符串向量更改为一个字符串,该字符串采用 Python 元组格式。

输入:

a <- c('stack', 'overflow', 'kicks', 'ass')

预期输出:

"('stack', 'overflow', 'kicks', 'ass')"

什么是易于实施的解决方案?

这就是我所做的,我希望应该有一个更简单的解决方案:

> b <- a[1]
> for(word in a[-1]){ b <- paste(b, word, sep="','") }
> b
[1] "stack','overflow','kick','ass"
> b <- paste("('", b, "')",sep="") 
> b
[1] "('stack','overflow','kick','ass')"
4

1 回答 1

3
> paste0("(", paste(sQuote(a), collapse = ","), ")")
[1] "(‘stack’,‘overflow’,‘kicks’,‘ass’)"
> options(useFancyQuotes = FALSE)
> paste0("(", paste(sQuote(a), collapse = ","), ")")
[1] "('stack','overflow','kicks','ass')"
>  substring(capture.output(dput(a)), 2)
[1] "(\"stack\", \"overflow\", \"kicks\", \"ass\")"
于 2013-10-28T20:07:43.010 回答