1、R语言strsplit用于分割字符串
创建测试数据
> test
> test
[1] "aa bb cc dd ee ff"
> class(test) ## 测试数据为字符
[1] "character"
2、按照指定分隔符拆分字符串
> a
> a
[[1]]
[1] "aa" "bb" "cc" "dd" "ee" "ff"
> class(a) ## 分割后的数据类型为列表
[1] "list"
> b
> b
[[1]]
[1] "a" "a" " " "b" "b" " " "c" "c" " " "d" "d" " " "e" "e" " " "f"
[17] "f"
> test
> strsplit(test,split = ",") ##指定分隔符为","进行拆分
[[1]]
[1] "aa" "bb" "cc" "dd" "ee!ff!GG"
> strsplit(test,split = "!") ##指定分隔符为"!"进行拆分
[[1]]
[1] "aa,bb,cc,dd,ee" "ff" "GG"
> test
> strsplit(test,split = ",") ##指定分隔符为","进行拆分
[[1]]
[1] "aa" "bb" "cc" "dd" "ee!ff!GG"
> strsplit(test,split = "!") ##指定分隔符为"!"进行拆分
[[1]]
[1] "aa,bb,cc,dd,ee" "ff" "GG"
> test
> strsplit(test,split = ".")
[[1]]
[1] "" "" "" "" "" "" "" "" "" "" "" "" "" ""
> strsplit(test,split = ".",fixed = T)
[[1]]
[1] "aa" "bb" "cc" "dd" "ee"
3、指定输出结果为向量
> test
> a
> a
[[1]]
[1] "aa" "bb" "cc" "dd" "ee"
> class(a)
[1] "list"
> b
> b
[1] "aa" "bb" "cc" "dd" "ee"
> class(b)
[1] "character"
到此这篇关于R语言strsplit函数用法深入详解的文章就介绍到这了,更多相关R语言strsplit函数内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!