统计
1 、字符数统计:nchar()
- length空字符时计数为1
- nchar空字符时计数为0
> x<-c("R","is","funny")
> nchar(x)
[1] 1 2 5
> length("")
[1] 1
> nchar("")
[1] 0
2 、转化为小写:tolower()
> DNA <- "AtGCtttACC"
> tolower(DNA)
[1] "atgctttacc"
3 、转化为大写:toupper()
> DNA <- "AtGCtttACC"
> toupper(DNA)
[1] "ATGCTTTACC"
4、替换函数:chartr("","",x)
chartr("A","B",x):字符串x中使用B替换A
> DNA <- "AtGCtttACC"
> chartr("Tt","Bb",DNA)
[1] "AbGCbbbACC"
> chartr("Tt","BB",DNA)
[1] "ABGCBBBACC"
字符串连接
5、字符串连接函数:paste()
> paste("Var",1:5,sep="")
[1] "Var1" "Var2" "Var3" "Var4" "Var5"
> x<-list(a='aaa',b='bbb',c="ccc")
> y<-list(d="163.com",e="qq.com")
> paste(x,y,sep="@")
[1] "aaa@163.com" "bbb@qq.com" "ccc@163.com"
#增加collapse参数,设置分隔符
> paste(x,y,sep="@",collapse=';')
[1] "aaa@163.com;bbb@qq.com;ccc@163.com"
> paste(x,collapse=';')
[1] "aaa;bbb;ccc"
字符串拆分
6、字符串拆分:strsplit()
语法格式:strsplit(x, split, fixed = FALSE, perl = FALSE, useBytes = FALSE)
- x为需要拆分的字串向量
- split为拆分位置的字串向量,默认为正则表达式匹配(fixed=FALSE),
设置fixed=TRUE,表示使用普通文本匹配或正则表达式的精确匹配。普通文本的运算速度快 - perl=TRUE/FALSE的设置和perl语言版本有关,如果正则表达式很长,正确设置表达式并且使用perl=TRUE可以提高运算速度。
- useBytes设置是否逐个字节进行匹配,默认为FALSE,即按字符而不是字节进行匹配。
- strsplit得到的结果是列表,后面要怎么处理就得看情况而定了
> text<-"today is a \nnice day!"
> text
[1] "today is a \nnice day!"
> strsplit(text," ")
[[1]]
[1] "today" "is" "a" "\nnice" "day!"
#换行符\n
> strsplit(text,'\\s')
[[1]]
[1] "today" "is" "a" "" "nice" "day!"
> class(strsplit(text, '\\s'))
[1] "list"
> strsplit(text,"")
[[1]]
[1] "t" "o" "d" "a" "y" " " "i" "s" " " "a" " " "\n" "n" "i" "c" "e" " " "d" "a"
[20] "y" "!"
字符串查询
7、字符串查询:grep(),grepl()
语法格式
grep(pattern, x, ignore.case = FALSE, perl = FALSE, value = FALSE,
fixed = FALSE, useBytes = FALSE, invert = FALSE)
grepl(pattern, x, ignore.case = FALSE, perl = FALSE,
fixed = FALSE, useBytes = FALSE)
两者的差别
- grep正则匹配后返回匹配项的下标
- grepl正则匹配后返回逻辑TRUE或者FALSE
> grep("\\.r$",files)
[1] 3 5 8 9 10 11 12 16 18 20 22 24 25 26 29
> grepl("\\.r$",files)
[1] FALSE FALSE TRUE FALSE TRUE FALSE FALSE TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE TRUE
[17] FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE TRUE TRUE FALSE FALSE TRUE FALSE FALSE FALSE
[33] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
两者用于提取数据子集的结果是一样的
> files[grep("\\.r$",files)]
[1] "agricolae.r" "cluster.r" "gam_model.r"
[4] "GBM.r" "gbm_model.r" "gbm_model1.r"
[7] "GBM1.r" "item-based CF推荐算法.r" "MASS_e107_rpart.r"
[10] "PankRange.r" "quantmod.r" "recommenderlab.r"
[13] "Rplot.r" "rules.r" "survival.r"
> files[grepl("\\.r$",files)]
[1] "agricolae.r" "cluster.r" "gam_model.r"
[4] "GBM.r" "gbm_model.r" "gbm_model1.r"
[7] "GBM1.r" "item-based CF推荐算法.r" "MASS_e107_rpart.r"
[10] "PankRange.r" "quantmod.r" "recommenderlab.r"
[13] "Rplot.r" "rules.r" "survival.r"
8、字符串查询:regexpr(),gregexpr(),regexec()
- 匹配具体位置和字符串长度
- 可以用于字符串的提取操作
> text<-c("Hello, Adam","Hi,Adam!","How are you,Adam")
> text
[1] "Hello, Adam" "Hi,Adam!" "How are you,Adam"
> regexpr("Adam",text)
[1] 8 4 13
attr(,"match.length")
[1] 4 4 4
attr(,"useBytes")
[1] TRUE
> gregexpr("Adam",text)
[[1]]
[1] 8
attr(,"match.length")
[1] 4
attr(,"useBytes")
[1] TRUE
[[2]]
[1] 4
attr(,"match.length")
[1] 4
attr(,"useBytes")
[1] TRUE
[[3]]
[1] 13
attr(,"match.length")
[1] 4
attr(,"useBytes")
[1] TRUE
> regexec("Adam",text)
[[1]]
[1] 8
attr(,"match.length")
[1] 4
[[2]]
[1] 4
attr(,"match.length")
[1] 4
[[3]]
[1] 13
attr(,"match.length")
[1] 4
字符串替换
9、字符串替换:sub(),gsub()
- 严格地说R语言没有字符串替换的函数
- R语言对参数都是传值不传址
- sub和gsub的区别是前者只做一次替换,gsub把满足条件的匹配都做替换
> text<-c("Hello, Adam","Hi,Adam!","How are you,Ava")
> sub(pattern="Adam",replacement="word",text)
[1] "Hello, word" "Hi,word!" "How are you,Ava"
> sub(pattern="Adam|Ava",replacement="word",text)
[1] "Hello, word" "Hi,word!" "How are you,word"
> gsub(pattern="Adam|Ava",replacement="word",text)
[1] "Hello, word" "Hi,word!" "How are you,word"
字符串提取
- substr和substring函数通过位置进行字符串拆分或提取,它们本身并不使用正则表达式
- 结合正则表达式函数regexpr、gregexpr或regexec使用可以非常方便地从大量文本中提取所需信息
语法格式
substr(x, start, stop)
substring(text, first, last = 1000000L)
- 第 1个参数均为要拆分的字串向量,第2个参数为截取的起始位置向量,第3个参数为截取字串的终止位置向量
- substr返回的字串个数等于第一个参数的长度
- substring返回字串个数等于三个参数中最长向量长度,短向量循环使用
> x <- "123456789"
> substr(x, c(2,4), c(4,5,8))
[1] "234"
> substring(x, c(2,4), c(4,5,8))
[1] "234" "45" "2345678"
因为x的向量长度为1,substr获得的结果只有1个字串,
即第2和第3个参数向量只用了第一个组合:起始位置2,终止位置4。
substring的语句三个参数中最长的向量为c(4,5,8),执行时按短向量循环使用的规则第一个参数事实上就是c(x,x,x),
第二个参数就成了c(2,4,2),最终截取的字串起始位置组合为:2-4, 4-5和2-8。
网友评论