当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


R stringr str_c 将多个字符串连接成一个字符串


str_c() 将多个字符向量合并为一个字符向量。它与 paste0() 非常相似,但使用 tidyverse 回收和 NA 规则。

理解 str_c() 工作原理的一种方法是绘制一个二维字符串矩阵,其中每个参数形成一列。 sep 插入到每列之间,然后每行组合在一起形成单个字符串。如果设置了 collapse,则会将其插入到每行之间,然后再次将结果合并,这次合并为单个字符串。

用法

str_c(..., sep = "", collapse = NULL)

参数

...

一个或多个字符向量。

NULL 被删除;标量输入(长度为 1 的向量)被回收到向量输入的公共长度。

与大多数其他 R 函数一样,缺失值是"infectious":每当缺失值与另一个字符串组合时,结果将始终缺失。使用dplyr::coalesce()str_replace_na() 转换为所需的值。

sep

要在输入向量之间插入的字符串。

collapse

用于将输出组合成单个字符串的可选字符串。如果您需要这种行为,通常最好使用str_flatten()

如果collapse = NULL(默认)长度等于最长输入的字符向量。如果collapse是一个字符串,则长度为1的字符向量。

例子

str_c("Letter: ", letters)
#>  [1] "Letter: a" "Letter: b" "Letter: c" "Letter: d" "Letter: e"
#>  [6] "Letter: f" "Letter: g" "Letter: h" "Letter: i" "Letter: j"
#> [11] "Letter: k" "Letter: l" "Letter: m" "Letter: n" "Letter: o"
#> [16] "Letter: p" "Letter: q" "Letter: r" "Letter: s" "Letter: t"
#> [21] "Letter: u" "Letter: v" "Letter: w" "Letter: x" "Letter: y"
#> [26] "Letter: z"
str_c("Letter", letters, sep = ": ")
#>  [1] "Letter: a" "Letter: b" "Letter: c" "Letter: d" "Letter: e"
#>  [6] "Letter: f" "Letter: g" "Letter: h" "Letter: i" "Letter: j"
#> [11] "Letter: k" "Letter: l" "Letter: m" "Letter: n" "Letter: o"
#> [16] "Letter: p" "Letter: q" "Letter: r" "Letter: s" "Letter: t"
#> [21] "Letter: u" "Letter: v" "Letter: w" "Letter: x" "Letter: y"
#> [26] "Letter: z"
str_c(letters, " is for", "...")
#>  [1] "a is for..." "b is for..." "c is for..." "d is for..." "e is for..."
#>  [6] "f is for..." "g is for..." "h is for..." "i is for..." "j is for..."
#> [11] "k is for..." "l is for..." "m is for..." "n is for..." "o is for..."
#> [16] "p is for..." "q is for..." "r is for..." "s is for..." "t is for..."
#> [21] "u is for..." "v is for..." "w is for..." "x is for..." "y is for..."
#> [26] "z is for..."
str_c(letters[-26], " comes before ", letters[-1])
#>  [1] "a comes before b" "b comes before c" "c comes before d"
#>  [4] "d comes before e" "e comes before f" "f comes before g"
#>  [7] "g comes before h" "h comes before i" "i comes before j"
#> [10] "j comes before k" "k comes before l" "l comes before m"
#> [13] "m comes before n" "n comes before o" "o comes before p"
#> [16] "p comes before q" "q comes before r" "r comes before s"
#> [19] "s comes before t" "t comes before u" "u comes before v"
#> [22] "v comes before w" "w comes before x" "x comes before y"
#> [25] "y comes before z"

str_c(letters, collapse = "")
#> [1] "abcdefghijklmnopqrstuvwxyz"
str_c(letters, collapse = ", ")
#> [1] "a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z"

# Differences from paste() ----------------------
# Missing inputs give missing outputs
str_c(c("a", NA, "b"), "-d")
#> [1] "a-d" NA    "b-d"
paste0(c("a", NA, "b"), "-d")
#> [1] "a-d"  "NA-d" "b-d" 
# Use str_replace_NA to display literal NAs:
str_c(str_replace_na(c("a", NA, "b")), "-d")
#> [1] "a-d"  "NA-d" "b-d" 

# Uses tidyverse recycling rules
if (FALSE) str_c(1:2, 1:3) # errors
paste0(1:2, 1:3)
#> [1] "11" "22" "13"

str_c("x", character())
#> character(0)
paste0("x", character())
#> [1] "x"
源代码:R/c.R

相关用法


注:本文由纯净天空筛选整理自Hadley Wickham等大神的英文原创作品 Join multiple strings into one string。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。