stringr

作者: 养猪场小老板 | 来源:发表于2020-01-16 22:36 被阅读0次

• 1.检测字符串长度 str_length(x)

> library("stringr")
> a="unbelievable"
> str_length(a)
[1] 12

• 2.字符串拆分与组合 str_split( ) /str_c()

> str_split(a,"i")
[[1]]
[1] "unbel"  "evable"
> str_c(a,"idolove")
[1] "unbelievableidolove"

• 3.提取字符串的一部分 str_sub()

> str_sub(a,1,3)#stri_sub(string, from = start, to = end)
[1] "unb

• 4.大小写转换 str_to_upper()/str_to_lower()/str_to_title()

> str_to_upper(a)
[1] "UNBELIEVABLE"
> str_to_title(a)
[1] "Unbelievable"

• 5.字符串排序 str_sort()

> x <- c("100a10", "100a5", "2b", "2a")
> str_sort(x)
[1] "100a10" "100a5"  "2a"     "2b"    
> str_sort(x, numeric = TRUE)
[1] "2a"     "2b"     "100a5"  "100a10"

• 6.字符检测 str_detect(x,“h”) –返回逻辑值

> str_detect(x,"a")
[1]  TRUE  TRUE FALSE  TRUE

• 7.提取匹配到的字符串 str_subset()

> str_subset(x,"a")
[1] "100a10" "100a5"  "2a"  

• 8.字符计数 str_count( )

> str_count(x )
[1] 6 5 2 2

• 9.字符串替换 str_replace()/str_replace_all()

> str_replace(fruits, c("a", "e", "i"), "-")
[1] "one -pple"     "two p-ars"     "three bananas"

相关文章

网友评论

      本文标题:stringr

      本文链接:https://www.haomeiwen.com/subject/jheqzctx.html