亲宝软件园·资讯

展开

R语言strsplit函数 R语言strsplit函数用法深入详解

返回主页小虾米2018 人气:1
想了解R语言strsplit函数用法深入详解的相关内容吗,返回主页小虾米2018在本文为您仔细讲解R语言strsplit函数的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:R语言strsplit函数,R语言字符串分割,下面大家一起来学习吧。

1、R语言strsplit用于分割字符串

创建测试数据

> test <- "aa bb cc dd ee ff" ##创建测试数据
> test
[1] "aa bb cc dd ee ff"
> class(test) ## 测试数据为字符
[1] "character"

2、按照指定分隔符拆分字符串

> a <- strsplit(test,split = " ") ##制动分隔符为空格进行拆分数据
> a
[[1]]
[1] "aa" "bb" "cc" "dd" "ee" "ff"

> class(a) ## 分割后的数据类型为列表
[1] "list"
> b <- strsplit(test,split = "") ## 分隔符设为空,则拆分每一个字符
> b
[[1]]
 [1] "a" "a" " " "b" "b" " " "c" "c" " " "d" "d" " " "e" "e" " " "f"
[17] "f"
> test <- "aa,bb,cc,dd,ee!ff!GG" ##重新创建测试数据
> 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 <- "aa,bb,cc,dd,ee!ff!GG" ##重新创建测试数据
> 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 <- "aa.bb.cc.dd.ee"   ##同上
> strsplit(test,split = ".")
[[1]]
 [1] "" "" "" "" "" "" "" "" "" "" "" "" "" ""

> strsplit(test,split = ".",fixed = T)
[[1]]
[1] "aa" "bb" "cc" "dd" "ee"

3、指定输出结果为向量

> test <- "aa,bb,cc,dd,ee" ##生产测试数据
> a <- strsplit(test,split = ",") 
> a
[[1]]
[1] "aa" "bb" "cc" "dd" "ee"

> class(a)
[1] "list"
> b <- unlist(strsplit(test,split = ",")) ## 加unlist函数
> b
[1] "aa" "bb" "cc" "dd" "ee"
> class(b)  
[1] "character"

加载全部内容

相关教程
猜你喜欢
用户评论