收集字符串,用逗号分隔

时间:2016-05-21 15:34:32

标签: r paste gsub

之前我使用过paste和gsub。我有几年的R经验。但是现在我不打算简单地将字符串合并为一个。我需要将字符串保持分离,然后用逗号分隔:

one <- letters[1:9]
assigned.object <- somefunction(one,sep=",")
assigned.object
[1] "a","b","c","d","e","f","g","h","i"

还搜索了网站this thread and its duplicate don't answer my question。找不到此问题的任何其他答案。有人知道R的方式吗?

解答:

副本回答了问题。但是,如果您需要将对象分配给您直接想要的对象:

cat(paste("\"",as.character(one),"\"",collapse=", ",sep=""))
[1] "a","b","c","d","e","f","g","h","i"
assigned.object <- cat(paste("\"",as.character(one),"\"",collapse=", ",sep=""))
assigned.object
NULL

原因是翻译中出现了一些错误&#39;:R可以打印我想要的对象,但不能像这样存储它。我想要的对象需要像这样存储:

assigned.object <- paste("\"",as.character(one),"\"",collapse=", ",sep="")
assigned.object
[1] "\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\", \"i\""

如果将其保存在txt文件

中,则如下所示
"a","b","c","d","e","f","g","h","i"

基本上R需要插入&#39; \&#39;区分字符中的引号和定义字符开头和结尾的引号。

0 个答案:

没有答案