1. sci666首页
  2. 实用技巧
  3. 生物信息学

R语言学习:paste/paste0函数讲解

其中的stringr包可以花式处理字符串,而paste/paste0函数也可以轻易的实现字符串的连接。

函数用法:

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

paste0(..., collapse = NULL)

paste0默认sep=””,这是两个函数唯一的区别

paste0(1:12)paste(1:12)       as.character(1:12)

R语言学习:paste/paste0函数讲解

 

多个向量一一对应的连接

paste0(1:12, c("st", "nd", "rd", rep("th", 9)))

R语言学习:paste/paste0函数讲解

 

设置分隔符

paste("a","b","c",sep = "+")

R语言学习:paste/paste0函数讲解

 

 

collapse参数可以使元素连接后合并在一起成为一个元素

paste0(1:12, c("st", "nd", "rd", rep("th", 9)))paste0(1:12, c("st", "nd", "rd", rep("th", 9)),collapse = "+")

R语言学习:paste/paste0函数讲解

再来回顾一下stringr包

 

str_c连接字符串

library(stringr)str_c("x", "y","z") str_c("x", "y", sep =", ")

R语言学习:paste/paste0函数讲解

 

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 ")

R语言学习:paste/paste0函数讲解

默认返回的是列表

可以使用unlist()

unlist(str_split(fruits, " and "))

R语言学习:paste/paste0函数讲解

 

使用simplify = TRUE返回矩阵

str_split(fruits, " and ", simplify = TRUE)

R语言学习:paste/paste0函数讲解

使用 n= 可以限制拆分个数

str_split(fruits, " and ", n = 3)str_split(fruits, " and ", n = 2)

R语言学习:paste/paste0函数讲解

 

使用str_split_fixed也可以返回矩阵

str_split_fixed(fruits, " and ", 3)str_split_fixed(fruits, " and ", 4)

R语言学习:paste/paste0函数讲解

 

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)

R语言学习:paste/paste0函数讲解

 

排序法则,默认是locale = “en”

str_order(letters, locale = "en")str_sort(letters, locale = "en")str_order(letters, locale = "haw")str_sort(letters, locale = "haw")

R语言学习:paste/paste0函数讲解

 

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_)

R语言学习:paste/paste0函数讲解

str_replace(fruits, "[aeiou]", c("1", "2", "3"))str_replace(fruits, c("a", "e", "i"), "-")

R语言学习:paste/paste0函数讲解

下回见。

这些可能会帮助到你: 问答社区 | 共享百度SVIP | 留言建议

欢迎入群交流:生信分析群: 732179952 · Meta分析群: 797345521

发表评论

登录后才能评论