如何在paste0结果的开头或结尾添加字符?

时间:2019-06-13 14:58:56

标签: r string-concatenation

我正在尝试创建一个看起来像这样的字符串 "c,1,2,3,4,5,6,7,8"

我能够通过执行以下操作来获取字符串的数字部分:

paste0(1:200, collapse = ",")

我如何在paste0的结果开头添加“ c”?另外,我如何加入结果的“,c”结尾?

3 个答案:

答案 0 :(得分:6)

我们可以用paste

换行
paste0("c(", paste0(1:200, collapse = ","), ")")

或与sprintf

sprintf("c(%s)", paste0(1:200, collapse=","))

如果我们只需要'c',则使用

sprintf("c,%s", paste0(1:200, collapse = ","))

答案 1 :(得分:4)

您只想要字符串c,而不是函数吧?

def add_to_axes(ax, data, param, context_id = None):
    data.plot(x = "Time_Elapsed", y = param, label = context_id, ax = ax)
    outlier = data["iso_forest"] == -1
    data[~outlier].plot("Time_Elapsed",param, style=".", color='green', ax=ax)
    data[outlier].plot("Time_Elapsed",param, style=".", color='red', ax=ax)

答案 2 :(得分:2)

创建由c1:8组成的向量,然后使用toString

toString(c("c", 1:8))
## [1] "c, 1, 2, 3, 4, 5, 6, 7, 8"