正则表达
2019-08-29 本文已影响0人
Jason数据分析生信教室
190829
Rデータ自由自在 第7章 文字処理
7.2 文字列的表现和结合
- cat
\n
:换行
x = 7
y = 10
cat("x should be greater than y,but x=",x,"and y =",y,"\n")
fill=
指定每一行最大文字数
> cat("Long strings can","be sidplayed over",
+ "several lines using",
+ "the fill=argument",
+ fill=40)
Long strings can be sidplayed over
several lines using the fill=argument
- paste
collapse=
对于vector文字使用,指定文字间加入的要素
sep=
对vector不管用
> paste("one",2,"three",4,"five")
[1] "one 2 three 4 five"
> paste(c("one","two","three","four"),collapse=" ")
[1] "one two three four"
> paste("X",1:5,sep="")
[1] "X1" "X2" "X3" "X4" "X5"
> paste(c("X","Y"),1:6,sep="")
[1] "X1" "Y2" "X3" "Y4" "X5" "Y6"
指定collapse
会产生一整个文字串,不用的话就形成一串独立的数据
> paste(c("X","Y"),1:5,sep="_",collapse="|")
[1] "X_1|Y_2|X_3|Y_4|X_5"
> paste(c("X","Y"),1:5,"^",c("a","b"),sep="_",collapse="|")
[1] "X_1_^_a|Y_2_^_b|X_3_^_a|Y_4_^_b|X_5_^_a"
> paste(c("X","Y"),1:5,"^",c("a","b"),sep="_")
[1] "X_1_^_a" "Y_2_^_b" "X_3_^_a" "Y_4_^_b" "X_5_^_a"
7.3文字列的部分操作
substring
> substring(state.name,first=2,last=6)
[1] "labam" "laska" "rizon" "rkans" "alifo" "olora" "onnec" "elawa" "lorid" "eorgi" "awaii" "daho" "llino"
[14] "ndian" "owa" "ansas" "entuc" "ouisi" "aine" "aryla" "assac" "ichig" "innes" "issis" "issou" "ontan"
[27] "ebras" "evada" "ew Ha" "ew Je" "ew Me" "ew Yo" "orth " "orth " "hio" "klaho" "regon" "ennsy" "hode "
[40] "outh " "outh " "ennes" "exas" "tah" "ermon" "irgin" "ashin" "est V" "iscon" "yomin"
也可以执行vector的参数
> mystring="dog cat duck"
> substring(mystring,first=c(1,5,9),last=c(3,7,12))
[1] "dog" "cat" "duck"
> mystring="dog cat duck"
> substring(mystring,5,7)="feline"
> mystring
[1] "dog fel duck"
> mystring="dog cat duck"
> substring(mystring,5,7)="a"
> mystring
[1] "dog aat duck"
7.4 R的正则表达
- 会用到的函数
strsplit
grep
sub
gsub
regexpr
grepexpr