字符串拼接函数 str_c ,与R语言自带的 paste 和 paste0 函数具有相同的作用。
字符计数函数 str_count ,计算字符串中指定字符的个数。
字符检查函数 str_detect ,检查字符串中是否包含指定字符,返回逻辑向量。
字符复制函数 str_dup ,将字符向量重复若干次,返回重复后的字符向量。
字符提取函数 str_extract 和 str_extract_all ,对字符串进行提取, str_extract_all 函数返回所有的匹配结果。
字符串格式化函数 str_glue ,用花括号 {} 表示占位符,括号内的变量被替换成全局变量值。
字符串长度函数 str_length ,计算字符串长度。
字符位置提取函数 str_locate 和 str_locate_all ,返回匹配到的字符的位置。
字符匹配函数 str_match 和 str_match_all 与字符提取函数 str_extract 类似,返回匹配到的字符,不同之处在于返回格式。
字符补齐函数 str_pad ,用于在字符串中添加单个字符,可选择添加的位置,在参数 side 中进行设置。
字符删除函数 str_remove 和 str_remove_all ,用于删除字符串中的部分字符。
字符替换函数 str_replace 、 str_replace_all 和 str_replace_na ,用于替换字符串中的部分字符。
字符排序函数 str_sort 和 str_order ,对字符向量进行排序。
字符分割函数 str_split 和 str_split_fixed ,对字符串进行分割。
字符过滤函数 str_sub 和 str_subset , str_sub 函数通过指定开始和结束位置,过滤出字符串的部分字符串。 str_subset 函数通过匹配模式,过滤出满足模式的字符串。
stringr 包中其他的有用函数,用于常见的字符处理。
R语言字符串处理常用函数R语言中常用的字符串处理函数:
(1)得到字符串长度:nchar()
# 生成示范字符串
(2)剪切字符串:strsplit()
# 对字符串进行剪切
(3) 得到子字符串:substr()
# 得到子字符串
(4) 判断字符串中是否有某个字串:grepl()
# 判断字符串中是否有某个字串
(5) 字符串替换:gsub()
(6)字符串定位
在Hmisc包里,有一个函数可以很方便的对文本的的目标字符串进行定位,substring.location()
R语言特征:1. type.convert()函数主要用在read.table()函数中,返回向量和因子类型,当输入为double型时会丢失精度。
>type.convert(c('abc','bcd')) # 返回因子类型
[1] abc bcd
Levels: abc bcd
>type.convert(c(as.double(1.12121221111),'1.121')) # double型丢失精度
[1] 1.121212 1.121000
2. 如果一个文件包含有小数位的数据,通过read.table()函数读取时,会指定为numeric类型。
新建一个文件num.csv包括小数
1,2,1.11
2.1,3,4.5
用read.table读取文件,并查看列的类型。
>num<-read.table(file="num.csv",sep=",") # 读文件
>num
V1 V2 V3
1 1.0 2 1.11
2 2.1 3 4.50
>class(num)
[1] "data.frame"
>class(num$V1)# 查看列的类型为numeric
[1] "numeric"
3. tools包用Rdiff()函数的参数useDiff为FALSE时,与POSIX系统的diff -b命令类似。
新建文件num2.csv
3,2,1.11
2.1,3,4.5
用Rdiff()比较两个文件num.csv和num2.csv。
>Rdiff('num.csv','num2.csv',useDiff = FALSE)
1c1
<1,2,1.11
---
>3,2,1.11
[1] 1
4. 新函数anyNA(),结果与 any(is.na(.))一致,性能更好。
>is.na(c(1, NA))
[1] FALSE TRUE
>any(is.na(c(1, NA)))
[1] TRUE
>anyNA(c(1, NA))
[1] TRUE
5. arrayInd()和which()函数增加useNames参数,用于列名的匹配。我在测试过程,不太理解这个参数的意义。
>which
function (x, arr.ind = FALSE, useNames = TRUE)
6. is.unsorted()函数支持处理原始数据的向量。
>is.unsorted(1:10) # 排序的向量
[1] FALSE
>is.unsorted(sample(1:10)) # 无序的向量
[1] TRUE
7. 用于处理table的as.data.frame()函数和as.data.frame.table()函数,支持向provideDimnames(sep,base)函数传参数。我在测试过程中,也不理解具体是什么更新。
8. uniroot()函数增加新的可选参数extendInt,允许自动扩展取值范围,并增加返回对象参数init.it。
>f1 <- function(x) (121 - x^2)/(x^2+1) # 函数f1
>f2 <- function(x) exp(-x)*(x - 12) # 函数f2
>try(uniroot(f1, c(0,10))) # 在(0,10)的区间求f1函数的根
Error in uniroot(f1, c(0, 10)) :
f() values at end points not of opposite sign
>try(uniroot(f2, c(0, 2))) # 在(0,2)的区间求f2函数的根
Error in uniroot(f2, c(0, 2)) :
f() values at end points not of opposite sign
>str(uniroot(f1, c(0,10),extendInt="yes")) # 通过extendInt参数扩大取值搜索范围
List of 5
$ root : num 11
$ f.root: num -3.63e-06
$ iter : int 12
$ init.it : int 4
$ estim.prec: num 6.1e-05
>str(uniroot(f2, c(0,2), extendInt="yes")) # 通过extendInt参数扩大取值搜索范围
List of 5
$ root : num 12
$ f.root: num 4.18e-11
$ iter : int 23
$ init.it : int 9
$ estim.prec: num 6.1e-05
9. switch(f,)函数,当参数f是因子类型时,会出警告提示,需要转换字符串参数。
>switch(ff[1], A = "I am A", B="Bb..", C=" is C")# ->"A" # 警告提示
[1] "I am A"
Warning message:
In switch(ff[1], A = "I am A", B = "Bb..", C = " is C") :
EXPR is a "factor", treated as integer.
Consider using 'switch(as.character( * ), ...)' instead.
>switch(as.character(ff[1]), A = "I am A", B="Bb..", C=" is C") # 转型为字符串处理
[1] " is C"
10. 解析器已经更新,使用更少的内存。