创建逗号分隔的向量 [英] Creating a comma separated vector

查看:40
本文介绍了创建逗号分隔的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数字向量,一个,我想把它变成一个字符向量,其中每个元素用逗号分隔.

I have a numeric vector, one, which I'm trying to turn into a character vector where each element is separated by commas.

> one = c(1:5)
> paste(as.character(one), collapse=", ")
[1] "1, 2, 3, 4, 5"
> paste(as.character(one), sep="' '", collapse=", ")
[1] "1, 2, 3, 4, 5"

但是,我希望输出看起来像:

However, I want the output to look like:

"1", "2", "3", "4", "5" 

我是否缺少粘贴功能中的某些参数?帮助!?

Am I missing some parameter from the paste function? Help!?

推荐答案

shQuote 可能是最好的方法.具体来说,这会让你得到你想要的输出:

shQuote is probably the best way to do this. Specifically, this gets you the output you want:

cat(paste(shQuote(one, type="cmd"), collapse=", "))

如果单引号没问题,可以使用:

If single quotes are fine, you can use:

paste(shQuote(one), collapse=", ")

type="cmd" 实际上提供了转义引号,这对大多数上下文实际上很有用,但是如果你真的想用非转义引号在某处显示它,cat提供.

type="cmd" actually provides escaped quotes, which is what's actually useful for most contexts, but if you really want to display it somewhere with unescaped quotes, cat provides that.

这篇关于创建逗号分隔的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆