我想将 c('1','2', 'text') 转换为只有一个元素 c('1','2', 'text') 的字符向量。

我试过这个:

> quote(c('1','2', 'text'))
c("1", "2", "text")


> class(quote(c('1','2', 'text')))
[1] "call"

和这个:
> toString(quote(c('1','2', 'text')))
[1] "c, 1, 2, text"

它删除了所有标点符号(虽然我想保留完全相同的字符串)。

最佳答案

deparse 用于将表达式转换为字符串。

deparse(c('1','2', 'text'))
#[1] "c(\"1\", \"2\", \"text\")"

cat(deparse(c('1','2', 'text')))
#c("1", "2", "text")

gsub("\"", "'", deparse(c('1','2', 'text')))
#[1] "c('1', '2', 'text')"

deparse(quote(c('1','2', 'text')))
#[1] "c(\"1\", \"2\", \"text\")"

也看看substitute
deparse(substitute(c(1L, 2L)))
#[1] "c(1L, 2L)"

关于r - 如何将R代码转换为字符串?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45942315/

10-12 16:40
查看更多