“凡是字符串,都要带引号”
“凡是字符串,都要带引号”
“凡是字符串,都要带引号”
————————————————————————
统计、提取、查找、替换、分割、连接
- NCHAR( ) 计算字符串长度
> nchar("Hello World")
[1] 11
> nchar(c(12,34,34456))
[1] 2 2 5
> month.name 十二个月份的名字
[1] "January" "February" "March"
[4] "April" "May" "June"
[7] "July" "August" "September"
[10] "October" "November" "December"
> nchar(month.name)
[1] 7 8 5 5 3 4 4 6 9 7 8 8
*****************nchar()与length()的区别:前者返回每个字符串的长度,后者返回字符串的个数
> length(month.name)
[1] 12
- PASTE( ) 连接字符串 (记得 sep = “ ” )
> paste(c("everybody","loves","sleep"))
[1] "everybody" "loves" "sleep" 错误❌不需要用c()
---------------------------------------
> paste("everybody","loves","sleep")
[1] "everybody loves sleep" 默认空格连接
> paste("everybody","loves","sleep",sep = "-") sep:连接符 = “ ”
[1] "everybody-loves-sleep"
> names <- c("LITTLE A","LITTLE B","LITTLE C")
> paste(names,"love you")
[1] "LITTLE A love you"
[2] "LITTLE B love you"
[3] "LITTLE C love you" 分别相连√√√
- SUBSTR( ) 提取某几位字符串中的字符 – substring
> substr(month.name,start = 1,stop = 3)
[1] "Jan" "Feb" "Mar" "Apr" "May" "Jun"
[7] "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
> temp <- substr(month.name,start=1,stop=3)
***********全字母大小写*************
> toupper(temp)
[1] "JAN" "FEB" "MAR" "APR" "MAY" "JUN"
[7] "JUL" "AUG" "SEP" "OCT" "NOV" "DEC"
> tolower(temp)
[1] "jan" "feb" "mar" "apr" "may" "jun"
[7] "jul" "aug" "sep" "oct" "nov" "dec"
- GREP( ) 、MATCH( ) 匹配函数→类似于索引?位置?
> x <- c("AC","B","A","A+")
> grep("A+",x)
[1] 1 3 4
> grep("A+",x,fixed=T)
[1] 4
> grep("A+",x,fixed=F)
[1] 1 3 4
> match("A+",x) 功能没有那么强大
[1] 4
- STRSPLIT( ) 分割数据串——分割成列表![ [ ] ]
> path <- c("/usr/local/R")
> strsplit(path,"/")
[[1]]
[1] "" "usr" "local" "R"
> strsplit(c(path,path),"/")
[[1]]
[1] "" "usr" "local" "R"
[[2]]
[1] "" "usr" "local" "R"
- OUTER( ) 相互连接?
> face <- 1:13
> suit <- c("spades","clubs","hearts","diamonds")
> outer(suit,face,FUN = paste)
[,1] [,2]
[1,] "spades 1" "spades 2"
[2,] "clubs 1" "clubs 2"
[3,] "hearts 1" "hearts 2"
[4,] "diamonds 1" "diamonds 2"
[,3] [,4]
[1,] "spades 3" "spades 4"
[2,] "clubs 3" "clubs 4"
[3,] "hearts 3" "hearts 4"
[4,] "diamonds 3" "diamonds 4"
[,5] [,6]
[1,] "spades 5" "spades 6"
[2,] "clubs 5" "clubs 6"
[3,] "hearts 5" "hearts 6"
[4,] "diamonds 5" "diamonds 6"
[,7] [,8]
[1,] "spades 7" "spades 8"
[2,] "clubs 7" "clubs 8"
[3,] "hearts 7" "hearts 8"
[4,] "diamonds 7" "diamonds 8"
[,9] [,10]
[1,] "spades 9" "spades 10"
[2,] "clubs 9" "clubs 10"
[3,] "hearts 9" "hearts 10"
[4,] "diamonds 9" "diamonds 10"
[,11] [,12]
[1,] "spades 11" "spades 12"
[2,] "clubs 11" "clubs 12"
[3,] "hearts 11" "hearts 12"
[4,] "diamonds 11" "diamonds 12"
[,13]
[1,] "spades 13"
[2,] "clubs 13"
[3,] "hearts 13"
[4,] "diamonds 13"
> outer(face,suit,FUN =paste,sep="-")
[,1] [,2]
[1,] "1-spades" "1-clubs"
[2,] "2-spades" "2-clubs"
[3,] "3-spades" "3-clubs"
[4,] "4-spades" "4-clubs"
[5,] "5-spades" "5-clubs"
[6,] "6-spades" "6-clubs"
[7,] "7-spades" "7-clubs"
[8,] "8-spades" "8-clubs"
[9,] "9-spades" "9-clubs"
[10,] "10-spades" "10-clubs"
[11,] "11-spades" "11-clubs"
[12,] "12-spades" "12-clubs"
[13,] "13-spades" "13-clubs"
[,3] [,4]
[1,] "1-hearts" "1-diamonds"
[2,] "2-hearts" "2-diamonds"
[3,] "3-hearts" "3-diamonds"
[4,] "4-hearts" "4-diamonds"
[5,] "5-hearts" "5-diamonds"
[6,] "6-hearts" "6-diamonds"
[7,] "7-hearts" "7-diamonds"
[8,] "8-hearts" "8-diamonds"
[9,] "9-hearts" "9-diamonds"
[10,] "10-hearts" "10-diamonds"
[11,] "11-hearts" "11-diamonds"
[12,] "12-hearts" "12-diamonds"
[13,] "13-hearts" "13-diamonds"