【发布时间】:2016-09-16 19:39:07
【问题描述】:
我想得到结果,使其显示为:
"6","4","8"
或逗号
my_vector = base::unique(mtcars$cyl)
my_vector_quoted =paste(my_vector, sep=" ' ")
现在我如何得到中间的逗号?我尝试使用 sep = ' 重复此操作,但这不起作用。
有什么办法吗?
【问题讨论】:
我想得到结果,使其显示为:
"6","4","8"
或逗号
my_vector = base::unique(mtcars$cyl)
my_vector_quoted =paste(my_vector, sep=" ' ")
现在我如何得到中间的逗号?我尝试使用 sep = ' 重复此操作,但这不起作用。
有什么办法吗?
【问题讨论】:
假设输入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 = ", ")
例如,
x <- c(6, 4, 8)
xs <- toString(shQuote(x, type = "cmd"))
给予:
> cat(xs, "\n")
"6", "4", "8"
> strsplit(xs, "")[[1]] # shows that xs contains 13 characters
[1] "\"" "6" "\"" "," " " "\"" "4" "\"" "," " " "\"" "8" "\""
【讨论】:
cat 中以打印所需的输出(即“6”、“4”、“8”)
你想要这个吗?
my_vector_quoted =paste(my_vector, collapse=",")
#"6,4,8"
【讨论】: