1、
> a <- "aaabbbcccddd" > substr(a,1,2) ## 提取字符串a的前两个字符串 [1] "aa"
> a <- "aaabbbcccddd" > substr(a,3,5) ## 3-5字符 [1] "abb" > substr(a, 5,nchar(a)) ## 5-最后一个字符 [1] "bbcccddd" > substr(a, nchar(a)-1, nchar(a)) ## 提取最后2个字符 [1] "dd"
> a <- c("abcdef", "1234567", "ABCDEFG" ) > a [1] "abcdef" "1234567" "ABCDEFG" > substr(a,1,3) ## 同样适用于向量 [1] "abc" "123" "ABC" > substr(a,3,5) [1] "cde" "345" "CDE" > substr(a,3,nchar(a)) [1] "cdef" "34567" "CDEFG" > substr(a, nchar(a) -2 , nchar(a)) [1] "def" "567" "EFG"