将值向量转换为逗号分隔的向量,并在每个向量周围引用

run*_*rds 1 string r paste

我想得到结果,以便显示为:

"6", "4", "8"

或者逗号

my_vector = base::unique(mtcars$cyl)
my_vector_quoted =paste(my_vector, sep=" ' ")
Run Code Online (Sandbox Code Playgroud)

现在我如何获得介于两者之间的逗号?我尝试用sep ='重复这个,但这不起作用.

有什么方法吗?

G. *_*eck 6

假设输入有以下几种可能性x:

toString(shQuote(x, type = "cmd"))

options(useFancyQuotes = FALSE)
toString(dQuote(x))

library(withr)
with_options(c(useFancyQuotes = FALSE), toString(dQuote(x)))

toString(sprintf('"%d"', x))

paste(paste0('"', x, '"'), collapse = ", ")
Run Code Online (Sandbox Code Playgroud)

例如,

x <- c(6, 4, 8)
xs <- toString(shQuote(x, type = "cmd"))
Run Code Online (Sandbox Code Playgroud)

赠送:

> cat(xs, "\n")
"6", "4", "8" 

> strsplit(xs, "")[[1]] # shows that xs contains 13 characters
[1] "\"" "6"  "\"" ","  " "  "\"" "4"  "\"" ","  " "  "\"" "8"  "\""
Run Code Online (Sandbox Code Playgroud)