R语言中的字符处理
(2011-07-10 22:29:48)R的字符串处理能力还是很强大的,具体有base包的几个函数和stringr包。
1.计算字符串的字符数
nchar()
paste(...,
=====================================
> paste(letters[1:5], "_1", sep = '')
[1] "a_1" "b_1" "c_1" "d_1" "e_1"
> paste(letters[1:5], collapse='_')
[1] "a_b_c_d_e"
=====================================3. 字符串分割
3.1 分隔符截取
strsplit(x,split, extended = TRUE, fixed = FALSE, perl = FALSE)
=================================================================================
QWE <- c("QWERTYUIOPASDFGHJKLZXCVBNM")
> unlist(strsplit(QWE, ""))
[1] "Q" "W" "E" "R" "T" "Y" "U" "I" "O" "P" "A" "S" "D" "F" "G" "H" "J" "K" "L"
[20] "Z" "X" "C" "V" "B" "N" "M"
> xtest #将xtest表示化成小时为单位的时间表示形式,以便用于坐标轴显示
[1] "0:00:04" "0:53:18" "1:53:18" "2:53:17" "3:53:16" "4:53:16"
[7] "5:53:16" "6:53:16" "7:53:16" "8:53:16" "9:53:15" "10:53:14"
[13] "11:53:13" "12:53:13" "13:53:13" "14:53:13" "15:53:13" "16:53:13"
[19] "17:53:13" "18:53:13" "19:53:13" "20:53:13" "21:53:13" "22:53:13"
[25] "23:53:14"
> xtest <- as.data.frame(strsplit(xtest, split= ":"))
> xtest
c..0....00....04.. c..0....53....18.. c..1....53....18.. c..2....53....17..
1 0 0 1 2
2 00 53 53 53
3 04 18 18 17
c..3....53....16.. c..4....53....16.. c..5....53....16.. c..6....53....16..
1 3
strsplit(x,
=================================================================================
QWE <- c("QWERTYUIOPASDFGHJKLZXCVB
> unlist(strsplit(QWE, ""))
[20] "Z" "X" "C" "V" "B" "N" "M"
> xtest
[13] "11:53:13" "12:53:13" "13:53:13" "14:53:13" "15:53:13" "16:53:13"
[19] "17:53:13" "18:53:13" "19:53:13" "20:53:13" "21:53:13" "22:53:13"
[25] "23:53:14"
> xtest <- as.data.frame(strsplit(xtest, split= ":"))
> xtest
1
2
3
1