str_c()
將多個字符向量合並為一個字符向量。它與 paste0()
非常相似,但使用 tidyverse 回收和 NA
規則。
理解 str_c()
工作原理的一種方法是繪製一個二維字符串矩陣,其中每個參數形成一列。 sep
插入到每列之間,然後每行組合在一起形成單個字符串。如果設置了 collapse
,則會將其插入到每行之間,然後再次將結果合並,這次合並為單個字符串。
參數
- ...
-
一個或多個字符向量。
NULL
被刪除;標量輸入(長度為 1 的向量)被回收到向量輸入的公共長度。與大多數其他 R 函數一樣,缺失值是"infectious":每當缺失值與另一個字符串組合時,結果將始終缺失。使用
dplyr::coalesce()
或str_replace_na()
轉換為所需的值。 - sep
-
要在輸入向量之間插入的字符串。
- collapse
-
用於將輸出組合成單個字符串的可選字符串。如果您需要這種行為,通常最好使用
str_flatten()
。
例子
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 stringr str_count 計算匹配次數
- R stringr str_conv 指定字符串的編碼
- R stringr str_which 查找匹配索引
- R stringr str_extract 提取完整的匹配項
- R stringr str_subset 查找匹配元素
- R stringr str_escape 轉義正則表達式元字符
- R stringr str_trim 刪除空格
- R stringr str_sub 使用子字符串的位置獲取和設置子字符串
- R stringr str_replace_na 把NA變成“NA”
- R stringr str_trunc 將字符串截斷至最大寬度
- R stringr str_match 從匹配中提取組件(捕獲組)
- R stringr str_like 以與 SQL 的 LIKE 運算符相同的方式檢測模式
- R stringr str_length 計算長度/寬度
- R stringr str_detect 檢測是否存在匹配
- R stringr str_split 將字符串分成幾段
- R stringr str_unique 刪除重複的字符串
- R stringr str_remove 刪除匹配的模式
- R stringr str_pad 將字符串填充到最小寬度
- R stringr str_equal 判斷兩個字符串是否相等
- R stringr str_view 查看字符串和匹配項
- R stringr str_glue 用膠水插補
- R stringr str_order 對字符向量進行排序、排名或排序
- R stringr str_starts 檢測開始/結束時是否存在匹配
- R stringr str_wrap 將單詞包裝成格式良好的段落
- R stringr str_dup 複製字符串
注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Join multiple strings into one string。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。