其他分享
首页 > 其他分享> > R语言中substr截取字符串

R语言中substr截取字符串

作者:互联网

 

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"

 

标签:字符,提取,##,截取,substr,字符串,nchar
来源: https://www.cnblogs.com/liujiaxin2018/p/16211941.html