如何在R中分割包含特殊字符的字符串值?


当我们拥有一个单一的长字符串或一个字符串向量,并且字符串中的值被一些特殊字符分隔时,分割这些值可以帮助我们更好地理解这些字符串。这种情况可能发生在字符串数据记录有错误或有其他目的的情况下。我们可以使用`strsplit`函数进行分割。

示例

 在线演示

x1<-"tutorialspoint is an E-learning platform/FREE"
x1

输出

[1] "tutorialspoint is an E-learning platform/FREE"
strsplit(x1,split='/',fixed=TRUE) [[1]]
[1] "tutorialspoint is an E-learning platform"
[2] "FREE"

示例

 在线演示

x2<-"tutorialspoint is an E-learning platform&FREE"
x2

输出

[1] "tutorialspoint is an E-learning platform&FREE"
strsplit(x2,split='&',fixed=TRUE)
[[1]]
[1] "tutorialspoint is an E-learning platform"
[2] "FREE"

示例

 在线演示

x3<-"tutorialspoint is an E-learning platform !FREE"
x3

输出

[1] "tutorialspoint is an E-learning platform !FREE"
strsplit(x3,split='!',fixed=TRUE)
[[1]]
[1] "tutorialspoint is an E-learning platform "
[2] "FREE"

示例

 在线演示

x4<-"tutorialspoint is an E-learning platform @FREE"
x4

输出

[1] "tutorialspoint is an E-learning platform @FREE"
strsplit(x4,split='@',fixed=TRUE) [[1]]
[1] "tutorialspoint is an E-learning platform "
[2] "FREE"

示例

 在线演示

x5<-"tutorialspoint is an E-learning platform #FREE"
x5

输出

[1] "tutorialspoint is an E-learning platform #FREE"
strsplit(x5,split='#',fixed=TRUE) [[1]]
[1] "tutorialspoint is an E-learning platform "
[2] "FREE"

示例

 在线演示

x6<-"tutorialspoint is an E-learning platform $FREE"
x6

输出

[1] "tutorialspoint is an E-learning platform $FREE"
strsplit(x6,split='$',fixed=TRUE) [[1]]
[1] "tutorialspoint is an E-learning platform "
[2] "FREE"

示例

 在线演示

x7<-"tutorialspoint is an E-learning platform%FREE"
x7

输出

[1] "tutorialspoint is an E-learning platform%FREE"
strsplit(x7,split='%',fixed=TRUE) [[1]]
[1] "tutorialspoint is an E-learning platform"
[2] "FREE"

示例

 在线演示

x8<-"tutorialspoint is an E-learning platform^FREE"
x8

输出

[1] "tutorialspoint is an E-learning platform^FREE"
strsplit(x8,split='^',fixed=TRUE) [[1]]
[1] "tutorialspoint is an E-learning platform"
[2] "FREE"

示例

 在线演示

x9<-"tutorialspoint is an E-learning platform()FREE"
x9

输出

[1] "tutorialspoint is an E-learning platform()FREE"
strsplit(x9,split='()',fixed=TRUE) [[1]]
[1] "tutorialspoint is an E-learning platform"
[2] "FREE"

示例

 在线演示

x10<-"tutorialspoint is an E-learning platform:FREE"
x10

输出

[1] "tutorialspoint is an E-learning platform:FREE"
strsplit(x10,split=':',fixed=TRUE) [[1]]
[1] "tutorialspoint is an E-learning platform"
[2] "FREE"

示例

 在线演示

x11<-"tutorialspoint is an E-learning platform{}FREE"
x11

输出

[1] "tutorialspoint is an E-learning platform{}FREE"
strsplit(x11,split='{}',fixed=TRUE) [[1]]
[1] "tutorialspoint is an E-learning platform"
[2] "FREE"

示例

 在线演示

x12<-"tutorialspoint is an E-learning platform***FREE"
x12

输出

[1] "tutorialspoint is an E-learning platform***FREE"
strsplit(x12,split='***',fixed=TRUE) [[1]]
[1] "tutorialspoint is an E-learning platform"
[2] "FREE"

示例

 在线演示

x13<-c("tutorialspoint ^ is", "an ^ E-learning", "platform ^ & FREE")
x13

输出

[1] "tutorialspoint ^ is" "an ^ E-learning" "platform ^ & FREE"
strsplit(x13,split='^',fixed=TRUE) [[1]]
[1] "tutorialspoint " " is"
[[2]]
[1] "an " " E-learning"
[[3]]
[1] "platform " " & FREE"

示例

 在线演示

x14<-c("tutorialspoint ^is", "an ^E-learning", "platform & ^FREE")
x14

输出

[1] "tutorialspoint ^is" "an ^E-learning" "platform & ^FREE"
strsplit(x14,split='^',fixed=TRUE) [[1]]
[1] "tutorialspoint " "is"
[[2]]
[1] "an " "E-learning"
[[3]]
[1] "platform & " "FREE"

示例

 在线演示

x15<-c("tutorialspoint^is the best", "resource for^E-learning","in the^world")
x15

输出

[1] "tutorialspoint^is the best" "resource for^E-learning"
[3] "in the^world"
strsplit(x15,split='^',fixed=TRUE) [[1]] [1] "tutorialspoint" "is the best"
[[2]] [1] "resource for" "E-learning"
[[3]] [1] "in the" "world"

更新于:2020年10月9日

909 次浏览

开启您的职业生涯

通过完成课程获得认证

开始学习
广告