install.packages("stringr")
library(stringr)
#1.str_c() 字符串连接函数
str_c(letters[1:6],c("ab","bb"),sep=",")
str_c(letters[1:6],c("ab","bb"),collapse = ",")
#区别:
#第一种:长度为6的向量,先插入分隔符
#第二种:长度为1的向量,先连接
#2.str_count() 字符串匹配个数统计函数
str<-c("abcdedf","aaa","bbb")
str_count(str,"a")
#a在向量str每个元素中出现的次数
#3.str_detect() 字符串匹配函数
str<-c("abcdedf","aaa","bbb")
str_detect(str,"a")
#如果向量中每个元素匹配到就返回TRUE,否则返回FALSE
#4.str_dup() 字符串扩展函数
str_dup(str,3)
str_dup(str,1:3)
#区别:
#第一种:向量str中每个元素重复3次
#第二种:向量str中每个元素分别重复1,2,3次
#5.str_extract()/str_extract_all() 抽取函数
str_extract(str,"a")
str_extract_all(str,"a")
#区别:
#第一种:将字符串中第一个匹配到的字符a取出,若匹配不到,则返回NA
#第二种:将字符串中所有匹配到的字符取出
#6.str_length() 字符串长度统计函数
str
str_length(str)
#7.str_locate()/str_locate_all() 子串定位函数
str1<-c("sfdf","ulkdsf","yellow")
str_locate(str1,"f")
str_locate_all(str1,"f")
#区别:
#第一种:返回第一次匹配到的起止位置
#第二种:返回所有匹配到的起止位置
#8.str_pad() 字符串占据位置函数
str_pad("yellow",10,"left",pad=",")
str_pad("yellow",10,"right",pad="*")
#yellow占据了6个字符,所有空白为4个字符,left表示填充的方向
#9.str_replace() 字符串替换函数
str3<-"addsjhadsk"
str_replace(str3,"a","$")
str_replace_all(str3,"a","$")
#区别:
#第一种:只替换第一次匹配到的字符
#第二种:替换所有匹配到的字符
#10.str_split() 字符串切分函数
#str_split(string,pattern,n=Inf)
str3<-"abcddsesdafs"
str_split(str3,"a",2)
#n表示最大切分数,返回向量格式
#11.str_split_fixed() 字符串切分函数
#str_split_fixed(string,pattern,n)
str_split_fixed(str3,"a",3)
#返回矩阵形式
#12.str_sub() 取字符串子串函数
#str_sub(string,start=1L,end=-1L)
str3
str_sub(str3,1,4)
#13.str_sub() 子串替换形式
#str_sub(string,start=1L,end=-1L)<-replacement
str3
str_sub(str3,1,3)<-"hello"
str3
#14.str_trim() 去除空白函数
#str_trim(string,side="both")
a<-" sadjlsa "
str_trim(a,"left")
#去除左边的空白
str_trim(a,"both")
#15.word()
#word(string,start=1L,end=start,sep=fixed(""))
data<-c("a b c efg sadf sdf")
word(data,2,-1)#-1表示最后一个
word(data,2)
#sep默认为一个空字符
#16.str_wrap() 调整段落格式
#str_wrap(string,width=80,indent=0,exdent=0)
#width:表示每行的长度
#indent:句子首行的缩进字符数
#exdent:非首行的缩进字符数
thanks_path<-file.path(R.home("doc"),"THANKS")
thanks<-str_c(readLines(thanks_path),collapse = "\n")
thanks<-word(thanks,1,3,fixed("\n\n"))
cat(str_wrap(thanks),"\n")
cat(str_wrap(thanks,width=40),"\n")
cat(str_wrap(thanks,width=60,indent=2),"\n")
cat(str_wrap(thanks,width=60,exdent=2),"\n")
#17.实战1:替换
#Ex1:替换“岁”,得到数值型年龄
age<-c("21岁","22岁","23岁","24岁")
age_replace<-str_replace(age,"岁","")
age_replace
age_numeric<-as.numeric(age_replace)
age_numeric
#18.实战2:分割
vec<-c("a-b","b-c","d-e","u-p")
sep_loc<-str_locate(vec,"-")
sep_loc
str_sub(vec,0,sep_loc[,1]-1)->left
left
str_sub(vec,sep_loc[,1]+1,str_length(vec))->right
right