gpt4 book ai didi

r - 将值向量转换为逗号分隔的向量,每个向量都带有引号

转载 作者:行者123 更新时间:2023-12-03 23:41:21 24 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Creating a comma separated vector

(6 个回答)


4 个月前关闭。




我想得到结果,使其显示为:

"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" "\""

关于r - 将值向量转换为逗号分隔的向量,每个向量都带有引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39539011/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com