R 字符串处理(2)
1.strsplit(m,n) 分割字符串:将字符串m,在含有字符n的地方进行分割
如:strsplit("abcdef","e"):
结果为:
[[1]]
[1] "abcd" "f"
strsplit(c("ab","cde","mnd"),"e") 结果为:
[[1]]
[1] "ab"
[[2]]
[1] "cd"
[[3]]
[1] "mnd"
2.substr(m,index1,length):截取字符串m,从index1索引处截取长度为length的字符串;
> substr("abcde",1,3)
[1] "abc"
#字符串连接:
paste() #paste(..., sep = " ", collapse = NULL)
#字符串分割:
strsplit() #strsplit(x, split, extended = TRUE, fixed = FALSE, perl = FALSE)
#计算字符串的字符数:
nchar()
#字符串截取:
substr(x, start, stop)
substring(text, first, last = 1000000)
substr(x, start, stop) <- value
substring(text, first, last = 1000000) <- value
#字符串替换及大小写转换:
chartr(old, new, x)
tolower(x)
toupper(x)
casefold(x, upper = FALSE)
字符完全匹配
grep()
字符不完全匹配
agrep()
字符替换
gsub()
#以上这些函数均可以通过perl=TRUE来使用正则表达式。
grep(pattern, x, ignore.case = FALSE, extended = TRUE,
perl = FALSE, value = FALSE, fixed = FALSE, useBytes = FALSE)
sub(pattern, replacement, x,
ignore.case = FALSE, extended = TRUE, perl = FALSE,
fixed = FALSE, useBytes = FALSE)
gsub(pattern, replacement, x,
ignore.case = FALSE, extended = TRUE, perl = FALSE,
fixed = FALSE, useBytes = FALSE)
regexpr(pattern, text, ignore.case = FALSE, extended = TRUE,
perl = FALSE, fixed = FALSE, useBytes = FALSE)
gregexpr(pattern, text, ignore.case = FALSE, extended = TRUE,
perl = FALSE, fixed = FALSE, useBytes = FALSE)
See Also:
regular expression (aka 'regexp') for the details of the pattern
specification.
'glob2rx' to turn wildcard matches into regular expressions.
'agrep' for approximate matching.
'tolower', 'toupper' and 'chartr' for character translations.
'charmatch', 'pmatch', 'match'. 'apropos' uses regexps and has
nice examples.
用R来处理字符串数据并不是一个很好的选择,还是推荐使用Perl或者Python等语言。不过R本身也提供了一些常用的字符串处理函数,这篇文章就对这些字符串函数做一个简单的总结,具体各个函数的使用方法还是要参考R的帮助文档。
- 取子字符串
substring(text, first, last=1000000 )
x为字符串向量。两个函数的不同之处在于函数substr()必须指定子字符串的起始位置和结束位置;而substring()可以不用指定结束位置,默认为1000000,如果字符串的长度小于1000000,则默认为取到字符串的结尾处。
> substr("sinablog",2,4)
[1] "ina"
> substring("sinablog",2,4)
[1] "ina"
> substring("sinablog",5)
[1] "blog"
> substr("sinablog",5,10)
[1] "blog"
另一个例子,此时x是一个含有多个元素的字符串向量:
> x <- c("asfef", "qwerty", "yuiop[", "b", "stuff.blah.yech")
> substring(x, 2, 5)
[1] "sfef" "wert" "uiop" "" "tuff"
- 字符串替换
substring(text, first, last = 1000000)<-value
同样的,substr必须指定开始处,结尾处,而substring结尾处默认为1000000,基本大于大多数的字符串长度,可以不用指定。
> x <- c("asfef", "qwerty", "yuiop[", "b", "stuff.blah.yech")
> substring(x, 2) <- c("..", "+++","test","test","test")
> x
[1] "a..ef" "q+++ty" "ytest[" "b" "stest.blah.yech"
> substr(x, 2,4) <- c("..", "+++","test","test","test")
> x
[1] "a..ef" "q+++ty" "ytest[" "b" "stest.blah.yech"
- 字符串拆分
其中x为待拆分的字符串向量,split拆分模式(可以使用正则表达式);fixed为TRUE表示精确匹配,否则表示可以使用正则表达式;perl为TRUE表示要是用Perl兼容正则表达式;由于可以使用正则表达式,所以一些特殊字符有特殊的含义,在使用的时候要注意对特殊字符的转义。
> strsplit("a.b.c", ".")
[[1]]
[1] "" "" "" "" ""
这是因为在正则表达式中,点“.”是通配符,表示任意的单个字符。而要想得到普通意义上的结果,要使用两个反斜杠"\\"对通配符“.”进行转义(这是windows下的试验,由于手边没有linux服务器,没有测试linux下是使用一个反斜杠还是两个,猜测是一个即可)或者使用精确匹配(即限定参数fixed=TRUE)来实现。
另外需要说明一点的是:直接使用split函数得到的结果是一个列表,如果希望得到一个向量,可以使用
unlist函数。
> unlist(strsplit("a.b.c", "\\."))
[1] "a" "b" "c"
> unlist(strsplit("a.b.c", ".",fixed=TRUE))
[1] "a" "b" "c"
若希望得到各个字母组成的字符串向量:
unlist(strsplit("abc",""))
[1] "a" "b" "c"
- 字符串连接
其中sep表示不同的字符串之间的分隔符,默认为空格。
> paste("a","b","c",sep=".")
[1] "a.b.c"
> paste("A", 1:6)
[1] "A 1" "A 2" "A 3" "A 4" "A 5" "A 6"
- 获取字符串的长度
其中type表示测量单位,有三个选择:chars,bytes,width。
nzchar(x)用于判断一个变量的长度是否为0。
需要注意的是,对于缺失值NA,nzchar()的结果为TRUE,而函数nchar()的返回结果为2。所以在对字符串进行测量之前,最好先使用is.na()函数判断一下是否是NA值。
> x<-"sinablog"
> nchar(x)
[1] 8
> nzchar(x)
[1] TRUE
> x<-NA
> is.na(x)
[1] TRUE
> nchar(x)
[1] 2
> nzchar(x)
[1] TRUE
- 从左端起取字符串的特定长度子串
width为要取的长度,如果width的值大于字符串x的长度,则默认取到x的结尾。
> strtrim(c("abcdef", "abcdef", "abcdef"), c(1,5,10))
[1] "a" "abcde" "abcdef"
浙公网安备 33010602011771号