R学习:paste/paste0函数
之前我们介绍过字符串的简单处理:
R学习 从Tidyverse学起,入门R语言(tidyr和stringr)
其中的stringr包可以花式处理字符串,而paste/paste0函数也可以轻易的实现字符串的连接。
函数用法:
paste (..., sep = " ", collapse = NULL)
paste0(..., collapse = NULL)
paste0默认sep="",这是两个函数唯一的区别
paste0(1:12)
paste(1:12)
as.character(1:12)
多个向量一一对应的连接
paste0(1:12, c("st", "nd", "rd", rep("th", 9)))
设置分隔符
paste("a","b","c",sep = "+")
collapse参数可以使元素连接后合并在一起成为一个元素
paste0(1:12, c("st", "nd", "rd", rep("th", 9)))
paste0(1:12, c("st", "nd", "rd", rep("th", 9)),collapse = "+")
再来回顾一下stringr包
str_c连接字符串
library(stringr)
str_c("x", "y","z")
str_c("x", "y", sep =", ")
str_length计算字符串长度
x <- c("why", "video", "cross", "extra", "deal", "authority")
str_length(x)
#> [1] 3 5 5 5 4 9
str_sub取子集
str_sub(x, 1, 2)
#> [1] "wh" "vi" "cr" "ex" "de" "au"
str_sub(x, 1, 3)
#> [1] "why" "vid" "cro" "ext" "dea" "aut"
str_split函数进行拆分
fruits <- c(
"apples and oranges and pears and bananas",
"pineapples and mangos and guavas"
)
str_split(fruits, " and ")
默认返回的是列表
可以使用unlist()
unlist(str_split(fruits, " and "))
使用simplify = TRUE返回矩阵
str_split(fruits, " and ", simplify = TRUE)
使用 n= 可以限制拆分个数
str_split(fruits, " and ", n = 3)
str_split(fruits, " and ", n = 2)
使用str_split_fixed也可以返回矩阵
str_split_fixed(fruits, " and ", 3)
str_split_fixed(fruits, " and ", 4)
str_order(),str_sort()对字符向量排序
## 返回排序后的索引
> str_order(x, decreasing = FALSE, na_last = TRUE, locale = "", ...)
## 返回排序后的实际值
> str_sort(x, decreasing = FALSE, na_last = TRUE, locale = "", ....)
# decreasing:排序方式,默认为升序
# na_last:是否将缺失值置于末尾,默认为TRUE
str_order(letters)
str_sort(letters)
排序法则,默认是locale = "en"
str_order(letters, locale = "en")
str_sort(letters, locale = "en")
str_order(letters, locale = "haw")
str_sort(letters, locale = "haw")
str_replace字符串替换
str_replace(string, pattern, replacement)
str_replace_all(string, pattern, replacement)
#string:需要处理的字符向量
#pattern:指定匹配模
#replacement:指定新的字符串用于替换匹配的模式
#str_replace与str_replace_all的区别在于前者只替换一次匹配的对象,而后者可以替换所有匹配的对象
fruits <- c("one apple", "two pears", "three bananas")
str_replace(fruits, "[aeiou]", "-")
str_replace_all(fruits, "[aeiou]", "-")
str_replace_all(fruits, "[aeiou]", toupper)
str_replace_all(fruits, "b", NA_character_)
str_replace(fruits, "[aeiou]", c("1", "2", "3"))
str_replace(fruits, c("a", "e", "i"), "-")
下回见。