01
grep()
grep(pattern,x)语句在字符串向量x里搜索给定字符串pattern。如果x里面有n个元素,则grep(pattern,x)会返回长度不超过n的向量。
> grep("love",c("love World","Our love","Preference"))
[1] 1 2
> grep("Love",c("love World","Our love","Preference"))
integer(0)
02
nchar()
> nchar("love World")
[1] 10
可以看出nchar()输出的结果是字符串的长度,属于字符计数函数。
3
paste()
> paste("Hello","China")
[1] "Hello China"
> paste("Hello","China",sep ="-")
[1] "Hello-China"
> paste0("Hello","China")
[1] "HelloChina"
> paste(c("A", "B"), c("C", "D"),sep =" ")
[1] "A C" "B D"
paste()是把字符串连接起来,输出一个长的字符串。paste()与cat()的区别可看往期文章。
04
substr()
substr(x,start,stop)
> substring("Preference",2,6)
[1] "refer"
substr(x,start,stop)返回的结果是x字符串中start-stop位置范围的字符串。
>dates <- c("Dec 4", "Nov 13", "Oct 23")
如果要分别提取dates中的月份简写和数字日期days要怎么操作?
#提取月份
> substr(dates, 1, 3)
[1] "Dec" "Nov" "Oct"
> substr(dates, 5, nchar(dates))
[1] "4" "13" "23"
如果要把dates中的Oct替换为Feb呢?
> substr(dates, 1, 3) <- c("Dec", "Nov", "Feb")
> dates
[1] "Dec 4" "Nov 13" "Feb 23"
05
strsplit()
> strsplit("love-World-love-Our",split="-")
[[1]]
[1] "love" "World" "love" "Our"
strsplit(x,split)是通过split将x分成若干个子字符串。
06
regexpr()
> regexpr("refer","Preference")
[1] 2
regexpr(pattern,text)在字符串text中寻找pattern,并返回pattern在text中起始位置。
07
sprintf()
> i<-4
> b<-sprintf("the square of %d is %d",i,i^2)
> b
[1] "the square of 4 is 16"
08
tolower( ) toupper()
> tolower("love World")
[1] "love world"
> toupper("love World")
[1] "LOVE WORLD"
tolower( )函数将文本转换为小写字母,而 toupper( )则相反