1. paste {base}
語(yǔ)法:
paste (..., sep = " ", collapse = NULL)
paste0(..., collapse = NULL)
兩個(gè)參數(shù):sep
字符串內(nèi)的拼接符战得;collapse
字符串間的拼接符澎语。paste
與paste0
的區(qū)別:paste0
參數(shù)sep
默認(rèn)為空字符幸缕,paste
的參數(shù)sep
默認(rèn)為空格。
示例:
paste("1st", "2nd", "3rd", collapse = ", ")
[1] "1st 2nd 3rd"
paste("1st", "2nd", "3rd", sep = ", ")
[1] "1st, 2nd, 3rd"
paste(c("a", "b", "c"),c(1:3) , sep = ": ", collapse = "; ")
[1] "a: 1; b: 2; c: 3"
2. str_c {stringr}
語(yǔ)法:
str_c(..., sep = "", collapse = NULL)
參數(shù):與paste一致,但對(duì)NA的處理方法不一致久信。
示例:
library("stringr")
str_c("Letter: ", letters)
[1] "Letter: a" "Letter: b" "Letter: c" "Letter: d" "Letter: e" "Letter: f" "Letter: g" "Letter: h"
[9] "Letter: i" "Letter: j" "Letter: k" "Letter: l" "Letter: m" "Letter: n" "Letter: o" "Letter: p"
[17] "Letter: q" "Letter: r" "Letter: s" "Letter: t" "Letter: u" "Letter: v" "Letter: w" "Letter: x"
[25] "Letter: y" "Letter: z"
#str_c在對(duì)含NA的字符拼接后冷冗,結(jié)果仍然為NA
str_c(c("a", NA, "b"), "-d")
[1] "a-d" NA "b-d"
#paste在對(duì)含NA的字符拼接后,NA被當(dāng)作為“NA”字符串處理
paste(c("a", NA, "b"), "-d")
[1] "a -d" "NA -d" "b -d"