> paste("ai","ya","ya",sep="*")#一个元素直接粘起来
[1] "ai*ya*ya"
> paste(c("ai","ya","ya"),1:2,sep="*")#向量粘贴对应位置粘贴,长度不够循环粘贴
[1] "ai*1" "ya*2" "ya*1"
> paste(c("ai","ya","ya"),collapse = "-")#向量内粘贴,sep向量间粘贴
[1] "ai-ya-ya"
> paste(c("ai","ya","ya"),1:3,sep="*",collapse = "-")#先sep再collapse
[1] "ai*1-ya*2-ya*3"
> paste0("ai","ya","ya")#直接粘贴,无连接符
[1] "aiyaya"
> x=c("bizzare","love","triangle","we're","meant")
> strsplit(x,"a")#按a分割
[[1]]
[1] "bizz" "re"
[[2]]
[1] "love"
[[3]]
[1] "tri" "ngle"
[[4]]
[1] "we're"
[[5]]
[1] "me" "nt"
> strsplit("love.U",".")#正则表达式.代表任意字符
[[1]]
[1] "" "" "" "" "" ""
> strsplit("love.U","[.]")#[.]就正常了
[[1]]
[1] "love" "U"
> strsplit("love.U",".",fixed = T)#fixed固定格式分割
[[1]]
[1] "love" "U"
> strsplit("abcd",NULL)#NULL切割直接分割
[[1]]
[1] "a" "b" "c" "d"
> l=strsplit(c("li lei","han meimei")," ")#切割后是2个列表
> l[[1]]
[1] "li" "lei"
> l
[[1]]
[1] "li" "lei"
[[2]]
[1] "han" "meimei"
> unlist(strsplit(c("li lei","han meimei")," "))#unlist后是一个向量
[1] "li" "lei" "han" "meimei"
> x
[1] "bizzare" "love" "triangle" "we're" "meant"
> substr(x,2,5) #向量每一个从2截取到5
[1] "izza" "ove" "rian" "e're" "eant"
> substr(l,5,20)#列表内每一个向量2截取到5
[1] "i\", \"lei\")" "an\", \"meimei\")"
> substr(x,2,1:4)#第一个从2开始,1结束,第二个2开始,2结束,第三个2开始,3结束,第2个开始,4结束,多余长度循环
[1] "" "o" "ri" "e'r" ""
> l=c(3,2,7,3,6)
> substr(x,1,l)
[1] "biz" "lo" "triangl" "we'" "meant"
> substr(x,2,1:4)="替换"
> x
[1] "bizzare" "l替ve" "t替换angle" "w替换re" "meant"
> x=c("better together","never known","gone")
> chartr("er","~-",x)#凡是碰到e都替换成~,r替换成-
[1] "b~tt~- tog~th~-" "n~v~- known" "gon~"
> sub("er","~-~",x)#把er当成整体替换,只替换第一次
[1] "bett~-~ together" "nev~-~ known" "gone"
> gsub("er","~-~",x)#全部er都替换,重复n次
[1] "bett~-~ togeth~-~" "nev~-~ known" "gone"
> gsub("on",12,c("on my way","Once agin"))#区分大小写
[1] "12 my way" "Once agin"
> gsub("on",12,c("on my way","Once again"),ignore.case = T)
[1] "12 my way" "12ce again"
> gsub("[^a-Z]"," ",c("hi~~~~~.","bye___"))
Error in gsub("[^a-Z]", " ", c("hi~~~~~.", "bye___")) :
invalid regular expression '[^a-Z]', reason 'Invalid character range'
In addition: Warning message:
In gsub("[^a-Z]", " ", c("hi~~~~~.", "bye___")) :
TRE pattern compilation error 'Invalid character range'
> a=substr(c("$1,000","$2,000"),2,20)#去除$
> as.numeric(gsub(",","",a))#去除,号,并转换成numeric类型
[1] 1000 2000
> grep("er",x)#数值位置
[1] 1 2
> grepl("er",x)#逻辑位置
[1] TRUE TRUE FALSE
> toupper(c("I","LOve","r"))#转大写
[1] "I" "LOVE" "R"
> tolower(c("I","lOVE","R"))#转小写
[1] "i" "love" "r"
> x
[1] "better together" "never known" "gone"
> regexpr("er",x)#regular expression
[1] 5 4 -1
attr(,"match.length")
[1] 2 2 -1
attr(,"index.type")
[1] "chars"
attr(,"useBytes")
[1] TRUE
> gregexpr("er",x)#重复regular expression
[[1]]
[1] 5 14
attr(,"match.length")
[1] 2 2
attr(,"index.type")
[1] "chars"
attr(,"useBytes")
[1] TRUE
[[2]]
[1] 4
attr(,"match.length")
[1] 2
attr(,"index.type")
[1] "chars"
attr(,"useBytes")
[1] TRUE
[[3]]
[1] -1
attr(,"match.length")
[1] -1
attr(,"index.type")
[1] "chars"
attr(,"useBytes")
[1] TRUE
> match(1:10,c(1,3,4,9))#1:10 到c里面找,返回位置信息,没找到返回NA
[1] 1 NA 2 3 NA NA NA NA 4 NA
> b=match(1:10,c(1,3,4,9),nomatch = 0)>0#没有找到的返回0
> 1:10 %in% c(1,3,4,9)#返回逻辑位置
[1] TRUE FALSE TRUE TRUE FALSE FALSE FALSE FALSE TRUE FALSE
> a=matrix(1:10,5,2)
> a
[,1] [,2]
[1,] 1 6
[2,] 2 7
[3,] 3 8
[4,] 4 9
[5,] 5 10
> a[c(T,T,F,T,F),]
[,1] [,2]
[1,] 1 6
[2,] 2 7
[3,] 4 9
> paste("I","love","r",sep="!")#!粘贴
[1] "I!love!r"
> paste(c("I","love","r"),1:3,sep = "&",collapse="-")#c和1:3二个向量合并,先sep合成1个向量-再粘贴
[1] "I&1-love&2-r&3"
> x=c("jiang zu","xu hu")
> toupper(x)
[1] "JIANG ZU" "XU HU"
> unlist(strsplit(x," "))#strsplit切割后是列表,列表解除再返回向量
[1] "jiang" "zu" "xu" "hu"
> gsub(" ","&&", x)#替换
[1] "jiang&&zu" "xu&&hu"
> regexpr(" ",x)#返回所在位置
[1] 6 3
attr(,"match.length")
[1] 1 1
attr(,"index.type")
[1] "chars"
attr(,"useBytes")
[1] TRUE
> substr(x,regexpr(" ",x)+1,20)#切割
[1] "zu" "hu"